矢量和使用STL

vectors and using the STL

本文关键字:STL      更新时间:2023-10-16

我是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);

}

根据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());
相关文章:
  • 没有找到相关文章