Click here to Skip to main content
15,902,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I'm doing c++ with network simulator(ns2)but I'm totally new in c++.
I want to read data for each node and keep in map.I didn't use array as all nodes will not be read for that time;the size of data is not known.

I give a command like this
C++
void Update(){
		int q=0.5;
		int NodeId=ra_addr ;
		CurNode= (MobileNode*) (Node::get_node_by_address(ra_addr_));
		map_table(NodeId,iEnergy );
		}

void map_table(int node_id,double Eg){
	map<int, double > mymap;
	map<int, double > ::iterator it;
	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 ii=mymap.begin(); ii!=mymap.end(); ii++)
	   {
	       cout << (*ii).first << ": " << (*ii).second << endl;
	   }



when i print,it contain only one node_id,not all.My idea is to check id if it's in the map,update a new data,Eg.i try to declare map in header file also give the same result.

the result should be like
node || data
1 3.45
2 4.25
6 1.23


thank you in advance
pare
Posted
Updated 28-Mar-12 1:32am
v2
Comments
Fredrik Bornander 29-Mar-12 6:03am    
If declaring the mymap outside doesn't work then you've done it incorrectly. See my update example.

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;

C++
#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
 
Share this answer
 
v2
thanks for ur reply.I did try as you told and got result like this

CSS
print data in map
0: 190.809
1: 91.8829
4: 58.4821
 print data in map
1: 91.8829
2: 2.40965
3: 276.987
4: 58.4821
 print data in map
0: 190.809
2: 2.40965
3: 276.987
4: 58.4821
 print data in map
0: 190.809
1: 91.8829
3: 276.987


seems like some id is missing.
 
Share this answer
 
v2
Please share the code where you are referencing this map , I guess you might be deleting the enrties from this map in some other flow, depending on your network simulation of nodes as the topology changes.
 
Share this answer
 
Here's the related code.

olsrm.h

C++
#include<mobilenode.h>
#include <stdio.h>
#include <map>
#include <algorithm>
class OLSRM : public Agent {

nsaddr_t ra_addr_;
double   reserved_;
protected:
double   iEnergy;
double 	 Cur_tx;
double   E_cost;
int 	 NodeId;
MobileNode  *CurNode;

inline double&	reserved(){ return reserved_; }
void	process_hello(OLSRM_msg&, nsaddr_t, nsaddr_t);
void    update_Energy(OLSRM_msg& );
void 	map_table(int ,double );
public:
	OLSRM(nsaddr_t);
	static int node_id(nsaddr_t);
     	std::map<int, double > mymap;
};


The cc code is called from tcl script

olsrm.cc

C++
void	OLSRM::process_hello(OLSRM_msg& msg, nsaddr_t receiver_iface, nsaddr_t sender_iface) {
	assert(msg.msg_type() == OLSRM_HELLO_MSG);
	update_Energy(msg);
	link_sensing(msg, receiver_iface, sender_iface);
	..
	
	}

void	OLSRM::update_Energy(OLSRM_msg& msg){
	int q=0.5;
	NodeId=OLSRM::node_id(msg.orig_addr_) ;
	CurNode = (MobileNode*) (Node::get_node_by_address(OLSRM::node_id(msg.orig_addr_)));
	if(CurNode==NULL)
	{ printf("Error: Mobile node object points to NULL\n");
	  exit(1);
	}

	Cur_tx = CurNode->energy_model()->et(); //link with model reading an energy value
	iEnergy = CurNode->energy_model()->energy();
	E_cost = ((q*Cur_tx )+((1-q)/iEnergy))*100;
	msg.hello().reserved()=E_cost;
	map_table(NodeId,E_cost);
        }
void OLSRM::map_table(int node_id,double Eg){
	map<int,>::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,>::iterator i = mymap.begin(); i != mymap.end(); ++i)
		{
		cout << (*i).first << ": " << (*i).second << endl;
		}
	}

int
	OLSRM::node_id(nsaddr_t addr) {
	// Preventing a bad use for this function
	if ((u_int32_t)addr == IP_BROADCAST)
	return addr;
	// Getting node id
	Node* node = Node::get_node_by_address(addr);
	assert(node != NULL);
	return node->nodeid();
	}


thank you
 
Share this answer
 
v2
Please check in your code , whether you are doing any mymap.erase()

search for mymap.erase in your files.


Regards,
Raina
 
Share this answer
 
I've already check but nothing in the file i erase().However,I'm not sure is it related to the type or not.I want to keep for double or float(64,32 bits) value but in the field that keep this value contain only 16 bits(packet structure).Is there any method i can keep this type of value?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900