title: Incorporating boilerplate vector<> code in C++
body: <p>There are certain functions I use in manipulating <code>vector&lt;T&gt;</code> that come up a lot but for which the standard interface is clunky.</p>
  
  <p>For example, suppose <code>v</code> is if of type <code>vector&lt;T&gt;</code> for a <code>typename T</code>. Ideally I would like to make calls like:</p>
  
  <pre><code> v.probe(x) //returns true if x is in v
   v.sort() // sort v
   v.unique() // unique elements of x
   v.locate(x) // pointer to the element in v equal to x if it exists, otherwise NULL
   v.cat(w) // concatenate vector w to x
   v.erase(x) // erase all x’s from v
  </code></pre>
  
  <p>and so on.</p>
  
  <p>These can all be done in stl, but the interface is clunky and wordy. For example, <code>v.probe(x)</code> would be something like </p>
  
  <pre><code> std::find(v.begin(),v.end(),x)!=v.end()
  </code></pre>
  
  <p>and <code>v.sort</code> is</p>
  
  <pre><code>std::sort(v.begin(),v.end())
  </code></pre>
  
  <p>which makes <code>std::sort</code> very awkward to use in the case of complex lvalue expressions, requiring a temporary. (I.e., I cannot easily sort <code>foo-&gt;bar.names[3]</code> without a temporary.</p>
  
  <p>Getting unique values of v in STL is even more ridiculously clunky, requiring, I believe:</p>
  
  <pre><code>std::erase(std::unique(std::sort(v.begin(),v.end()).end(),v.end())
  </code></pre>
  
  <p>I assume that virtually every C++ programmer has run into this issue or issues like them.</p>
  
  <p>What is the best way around this? </p>
  
  <p>I have considered 3 options:</p>
  
  <p>Write special purpose code for each type of <code>vector&lt;&gt;</code> I use in the code.</p>
  
  <p>Write a template header for common vector functions</p>
  
  <p>Have a personal <code>vector&lt;T&gt;</code> class <code>K&lt;T&gt;</code> that subclasses both <code>vector&lt;T&gt;</code> and a mixin class <code>algorithm_vector&lt;T&gt;</code> with the algorithms I need.</p>
  
  <p>Option 1 seems simple but gets very wordy after a while.
  Option 2 is not as simple as it seems. Consider writing a special function probe, like</p>
  
  <pre><code>    template&lt;typename T&gt; probe(const vector&lt;T&gt; &amp; v, const T &amp;x)....
  </code></pre>
  
  <p>Well, the thing is that we actually only want to pass in <code>x</code> by reference if the size of <code>T</code> is large, otherwise we want to use value. I don’t even know how to write a template function that intelligently decides whether to pass its argument by value or reference, and, even if I did, it sounds hard to do.</p>
  
  <p>Option 3 is probably the cleanest, but has semantic issues that make it unclear.</p>
  
  <p>In conclusion, my question this: what is the best way to add common, simple generic functions on vectors to a program?</p>
  
  <p>(Also, as an optional point which might shed some insight into this, I don’t understand why STL makes it so wordy and awkward to do common things like search a vector for an element, or sort a vector. Is there some reason that STL make the most common usages so wordy, and doesn’t overload to default on the whole container?)</p>