So, in python it is often useful to do this:
v = [4,5,2,7,5]
s = set(v)
if 4 in v :
print "YESS!!"
By doing set(v) you create a set from the given list, which you can then use to do 4 in v.
I was practicing topcoder's SRM 426, and once again I got myself into a common theme: You have a vector<int> cardsReceived, does it contain int x?.
The form I would have used until today: (find(cardsReceived.begin(), cardsReceived.end(),x) != cardsReceived.end() ) is excessively ugly . But somehow it occurred to me to just try std::set's constructors:
set<int> rec( cardsReceived.begin(), cardsReceived.end() );
// ...
if( recset.count(x) ) {
cout<<"YESSS"!"<<endl;
}
That's right, do not underestimate STL's constructors. Since this constructor uses generic iterators, you can use it for arrays as well:
int A[6] = {2,3,2,2,4,5};
setrec( A, A+6);
Do not forget also that constructors may be used as functions:
int A[6] = {2,3,2,2,4,5};
if( set( A, A+6).count(4) ) {
cout<<"YEss!!"<<endl;
}
Off-topic: I once again got into the classical issue with using HTML for simple communication, thanks to HTML it is very difficult to use < and you have to always remember to use <, that is very dumb, I will begin to look forward better wash to write entries for blogger.