user3350597
user3350597

Reputation: 465

Filling and removing elements form vector and map in c++

This is my problem:

  1. fill vector and map with random numbers from 1 to 9
  2. remove random numbers of element up to 15 from both of them
  3. and then remove all the elements except those that are the same (keep duplicates)

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...


Full code:

#include "stdafx.h"
#include <iostream>
#include "math.h"
#include <vector>
#include <map>

using namespace std;


void fillVecFunction(vector<int>& vec,int size)
{
    int randomNumber;
    for(int i=0; i<size; i++)
    {
        randomNumber=1+rand()%9;    
        vec.push_back(randomNumber);
    }
}

void printVecFunction(vector<int>& vec)
{
    for(vector<int>::const_iterator i = vec.begin(); i != vec.end(); ++i)
        cout << *i << " ";
    cout<<" DONE PRINTING VECTOR!"<<endl<<endl;
}

void fillMapFunction(map<int,int>& map, int size)
{
        for(int i=0; i<size; i++)
    {
        map[i]=1+rand()%9;
    }
}
void printMapFunction(map<int,int>& map,int size)
{
    for(int i=0; i<size;i++)
    if(map[i]!=0)
        cout<<map[i]<<" ";
    cout<<"DONE PRINTING MAP!"<<endl<<endl;
}
void delElements(map<int,int>& map, vector<int>& vec,int size)
{
    int randomNumToRemove=5+rand()%15;
    cout<<"REMOVING : "<<randomNumToRemove<<" ELEMENTS"<<endl<<endl;

    vec.erase(vec.begin(),vec.begin()+randomNumToRemove);   

    std::map<int,int>::iterator it;
    it=map.begin();
    while(randomNumToRemove)
    {
        map.erase(it++);
        randomNumToRemove--;
    }
}

void fillMapFromVec(vector<int>& vec,int size)
{
    map<int,int> NewMap;

    for(int i=0; i<size; i++)
        NewMap[i]=vec[i];
    printMapFunction(NewMap,size);
}
void keepTheSame(map<int,int>& map,vector<int>& vec,int sizeMap, int sizeVec)
{
    vector<int> tempV,tempM;
    int counterM=0,counterV=0;
    for(int i=0; i<sizeVec;i++){
        for(int j=0; j<sizeMap; j++)
        {

            if(vec[i]==map[j])
            {
                //tempM[counterM]=map[17];
                counterM++;
            }
        }
        if(counterM>counterV)
        {
            tempV[counterV]=vec[i];
            counterV++;
        }

    }

    cout<<counterM<<" "<<counterV;
    //printVecFunction(tempV);
    //fillMapFromVec(tempM,sizeof(tempM)+sizeof(int)*tempM.capacity());
}

int _tmain(int argc, _TCHAR* argv[])
{

    int MAX=20;

    vector<int> vec;
    fillVecFunction(vec,MAX);
    printVecFunction(vec);

    map<int,int> 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;
}

Upvotes: 1

Views: 146

Answers (1)

Barry
Barry

Reputation: 302718

This:

tempV[counterV]=vec[i];
counterV++;

is writing to uninitialised memory. The size of tempV is always zero, hence your subscript issue. When appending to a vector, you need to do:

tempV.push_back(vec[i]);

No counterV necessary.

Upvotes: 2

Related Questions