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

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

提交回复
热议问题