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

前端 未结 7 1133
悲哀的现实
悲哀的现实 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条回答
  • 2021-02-07 11:07

    std::multimap

    The link provides an excellent example. Quoted below:

     int main()
    {
      multimap<const char*, int, ltstr> m;
    
      m.insert(pair<const char* const, int>("a", 1));
      m.insert(pair<const char* const, int>("c", 2));
      m.insert(pair<const char* const, int>("b", 3));
      m.insert(pair<const char* const, int>("b", 4));
      m.insert(pair<const char* const, int>("a", 5));
      m.insert(pair<const char* const, int>("b", 6));
    
      cout << "Number of elements with key a: " << m.count("a") << endl;
      cout << "Number of elements with key b: " << m.count("b") << endl;
      cout << "Number of elements with key c: " << m.count("c") << endl;
    
      cout << "Elements in m: " << endl;
      for (multimap<const char*, int, ltstr>::iterator it = m.begin();
           it != m.end();
           ++it)
       cout << "  [" << (*it).first << ", " << (*it).second << "]" << endl;
    }
    
    0 讨论(0)
  • 2021-02-07 11:11

    As two others pointed out, std::multimap can be your solution.

    Also consider std::tr1::unordered_multimap. It is available in VS 2008 seems to have it, GCC has it at least from version 4.3.

    0 讨论(0)
  • 2021-02-07 11:13

    you can use a multimap from the STL and use the call

    pair<iterator, iterator> equal_range(const key_type& k)
    

    to get a range of iterators that match your key

    personally i find this slightly clunky due to having to deal with iterator ranges rather than just getting an object back that represents all values for that key. to get around that you could also store a vector in a regular map and add your strings to the vector.

    0 讨论(0)
  • 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<std::string, std::pair<std::int, int> > myMap2
    

    use it in the function as:

    #include<iostream>
    #include<map>
    #include<iterator>
    using namespace std;
    int main(){
    map<string,pair<int,int>>mp;
    mp.insert(pair<string,pair<int,int>>("ab",make_pair(50,7)));
    mp.insert(pair<string,pair<int,int>>("cd",make_pair(51,8)));
    map<string,pair<int,int>>::iterator it;
    for(it=mp.begin();it!=mp.end();it++)
        cout<<it->first<<" "<<it->second.first<<" "<<it->second.second<<" ";
    return 0;
    }
    
    0 讨论(0)
  • 2021-02-07 11:16

    You may also use unordered_map<char,vector<string>> which has some benefit over map structure. You can do such insertion if your dictionary is like 'c': "cat", 'c':"car", 'a':apple, 'a':"angus" :

    unordered_map<char, vector<string>> char_to_strings_map;
    //loop to traverse the dictionary : key:c, value:s
      char_to_strings_map[c].emplace_back(s);
    //loop ends
    
    0 讨论(0)
  • 2021-02-07 11:17

    If you're using C++ then just create a class to represent your key-value pairs:

    Class foo {
        key : String
        values : list of values
    }
    

    Then, create a map that maps each key to an object containing its values.

    This is simple, extendable, and can be done in any OO-language.

    Sorry, my C++ is rusty so the syntax is wrong, but the essential idea is straightforward.

    0 讨论(0)
提交回复
热议问题