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的解决方案时遇到了这个问题,同时解决了这个问题。
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的解决方案时遇到了这个问题,同时解决了这个问题。
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;