C++ 在 vector<double> 上使用 std::max_element

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10158756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:41:00  来源:igfitidea点击:

Using std::max_element on a vector<double>

c++vectormaxmin

提问by synaptik

I'm trying to use std::min_elementand std::max_elementto return the min and max elements in a vector of doubles. 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.

我正在尝试使用std::min_elementstd::max_element返回双精度向量中的最小和最大元素。我的编译器不喜欢我目前尝试使用它们的方式,而且我不明白错误消息。我当然可以编写自己的程序来找到最小值/最大值,但我想了解如何使用这些函数。

#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?

有人可以解释一下我做错了什么吗?

回答by David Schwartz

min_elementand max_elementreturn iterators, not values. So you need *min_element...and *max_element....

min_elementmax_element返回迭代器,而不是值。所以你需要*min_element...*max_element...

回答by Johnsyweb

As others have said, std::max_element()and std::min_element()return iterators, which need to be dereferencedto 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 positionof 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. The valuesfor my example above would be the same, but the position of the "max" element would be 9since...

std::minmax_element()对于大型数据集,按照上述评论中的建议使用可能会更快,但可能会给出略有不同的结果。我上面例子的是一样的,但是“max”元素的位置是9因为......

If several elements are equivalent to the largest element, the iterator to the last such element is returned.

如果多个元素等价于最大元素,则返回到最后一个这样的元素的迭代器。

回答by Cornstalks

min/max_element return the iteratorto the min/max element, not the value of the min/max element. You have to dereference the iterator in order to get the value out and assign it to a double. That is:

min/max_element 将迭代器返回到 min/max 元素,而不是 min/max 元素的值。您必须取消引用迭代器才能取出值并将其分配给双精度值。那是:

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