c++: replace multiple index equality tests by a single function call -


in c++/c++11 proper way replace multiple comparison of form:

if(isindextofind==index1 || isindextofind==index2 ...) 

with less messy of form:

if(isin(indextofind,index1,index2,...)) 

for varying number of parameters index1, index2, ... code belongs numerical have efficient direct comparison.

maybe interesting add index1, index2 const static values maybe variadic template based solution of interest ?

you can write like

#include <iostream> #include <algorithm> #include <initializer_list>  template <class t> bool one_of( const t &value, std::initializer_list<t> lst ) {     return std::any_of( lst.begin(), lst.end(), [&]( const t &x ) { return value == x; } ); }      int main() {     std::cout << one_of( 1, { 2, 3, 1, 5 } ) << std::endl;     std::cout << one_of( 4, { 2, 3, 1, 5 } ) << std::endl; }     

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -