In this below code, instead of seeing output as
0 -> 1 2
1 -> 2 3
..
..
99 ->100 101
I am getting output as,
0 -> 100 101
1 -> 100 101
...
99 -> 100 101
Please help me how to resolve this problem, where exactly it is going wrong? While debugging I found, in first iteration it stores
0 -> 1 2
2nd iteration it it updates like,
0 -> 2 3
1 -> 2 3
Why?
class abc{
public:
int x, y;
};
std::map<int, abc*> MAP;
int main()
{
abc *ab;
ab = new abc();
int i = 0;
for(i = 0; i < 100; i++)
{
ab->x = i + 1;
ab->y = i + 2;
MAP.insert(std::pair<int, abc*>(i, ab));
}
map<int, abc*>::iterator it;
for(it = MAP.begin(); it != MAP.end(); it++)
{
cout << it->first << "->" << it->second->x <<" "<< it->second->y << endl;
}
system("pause");
return 0;
}