Reputation: 189
I'm new to C++ and vectors and have been try to experiment with some of the STL functions and am wondering how come this is not working. I'm guessing its because of my first and last positions, are they not allowed to be ints?
#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);
}
Upvotes: 1
Views: 166
Reputation: 23664
According to max_element
documentation: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);
The first two parameters have to be FowardIterator
, not simple integers.
You may try the following:
std::cout << "The maximum value is: "
<< *max_element(scramble.begin(), scramble.end());
Upvotes: 8