简体   繁体   English

在向量<double>上使用std :: max_element

[英]Using std::max_element on a vector<double>

I'm trying to use std::min_element and std::max_element to return the min and max elements in a vector of doubles. 我正在尝试使用std::min_elementstd::max_element来返回双精度向量中的min和max元素。 My compiler doesn't like how I'm currently trying to use them, and I don't understand the error message. 我的编译器不喜欢我目前正在尝试使用它们,我不理解错误消息。 I could of course write my own procedure to find the min/max, but I'd like to understand how to use the functions. 我当然可以编写自己的程序来查找最小值/最大值,但我想了解如何使用这些函数。

#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char** argv) {

    double cLower, cUpper;
    vector<double> C;

    // code to insert values in C not shown here

    cLower = min_element(C.begin(), C.end());
    cUpper = max_element(C.begin(), C.end());

    return 0;
}

Here is the compiler error: 这是编译器错误:

../MIXD.cpp:84: error: cannot convert '__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >' to 'double' in assignment
../MIXD.cpp:85: error: cannot convert '__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >' to 'double' in assignment

Would someone please explain what I'm doing wrong? 有人请解释我做错了什么吗?

min_element and max_element return iterators, not values. min_elementmax_element返回迭代器,而不是值。 So you need *min_element... and *max_element... . 所以你需要*min_element...*max_element...

As others have said, std::max_element() and std::min_element() return iterators , which need to be dereferenced to obtain the value . 正如其他人所说, std::max_element()std::min_element()返回迭代器 ,需要取消引用才能获得该

The advantage of returning an iterator (rather than just the value) is that it allows you to determine the position of the (first) element in the container with the maximum (or minimum) value. 返回迭代器(而不仅仅是值)的优点是它允许您使用最大(或最小)值确定容器中(第一个)元素的位置

For example (using C++11 for brevity): 例如(为简洁起见使用C ++ 11):

#include <vector>
#include <algorithm>
#include <iostream>

int main()
{
    std::vector<double> v {1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0};

    auto biggest = std::max_element(std::begin(v), std::end(v));
    std::cout << "Max element is " << *biggest
        << " at position " << std::distance(std::begin(v), biggest) << std::endl;

    auto smallest = std::min_element(std::begin(v), std::end(v));
    std::cout << "min element is " << *smallest
        << " at position " << std::distance(std::begin(v), smallest) << std::endl;
}

This yields: 这会产生:

Max element is 5 at position 4
min element is 1 at position 0

Note: 注意:

Using std::minmax_element() as suggested in the comments above may be faster for large data sets, but may give slightly different results. 对于大型数据集,使用上面注释中建议的std::minmax_element()可能会更快,但可能会产生稍微不同的结果。 The values for my example above would be the same, but the position of the "max" element would be 9 since... 上面我的示例的将是相同的,但“max”元素的位置将是9因为......

If several elements are equivalent to the largest element, the iterator to the last such element is returned. 如果多个元素等效于最大元素,则返回最后一个元素的迭代器。

min/max_element return the iterator to the min/max element, not the value of the min/max element. min / max_element将迭代器返回到min / max元素,而不是min / max元素的值。 You have to dereference the iterator in order to get the value out and assign it to a double. 您必须取消引用迭代器才能获取值并将其分配给double。 That is: 那是:

cLower = *min_element(C.begin(), C.end());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM