user1719605
user1719605

Reputation: 189

vectors and using the STL

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

Answers (1)

taocp
taocp

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

Related Questions