Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Collections : how to create a map like structure

What kind of collection method would store a pair (key and value) where the key isn't unique (which technically doesn't make it a key I think)?

Somewhere in my program I have:

typedef struct 
{
    int nKey;
    string strFormType;
} KeyPair;

Then I'll store objects in a vector using this struct.

 vector<KeyPair> vKeyList;
 KeyPair MenuOne;
 MenuOne.nKey = 1;
 MenuOne.strFormType = "Window";
 vKeyList.push_back(MenuOne);       

 MenuOne.nKey = 0;
 MenuOne.strFormType = "Window2";
 vKeyList.push_back(MenuOne);  

 MenuOne.nKey = 1;
 MenuOne.strFormType = "WindowC";
 vKeyList.push_back(MenuOne);      

This is basically how I want to store objects in the vector. My problem is, If I'm to store like a hundred KeyPairs, I should be doing it in a loop and just read off the KeyPairs from a storage then push_back it the the vector.

What if I have to store these KeyPairs:

 KEY WINDOW
 1   Window 
 0   Window2
 1   WindowC
 3   Windowfoo
 1   Window
 and so on... 

I couldn't store it in a map because you have to have a unique key. The keys of the KeyPairs I have aren't unique. Any suggestion?

like image 432
Owen Avatar asked Dec 22 '22 20:12

Owen


1 Answers

multimap<>

like image 156
Ignacio Vazquez-Abrams Avatar answered Dec 28 '22 05:12

Ignacio Vazquez-Abrams