title: Filling and removing elements form vector and map in c++
body: <h2>This is my problem:</h2>
  
  <ol>
  <li>fill vector and map with random numbers from 1 to 9</li>
  <li>remove random numbers of element up to 15 from both of them</li>
  <li>and then remove all the elements except those that are the same (keep duplicates)</li>
  </ol>
  
  <p>More or less all of the functions are doing their job except void keepTheSame(). I am getting vector subscript out of range error. I know what I am doing wrong but unable to fix it. 
  If someone can help please do. Still in learning process... </p>
  
  <hr>
  
  <p><strong>Full code:</strong></p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include "math.h"
  #include &lt;vector&gt;
  #include &lt;map&gt;
  
  using namespace std;
  
  
  void fillVecFunction(vector&lt;int&gt;&amp; vec,int size)
  {
      int randomNumber;
      for(int i=0; i&lt;size; i++)
      {
          randomNumber=1+rand()%9;    
          vec.push_back(randomNumber);
      }
  }
  
  void printVecFunction(vector&lt;int&gt;&amp; vec)
  {
      for(vector&lt;int&gt;::const_iterator i = vec.begin(); i != vec.end(); ++i)
          cout &lt;&lt; *i &lt;&lt; " ";
      cout&lt;&lt;" DONE PRINTING VECTOR!"&lt;&lt;endl&lt;&lt;endl;
  }
  
  void fillMapFunction(map&lt;int,int&gt;&amp; map, int size)
  {
          for(int i=0; i&lt;size; i++)
      {
          map[i]=1+rand()%9;
      }
  }
  void printMapFunction(map&lt;int,int&gt;&amp; map,int size)
  {
      for(int i=0; i&lt;size;i++)
      if(map[i]!=0)
          cout&lt;&lt;map[i]&lt;&lt;" ";
      cout&lt;&lt;"DONE PRINTING MAP!"&lt;&lt;endl&lt;&lt;endl;
  }
  void delElements(map&lt;int,int&gt;&amp; map, vector&lt;int&gt;&amp; vec,int size)
  {
      int randomNumToRemove=5+rand()%15;
      cout&lt;&lt;"REMOVING : "&lt;&lt;randomNumToRemove&lt;&lt;" ELEMENTS"&lt;&lt;endl&lt;&lt;endl;
  
      vec.erase(vec.begin(),vec.begin()+randomNumToRemove);   
  
      std::map&lt;int,int&gt;::iterator it;
      it=map.begin();
      while(randomNumToRemove)
      {
          map.erase(it++);
          randomNumToRemove--;
      }
  }
  
  void fillMapFromVec(vector&lt;int&gt;&amp; vec,int size)
  {
      map&lt;int,int&gt; NewMap;
  
      for(int i=0; i&lt;size; i++)
          NewMap[i]=vec[i];
      printMapFunction(NewMap,size);
  }
  void keepTheSame(map&lt;int,int&gt;&amp; map,vector&lt;int&gt;&amp; vec,int sizeMap, int sizeVec)
  {
      vector&lt;int&gt; tempV,tempM;
      int counterM=0,counterV=0;
      for(int i=0; i&lt;sizeVec;i++){
          for(int j=0; j&lt;sizeMap; j++)
          {
  
              if(vec[i]==map[j])
              {
                  //tempM[counterM]=map[17];
                  counterM++;
              }
          }
          if(counterM&gt;counterV)
          {
              tempV[counterV]=vec[i];
              counterV++;
          }
  
      }
  
      cout&lt;&lt;counterM&lt;&lt;" "&lt;&lt;counterV;
      //printVecFunction(tempV);
      //fillMapFromVec(tempM,sizeof(tempM)+sizeof(int)*tempM.capacity());
  }
  
  int _tmain(int argc, _TCHAR* argv[])
  {
  
      int MAX=20;
  
      vector&lt;int&gt; vec;
      fillVecFunction(vec,MAX);
      printVecFunction(vec);
  
      map&lt;int,int&gt; map;
      fillMapFunction(map, MAX);
      printMapFunction(map,MAX);
  
      delElements(map,vec,MAX);
      printVecFunction(vec);
      printMapFunction(map,MAX);
  
      keepTheSame(map,vec,map.size(),vec.size());
  
  
      system("PAUSE");
      return 0;
  }
  </code></pre>