我是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);
}
答案 0 :(得分: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());