title: Iterate through STL sequence and associative containers using same code?
body: <p>Let's say I'd like to write an algorithm that prints the value of each element in a container. The container could be a <code>Sequence</code> or <code>Associative</code> container (e.g. <code>std::vector</code> or <code>std::map</code>). In the case of a sequence, the algorithm would print the <code>value_type</code>. In the case of an associative type, the algorithm would print the <code>data_type</code>. How can I write my algorithm (<strong>only once!</strong>) so that it works with either one? Pretend that the algorithm is complex and that I don't want to repeat it for both sequence/associative versions.</p>
  
  <p>For example:</p>
  
  <pre><code>template &lt;class Iterator&gt;
  void printSequence(Iterator begin, Iterator end)
  {
      for (Iterator it=begin; it!=end; ++it)
          std::cout &lt;&lt; *it;
  }
  
  template &lt;class Iterator&gt;
  void printAssociative(Iterator begin, Iterator end)
  {
      for (Iterator it=begin; it!=end; ++it)
          std::cout &lt;&lt; it-&gt;second;
  }
  
  template &lt;class Iterator&gt;
  void printEither(Iterator begin, Iterator end)
  {
      // ????
  }
  </code></pre>