I've developing a socket-based network piece of software as hw. I ran into some problems using STL's map and vector.
What my program does is keeping a map <socket_id, vector <message *> > allMsg of messages directly bound to their specific socket. Everything work great, except when I receive a message and try to put it at the end of the vector, like this:
m = new message ();
... set m's data from the bytes received ...
allMsg [c].push_back (m)
Not working!
I've tried to get a local copy of the vector, like:
vector <message *> msg_queue = allMsg [c];
m = new message ();
... set m's data ...
msg_queue.push_back (m);
... now since msg_queue is local, copy it, member by member, to the allMsg [c]
It seems like it *should* work. What kind of error do you get? (compile- or runtime? If it is less than 2 pages, it would be good to post the exact message...)
Looks like the first method should work. If socket c doesn't exist in the map though you could have problems, try printing out the vector you get from allMsg[c] and make sure it contains data.
Examining existing IPs for this station: ...
Looking for the first IP v4 match... bound to 192.168.0.83
Starting server on 192.168.0.83 : 8800 ... server started
fromConnections.size () = 1 // I have connected to this ip from other host, so we have a vector with 1 entry
fromMessages.size () = 1 //same, i have a single entry in the map, altough the vector is empty, no messages yet
New message from 192.168.0.83 I've send a message, the code works for the first one
Message queue after incoming message: 1
Message queue:
Client -> #msg: 1
Running command: xxx
fromConnections.size () = 2 //Proof, now i'm connected from 2 hosts, you can also see below
fromMessages.size () = 2 // here
New message from 192.168.0.83 //And of course, the message from second host is intercepted, but i'm unable to save it anymore in the map
The weird this is that the map has 2 entries, meaning the keys are different (which is right, no overwrites). 2 vectors are inited, I just can't write inside them!! :))
Me Out
I've solved the problem guys... the problem was related to the fact that every = or * operation on the map (like (*iter).first) or vector <message> v = fromMessages [c], made a local copy no matter how i referenced the map.
The solution was to use the iterator directly, like:
(*i).second.clear () -> corect
(*i).second.push_back (x) -> corect
everything else got me into a dead end so ... thanks everybody for support :)