While trying to solve http://www.usaco.org/index.php?page=viewproblem2&cpid=891, I coded :
vector<int> abc(3) ;
vector<int> counter(3) ;
for (int i=0; i<3; i ) {
abc[i] = i ;
}
for (int i=0; i<3; i ) {
int a,b,g ;
ifstr >> a >> b >> g ;
a-- ;
b-- ;
g-- ;
swap(abc[a], abc[b]) ;
counter[abc[g]] ;
}
ofstr << std::max(counter[0], counter[1], counter[2]) ;
return 0 ;
I got 4 overloads on my max() function. How do I solve this problem?
CodePudding user response:
The correct way to use std::max
with anything else than 2 arguments is by using an initializer list:
ofstr << std::max( { counter[0], counter[1], counter[2] } );
// ^ ^
Alternatively, you could use std::max_element
which doesn't require a copy of all the elements like the initializer list does. Instead you give it the begin
and end
iterators of your container:
ofstr << *std::max_element(std::begin(counter), std::end(counter));
// ^
Note that it returns an iterator to the found element (or the end
iterator) and it needs to be dereferenced to get the actual value, hence the *
above.
CodePudding user response:
Given you counter, you can do this
ofstr << *std::max_element(counter.begin(), counter.end());
(if you include the alogirhtm header).