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

前端 未结 7 1299
悲哀的现实
悲哀的现实 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:13

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

    pair 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.

提交回复
热议问题