c++ - 查找 vector 的 vector 的最大值/最小值

标签 c++ c++11 vector max min

找到 vector 的 vector 的最大/最小项的最有效和标准 (C++11/14) 方法是什么?

std::vector<std::vector<double>> some_values{{5,0,8},{3,1,9}};

想要的最大元素是 9

想要的最小元素是0

最佳答案

这是一个多线程解决方案,它为通用类型 T 返回一个迭代器(或抛出)到最大值(假设 operator< 是为 T 定义的)。请注意,最重要的优化是对“列”执行内部最大操作,以利用 C++ 的列优先顺序。

#include <vector>
#include <algorithm>

template <typename T>
typename std::vector<T>::const_iterator max_element(const std::vector<std::vector<T>>& values)
{
    if (values.empty()) throw std::runtime_error {"values cannot be empty"};

    std::vector<std::pair<typename std::vector<T>::const_iterator, bool>> maxes(values.size());

    threaded_transform(values.cbegin(), values.cend(), maxes.begin(),
                       [] (const auto& v) {
                           return std::make_pair(std::max_element(v.cbegin(), v.cend()), v.empty());
                       });

    auto it = std::remove_if(maxes.begin(), maxes.end(), [] (auto p) { return p.second; });

    if (it == maxes.begin()) throw std::runtime_error {"values cannot be empty"};

    return std::max_element(maxes.begin(), it,
                            [] (auto lhs, auto rhs) {
                                return *lhs.first < *rhs.first;
                            })->first;
}

threaded_transform (还)不是标准库的一部分,但这里有一个你可以使用的实现。

#include <vector>
#include <thread>
#include <algorithm>
#include <cstddef>

template <typename InputIterator, typename OutputIterator, typename UnaryOperation>
OutputIterator threaded_transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op, unsigned num_threads)
{
    std::size_t num_values_per_threads = std::distance(first, last) / num_threads;

    std::vector<std::thread> threads;
    threads.reserve(num_threads);

    for (int i = 1; i <= num_threads; ++i) {
        if (i == num_threads) {
            threads.push_back(std::thread(std::transform<InputIterator,
                                      OutputIterator, UnaryOperation>,
                                      first, last, result, op));
        } else {
            threads.push_back(std::thread(std::transform<InputIterator,
                                      OutputIterator, UnaryOperation>,
                                      first, first + num_values_per_threads,
                                      result, op));
        }
        first  += num_values_per_threads;
        result += num_values_per_threads;
    }

    for (auto& thread : threads) thread.join();

    return result;
}

template <typename InputIterator, typename OutputIterator, typename UnaryOperation>
OutputIterator threaded_transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op)
{
    return threaded_transform<InputIterator, OutputIterator, UnaryOperation>(first, last, result, op, std::thread::hardware_concurrency());
}

关于c++ - 查找 vector 的 vector 的最大值/最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31556072/

相关文章:

c++ - 在 C++ 中将指针传递给函数

c++ - 如何通过将 vector 元素传递给函数来更改 vector 元素

c++ - 在 llvm 中位转换后 vector 的位布局

c++ - 你怎么知道当前对象是什么?

c++ - 为什么 char* 和 std::string& 的别名优化结果不同?

具有不同参数的 C++ 运算符重载模板

c++ - Boost asio ip tcp iostream 是否支持异步?

c++ - 使用删除-删除范例将元素从一个 vector move 到另一个 vector

c++ - 我的overflow()的实现有什么问题?

python - scikit learn TSNE 转换应用于词向量时返回奇怪的结果