The problem is that
mymap
is local to the
map_table
method. So every time you add something to it it's going to be the first one.
Make sure
mymap
is declared outside of the method, or is being passed in to the method.
Something like this might work;
#include <iostream>
#include <map>
using namespace std;
map<int, double> mymap;
void map_table(int node_id, double Eg)
{
map<int, double>::iterator it = mymap.find(node_id);
if(it == mymap.end())
mymap.insert(map<int,double>::value_type(node_id,Eg));
else
mymap[node_id]=Eg;
for(map<int, double>::iterator i = mymap.begin(); i != mymap.end(); ++i)
{
cout << (*i).first << ": " << (*i).second << endl;
}
}
int main()
{
map_table(1, 40.0);
cout << "---------------------------" << endl;
map_table(2, 42.0);
cout << "---------------------------" << endl;
map_table(1, 43.0);
cout << "---------------------------" << endl;
map_table(3, 40.0);
return 0;
}
That prints:
1: 40
---------------------------
1: 40
2: 42
---------------------------
1: 43
2: 42
---------------------------
1: 43
2: 42
3: 40
Hope this helps,
Fredrik