使用std::max_element在vector。

时间:2022-09-05 22:52:17

I'm trying to use std::min_element and std::max_element to 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_element和std::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?

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

3 个解决方案

#1


98  

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

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

#2


52  

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. The values for my example above would be the same, but the position of the "max" element would be 9 since...

使用std::minmax_element()在上面的注释中建议可能对大数据集更快,但是可能会给出稍微不同的结果。上面我的示例的值是相同的,但是“max”元素的位置是9,因为…

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

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

#3


25  

min/max_element return the iterator to 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元素的值。为了得到值并将其赋值为double,您必须取消迭代器。那就是:

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

#1


98  

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

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

#2


52  

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. The values for my example above would be the same, but the position of the "max" element would be 9 since...

使用std::minmax_element()在上面的注释中建议可能对大数据集更快,但是可能会给出稍微不同的结果。上面我的示例的值是相同的,但是“max”元素的位置是9,因为…

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

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

#3


25  

min/max_element return the iterator to 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元素的值。为了得到值并将其赋值为double,您必须取消迭代器。那就是:

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