title: Howto Create Map of Vector From Sorted Data
body: <p>I have the following data as input (sorted by first column):</p>
  
  <pre><code>foo 1 2
  foo 3 3
  bar 10 11
  </code></pre>
  
  <p>I want to create a Map of Vector with first column as key of the map
  such that we have:</p>
  
  <pre><code>foo = {1,2,3,3}
  bar = {10,11}
  </code></pre>
  
  <p>But why my code below doesn't work as expected?</p>
  
  <pre><code>    #include &lt;vector&gt;
      #include &lt;map&gt;
      #include &lt;iostream&gt;
      #include &lt;fstream&gt;
      #include &lt;sstream&gt;
  
      using namespace std;
  
      int main  ( int arg_count, char *arg_vec[] ) {
          if (arg_count !=2 ) {
              cerr &lt;&lt; "expected one argument" &lt;&lt; endl;
              return EXIT_FAILURE;      
          }
  
          string line;         
          ifstream acemblyfile (arg_vec[1]); 
  
          map &lt;string, vector&lt;int&gt; &gt; myMapOfVec; 
          vector &lt;string&gt; myVec;  
          string KEY = "" ;    
  
  
          if (acemblyfile.is_open())
          {
              while (getline(acemblyfile,line) )
              {
                  stringstream ss(line);    
                  string KEY_TEMP;
                  int VAL1;
                  int VAL2;           
  
                  ss &gt;&gt; KEY_TEMP &gt;&gt; VAL1 &gt;&gt; VAL2;
  
                  MyVec.push_back(VAL1);   
                  MyVec.push_back(VAL2);   
  
  
                  if (KEY_TEMP != KEY) {     
                    myMapOfVec[KEY] = MyVec;
                    KEY = KEY_TEMP;            
                    MyVec.clear();          
                  }
  
              }
              acemblyfile.close();      
          }
          else {
              cout &lt;&lt; "Unable to open file"; 
          }
  
  
  for( map&lt;string, vector&lt;int&gt; &gt;::iterator iter = myMapOfVec.begin(); iter != myMapOfVec.end(); ++iter ) {
         vector &lt;int&gt; tempVec = (*iter).second;
         string Key = (*iter).first;
         for (unsigned i =0; i&lt;tempVec.size(); i++) {
            cout &lt;&lt; Key &lt;&lt; " " &lt;&lt; tempVec[i] &lt;&lt; endl;
         }
      }
  
          return 0;
      }
  </code></pre>