Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualbedtime

Posted 27 November 2012 - 10:22 AM

A composite key would be instead of using a multimap with an int key containing multimaps with int keys, instead use a single multimap with a key that contains two ints like a std::pair<int, int> such as std::multimap<std::pair<int, int>, GameObject *>.


This sounds like a great idea and I don't want to keep bugging you for examples. Is there perhaps a good tutorial on this? I have several C++ books but they don't seem to go this in depth. If you can recommend a book that will help me with this I'll read it.

There seems to be a another way. That is to use a Key class to store the keys. How does that compare to using a 'pair' of keys?

Here's what I have played with so far ('val' is encapsulated code that allows me to store to that object and test it):

[source lang="cpp"]class Keys { public: Keys(int k1, int k2) : key1(k1), key2(k2) { } bool operator<(const Keys &right) const { return (key1 < right.key1 && key2 < right.key2); } const int getKey1() const { return key1; } const int getKey2() const { return key2; } int key1; int key2;}[/source]

[source lang="cpp"] std::multimap<Keys, GameObject> go2; GameObject* gameObj1 = new GameObject; gameObj1->val()->newNum("test", 10); go2.insert(std::pair<Keys, GameObject>( Keys(1, 2), *gameObj1) ); GameObject* gameObj2 = new GameObject; gameObj2->val()->newNum("test", 20); go2.insert(std::pair<Keys, GameObject>( Keys(3, 4), *gameObj2) ); GameObject* gameObj3 = new GameObject; gameObj3->val()->newNum("test", 30); go2.insert(std::pair<Keys, GameObject>( Keys(1, 2), *gameObj3) ); for(auto& i : go2) { std::cout << "x: " << i.first.getKey1() << " y: " << i.first.getKey2() << " test: " << i.second.val()->getNum("test") << std::endl; }[/source]

The above code seems to print out the results just fine, but I question how easy it'll be to get this to iterate in ranges. I do think it might be nice as it would allow multiple ways to sort but I'm new to all this.

#1bedtime

Posted 27 November 2012 - 10:20 AM

A composite key would be instead of using a multimap with an int key containing multimaps with int keys, instead use a single multimap with a key that contains two ints like a std::pair<int, int> such as std::multimap<std::pair<int, int>, GameObject *>.


This sounds like a great idea and I don't want to keep bugging you for examples. Is there perhaps a good tutorial on this? I have several C++ books but they don't seem to get to this level.

There seems to be a another way. That is to use a Key class to store the keys. How does that compare to using a 'pair' of keys?

Here's what I have played with so far ('val' is encapsulated code that allows me to store to that object and test it):

[source lang="cpp"]class Keys { public: Keys(int k1, int k2) : key1(k1), key2(k2) { } bool operator<(const Keys &right) const { return (key1 < right.key1 && key2 < right.key2); } const int getKey1() const { return key1; } const int getKey2() const { return key2; } int key1; int key2;}[/source]

[source lang="cpp"] std::multimap<Keys, GameObject> go2; GameObject* gameObj1 = new GameObject; gameObj1->val()->newNum("test", 10); go2.insert(std::pair<Keys, GameObject>( Keys(1, 2), *gameObj1) ); GameObject* gameObj2 = new GameObject; gameObj2->val()->newNum("test", 20); go2.insert(std::pair<Keys, GameObject>( Keys(3, 4), *gameObj2) ); GameObject* gameObj3 = new GameObject; gameObj3->val()->newNum("test", 30); go2.insert(std::pair<Keys, GameObject>( Keys(1, 2), *gameObj3) ); for(auto& i : go2) { std::cout << "x: " << i.first.getKey1() << " y: " << i.first.getKey2() << " test: " << i.second.val()->getNum("test") << std::endl; }[/source]

The above code seems to print out the results just fine, but I question how easy it'll be to get this to iterate in ranges. I do think it might be nice as it would allow multiple ways to sort but I'm new to all this.

PARTNERS