title: C++ best way to copy all elements from vector into map / unordered_map
body: <p>Using C++, if I want to convert a <code>vector</code> into <code>set</code> or <code>unordered_set</code> container, it can be easily done by:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;set&gt;
  #include &lt;unordered_map&gt;
  #include &lt;unordered_set&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  int main() {
      vector&lt;int&gt; vec {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
  
      // pass
      unordered_set&lt;int&gt; uSet(vec.begin(), vec.end());
      // pass
      set&lt;int&gt; s(vec.begin(), vec.end());
      // fail
      unordered_map&lt;int, size_t&gt; uMap(vec.begin(), vec.end());
      // fail
      map&lt;int, size_t&gt; m(vec.begin(), vec.end());
  
      return 0;
  }
  </code></pre>
  
  <p>However, the same technique doesn't work for <code>map</code> or <code>unordered_map</code> containers. I'm wondering if there is a better way to store all the elements from a vector into <code>map</code> / <code>unordered_map</code> container other than:</p>
  
  <pre><code>for (int ele : vec) {
      ++uMap[ele];
  }
  </code></pre>
  
  <p>Also, which copy constructor from <a href="https://en.cppreference.com/w/cpp/container/unordered_set/unordered_set" rel="nofollow noreferrer">https://en.cppreference.com/w/cpp/container/unordered_set/unordered_set</a> is called for the code below:</p>
  
  <pre><code>set&lt;int&gt; s(vec.begin(), vec.end()); 
  </code></pre>
  
  <p>And why a similar copy constructor from <a href="https://en.cppreference.com/w/cpp/container/unordered_map/unordered_map" rel="nofollow noreferrer">https://en.cppreference.com/w/cpp/container/unordered_map/unordered_map</a> is not available?</p>