title: Overload operator for both std::vector and std::list
body: <p>I want to overload <code>operator&lt;&lt;</code> for both <code>std::list</code> and <code>std::vector</code> with the following code. But the two functions are almost the same. Is there any way to combine them, <em>i.e.</em>, create a more generic overload?</p>
  
  <pre><code>#include &lt;iterator&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;list&gt;
  
  template &lt;typename T&gt;
  std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, const std::vector&lt;T&gt; &amp;v)
  {
    if (!v.empty())
      std::copy(v.begin(), v.end(), std::ostream_iterator&lt;T&gt;(out, ", "));
    return out;
  }
  
  template &lt;typename T&gt;
  std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, const std::list&lt;T&gt; &amp;v)
  {
    if (!v.empty())
      std::copy(v.begin(), v.end(), std::ostream_iterator&lt;T&gt;(out, ", "));
    return out;
  }
  
  int main()
  {
    std::cout &lt;&lt; std::vector&lt;int&gt;({1, 2, 3, 4}) &lt;&lt; std::endl;
    std::cout &lt;&lt; std::list&lt;int&gt;({1, 2, 3, 4}) &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>