title: Checking if all elements of a vector are equal in C++
body: <p>If I have a vector of values and want to check that they are all the same, what is the best way to do this in C++ efficiently? If I were programming in some other language like R one way my minds jumps to is to return only the unique elements of the container and then if the length of the unique elements is more than 1, I know all the elements cannot be the same. In C++ this can be done like this:</p>
  <pre><code>//build an int vector
  std::sort(myvector.begin(), myvector.end());
  std::vector&lt;int&gt;::iterator it;
  //Use unique algorithm to get the unique values.
  it = std::unique(myvector.begin(), myvector.end());
  positions.resize(std::distance(myvector.begin(),it));
  if (myvector.size() &gt; 1) {
      std::cout &lt;&lt; &quot;All elements are not the same!&quot; &lt;&lt; std::endl;
  }
  </code></pre>
  <p>However reading on the internet and SO, I see other answers such using a <code>set</code> or the <code>find_if</code> algorithm. So what is the most efficient way of doing this and why? I imagine mine is not the best way since it involves sorting every element and then a resizing of the vector - but maybe I'm wrong.</p>