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.
kanywhere? - Caleth