Click here to Skip to main content
16,020,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Wanted to be able to put multiple items into my set which is in my map after I realised I had got the project wrong.

The code I have is
map<string,set<string> > token;

int main()
{
label = "s1";
token[label].insert("orange", "pears");

return 0;
}


Result I want in the map should be the first string is "s1" and the set should contain "orange" and "pears"


Prompt reply would be ideal as deadline is scary close
Posted
Updated 11-Jan-11 3:41am
v2

You probably want to use a multimap[^]

Update:
using namespace std;

typedef map< string , set < string > > tokenmap;

tokenmap token;

int _tmain(int argc, _TCHAR* argv[])
{
    string label("s1");
    set < string > stringSet;
    stringSet.insert("orange");
    stringSet.insert("pears");
    token.insert(tokenmap::value_type(label,stringSet));
    return 0;
}



Regards
Espen Harlinn
 
Share this answer
 
v3
Comments
Budlee 11-Jan-11 9:21am    
I know it is possible without a multimap and would ideally not want to use one


Thanks for the answer that will do the job perfectly and also the prompt reply is IDEAL

Many Thanks Coding project and especially Espen for saving me again :D
Sergey Alexandrovich Kryukov 11-Jan-11 10:34am    
Good answer - a 5.
Espen Harlinn 11-Jan-11 10:59am    
Thanks SAKryukov!
Well map is a collection of key,value pair. Since your value is "orange", "pears" that is what you will get.

a. You can insert your key part of your value insert
b. when you fetch your value, append to it your key as well.
c. Extend map to have that functionality for you.

In short, the map will not do it for you.

just my 2cent.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-11 10:32am    
That's right - a 5.

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