Hema Chandra
Hema Chandra

Reputation: 383

Deleting an element from Vector in C++

I have a "map" that stores "int" as key and "Student" class object as value. After storing data into the map (group of maps : each map contain one key and one student class object as value) I need to store those group of maps into a vector. I have also done them. But when I try to delete a map from a particular position from the vector only the last/end map in the vector is getting deleted. I want to delete the map based on index.

My Program:

#include <map>  
#include <iostream>  
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class Student
{

public:
    string name;
    int id;

void  setName(string nam)
{
        name = nam;
}
void setId(int i)
{
        id = i;
}
string  getName()
{
       return name;
}
int  getId()
{
       return id;
}
};


vector<map<int,Student>> coll;
typedef std::map<int,Student> Mymap;
Mymap c1;
map<int, Student> mine(int key,Student value)
{   

    c1.insert(Mymap::value_type( key,value));   
    return c1;   
}   

void print(Mymap a)
{
coll.push_back(a);
}

int main()
{
     typedef  map<int, Student> map1;  
     Student s[5];
     Student s1;
     map1 m1;
     int i=0;
     s[0].setName("SAI");
     s[1].setName("RAVI");
     s[2].setName("RAJU");
     s[3].setName("HemaChandra");
     s[4].setName("Tushar");

     s[0].setId(10);
     s[1].setId(20);
     s[2].setId(30);
     s[3].setId(40);
     s[4].setId(50);

     for(int j=0;j<5;j++)
     {
      m1 =  mine(j,s[j]);
      print(m1);
     }

      cout<<endl<<endl;

      cout<<"Before Deleting size  = "<<coll.size()<<endl;

        map1 m2;
        Student st ;
        std::vector<map<int,Student>>::const_iterator k;

    for(k=coll.begin(); k!=coll.end(); ++k)
    {
        m2 = coll[i];
        st = m2.at(i);
        std::cout <<" "<< st.getName()<<std::endl;
        i++;
    }

    coll.erase(coll.begin() + 2);    //Should delete 3 element but not deleting

    cout<<"\nAfter Deleting :\n"<<endl;
    i=0;
     for(k=coll.begin(); k!=coll.end(); ++k)
    {
        m2 = coll[i];
        st = m2.at(i);
        std::cout <<" "<< st.getName()<<std::endl;
        i++;
    }
    /*
     coll.erase(std::remove(coll.begin(), coll.end(), m1), coll.end()); 
     This one also not working
    */
    return 0;
}

When I try to display the elements I can display the first 4 elements but not the last element.Actually the program should remove the 3 element from the vector(That is 3rd map) and display all other maps: 1,2,4,5 data. Please anyone help me in deleting the map from a particular position from the map.

Upvotes: 1

Views: 952

Answers (2)

Marco A.
Marco A.

Reputation: 43662

The code is working fine, not sure what you would expect but this code

for (int j = 0; j<5; j++) {
    m1 = mine(j, s[j]);
    print(m1);
}

can be simplified as:

Student s[5];
s[0].setName("SAI");
s[1].setName("RAVI");
s[2].setName("RAJU");
s[3].setName("HemaChandra");
s[4].setName("Tushar");

vector<map<int, Student>> coll;
map<int, Student> m1;
for(int i = 0; i < 5; ++i) {
   m1.emplace(i, s[i]);
   coll.push_back(m1); // You're push_back'ing a copy of the map each time,
                       // and each time it will have a new element in it
}

so your line

coll.erase(coll.begin() + 2);

actually erases just the third map from the vector:

vector {
    0: map { SAI };
    1: map { SAI, RAVI };
    2: map { SAI, RAVI, RAJU }; <- this is deleted
    3: map { SAI, RAVI, RAJU, HemaChandra };
    4: map { SAI, RAVI, RAJU, HemaChandra, Tushar };
}

Printing the elements with indices 0, 1, 2, 3 will yield SAI, RAVI, RAJU, HemaCHandra. If you instead print the last ones as

  cout << "\nAfter Deleting :\n" << endl;
  for (k = coll.begin(); k != coll.end(); ++k)
  {
    st = k->rbegin()->second; // Get the last one in each map
    std::cout << " " << st.getName() << std::endl;
  }

you will get SAI, RAVI, HemaChandra, Tushar.

Upvotes: 1

alexeykuzmin0
alexeykuzmin0

Reputation: 6440

Everything is deleted just fine, you can check it in the debugger.

The problem is that in the line

st = m2.at(i);

in the second printing loop you access the elements 1, 2, 3, 4 of the maps you're storing, not the elements 1, 2, 4, 5 as you expect. This code can be fixed by changing this line to

st = m2.rbegin()->second;

Upvotes: 0

Related Questions