jeudi 28 janvier 2016

Performance hit from using std::find

I have the following code to check whether a value belongs to a list of values. eg: contains({1,2,3},3). Almost always, I could write a bunch of if-elses. How much performance hit does the contains approach create? Is there a way to avoid this?

template<typename T1,typename T2>
std::enable_if_t<std::is_same<std::initializer_list<T2>,T1>::value, bool> contains(const T1 &container,const T2 &item)
{
    return(std::find(container.begin(),container.end(),item)!=container.end());
}

Aucun commentaire:

Enregistrer un commentaire