I need to have a key with multiple values. What datastructure would you recommend?

前端 未结 7 1302
悲哀的现实
悲哀的现实 2021-02-07 10:53

I have an string array filled with words from a sentence.

words[0] = \"the\"
words[1] = \"dog\"
words[2] = \"jumped\"
words[3] = \"over\"
words[4] = \"the\"
word         


        
7条回答
  •  萌比男神i
    2021-02-07 11:14

    There can be an alternate approach to achieve two values per key, especially for the cases when most of the map elements have two values per key. By pairing two values for a key, as mentioned in this link:

    std::map > myMap2
    

    use it in the function as:

    #include
    #include
    #include
    using namespace std;
    int main(){
    map>mp;
    mp.insert(pair>("ab",make_pair(50,7)));
    mp.insert(pair>("cd",make_pair(51,8)));
    map>::iterator it;
    for(it=mp.begin();it!=mp.end();it++)
        cout<first<<" "<second.first<<" "<second.second<<" ";
    return 0;
    }
    

提交回复
热议问题