Search code examples
c++cvectormultimap

Return content of multimap


Here I have used multimap and at bottom part printing the content of it.

Generally I work with array only and return and fetch content of array easily.

like:

void main(){
char *ch;
ch=client(); //function call 
//Now we can get ch[0]...
}

char function client()
{
char ar[2]
....
return ar;
}

Can I work in similar manner for multimap? Because I want to return string and int value at same moment. And working with socket programming so it would use send and recv() method to send and receive.

std::multimap<int,std::string>::iterator it = dst.begin();
for(int count = 0;count<3 && it !=dst.end();++it,++count)
   std::cout<<it->second<<":"<<it->first<<std::endl;

In this code I want send it->second and it->first. What would be correct way?


Solution

  • Yes that will work, but prefer using const ierator. As const suggest (at least in recent compilers and libraries) also thread safe. So prefer :

    std::multimap<int,std::string>::const_iterator it = dst.cbegin();
    for(int count = 0;count<3 && it !=dst.cend();++it,++count)
       std::cout<<it->second<<":"<<it->first<<std::endl;