Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if all elements of a vector are equal

Tags:

c++

I want to test if a non-empty vector contains identical elements. Is this the best way?

count(vecSamples.begin()+1, vecSamples.end(), vecSamples.front()) == vecSamples.size()-1;
like image 469
Neil Kirk Avatar asked Mar 20 '13 17:03

Neil Kirk


People also ask

How do you compare elements in vectors?

Comparing two vectors using operator == std::vector provides an equality comparison operator==, it can be used to compare the contents of two vectors. For each element in the vector it will call operator == on the elements for comparisons.

How do I see all values in a vector in R?

R – all() function all() function in R Language will check in a vector whether all the values are true or not.

How do you check if values are the same in R?

setequal() function in R Language is used to check if two objects are equal. This function takes two objects like Vectors, dataframes, etc. as arguments and results in TRUE or FALSE, if the Objects are equal or not.

How do you compare two vectors that are equal?

For two vectors to be equal, they must have both the magnitude and the directions equal.


1 Answers

In c++11 (or Boost Algorithm)

std::all_of(vecSamples.begin()+1,vecSamples.end(),
          [&](const T & r) {return r==vecSamples.front();})
like image 130
sbabbi Avatar answered Sep 21 '22 05:09

sbabbi