0
int N = 6;
vector< vector<int> > A(N, vector<int>(3));

/* Do operation with A */

cout<<(*max_element(a.begin(),a.end()))[2]<<endl;

我不确定是什么max_element这里在做什么。任何人都可以帮助理解这一点吗?

PS:我在TopCoder练习室复习indy256的解决方案时遇到了这个问题,同时解决了这个问题。

4

1 回答 1

5

Lexicographicaly comparing (because the elements are vectors), max_element finds the largest element in the vector a. It returns an iterator that's immediately dereferenced, giving a reference to the element. It then calls calls operator[], giving back the element at index 2 that's ultimately streamed to cout.

A less terse equivalent would be:

auto it = max_element(a.begin(), a.end());
int i = (*it)[2]; // better make sure the vector has at least 3 elements!

cout << i;
于 2013-07-21T10:53:35.620 回答