C++ in condition

  • A+
Category:Languages

How to check if a value is in a on-the-fly created set. I'm looking for some syntactic sugar, like we have in python

if s in set(['first','second','third','fourth']):     print "It's one of first,second,third,fourth"; 

How can it be done efficiently in C++?

 


How about this:

std::string s = "first"; if(std::set<std::string>{"first","second","third","fourth"}.count(s)>=1){     std::cout << s << " is found" << std::endl; } 

BTW, in C++20 and over I think std::set::contains is more preferable.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: