1

我是 C++ 和向量的新手,一直在尝试尝试一些 STL 函数,我想知道这怎么不起作用。我猜是因为我的第一个和最后一个位置,它们不允许是整数吗?

#include <cstdlib>
#include <vector>
#include <iostream>

using namespace std;

/*
 * 
 */
int main() {

    const int lowest = 10;
    const int highest = 99;

    vector<int> scramble;
    for (int i = 0; i < 20; i++){
        scramble.push_back(lowest + rand() % (highest - lowest + 1));
        cout << scramble.at(i) << endl;
    }
    int first = 0;
    int last = 19;

   cout << "The maximum value is: " << max_element(first, last);


}
4

1 回答 1

8

根据max_element文档:std::max_element

std::max_element  
template <class ForwardIterator>
ForwardIterator max_element (ForwardIterator first, ForwardIterator last);

template <class ForwardIterator, class Compare>
ForwardIterator max_element (ForwardIterator first, ForwardIterator last,
                          Compare comp);

前两个参数必须是FowardIterator,而不是简单的整数。

您可以尝试以下方法:

 std::cout << "The maximum value is: " 
  << *max_element(scramble.begin(), scramble.end());
于 2013-05-07T23:57:55.507 回答