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"
words[5] = "wall."
words[6] = "the"
words[7] = "cat"
words[8] = "fell"
words[9] = "off"
words[10] = "the"
words[10] = "house."
etc. (Stupid example, but it works for this)
Each word will be a key with it's following word as it's value. so "over" => "the". Some keys can have multiple values. For example, "the" => "dog" || "wall" || "cat" || "house". The value is randomly chosen from those for that key.
When the program runs it picks a word at random and makes a sentence. So it could be something like: "the cat fell off the dog".
I tried implementing a map (map myMap;) but this allows only one value per key (I think).
Hope I explained this right.
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With