c++ - Insert data into std::map <std::string, int> mymap from two different data structure and sending it via socket -


i have declared map:

std::map <std::string, int> mymap; 

i want insert 2 values in above map *vit , hit->first , sending , receiving via socket.

my code:

for (std::map < int, std::vector < std::string > >::iterator hit = three_highest.begin(); hit != three_highest.end(); ++hit) {  (std::vector < std::string >::iterator vit = (*hit).second.begin(); vit != (*hit).second.end(); vit++) {         std::cout << hit->first << ":";         std::cout << *vit << "\n";         mymap.insert( std::pair<std::string,int> (*vit,hit->first)); //is correct way        }     } 

//then send via socket

if ((bytecount = send(*csock, mymap ,  sizeof(mymap), 0)) == -1) { // think wrong, can correct it?     fprintf(stderr, "error sending data %d\n", errno);     goto finish;     } 

and @ receiving end how 2 variables back?

std::map <std::string, int> mymap; if((bytecount = recv(hsock, mymap, sizeof(mymap), 0))== -1){   //needs here  // getting mymap->first, mymap->second.          fprintf(stderr, "error receiving data %d\n", errno);         } 

like said in comment, kind of data structure containing pointers, file/socket handles , similar, can't sent on network, or saved file. @ least not without kind of marshalling or serialization.

in case can half simple. first thing need send size of map, i.e. number of entries in it. helps in recreating map on receiving side.

then can first send keys, followed values. sending values simple, put them in std::vector , use e.g. std::vector::data pointer actual data. sending keys little more complicated (since may have different lengths). key, can either make array of fixed-size arrays big enough fit key strings, , send that. or can send each key 1 one receiving side checking string terminator. or send strings 1 one, length of string first followed actual string.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -