 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
Dr.Disrespect Grandmaster Cheater
Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Tue Aug 23, 2016 11:25 am Post subject: (C++) How to implement this data type? |
|
|
In C++, I want to make an array-like data type which has each key(string) associated with several values. It's like a multi-dimensional array, but using strings as keys. How to do that?
I tried to use multimap. But I do not know how to access a specific value. For example:
Code: |
std::multimap<std::string, int> test;
test.insert(std::pair<std::string, int>("Player1", 100));
test.insert(std::pair<std::string, int>("Player1", 200));
|
I use the multimap to save each player's data, such as attack power, defense, etc. But how can I access, for example, the second value under the key "Player1"?
Thanks a lot.
|
|
Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4702
|
Posted: Tue Aug 23, 2016 1:30 pm Post subject: |
|
|
It's possible to iterate over a specific key using multimap::equal_range (reference & example); however, the values associated with a specific key have no more semantic value than what they already have on their own. In this case, you won't be able to discern a specific player's attack from their defense if you store them as basic integers under the key of the player's name.
Alternatively, using a map of structs or classes might be better.
Code: | #include <iostream>
#include <map>
#include <string>
struct playerStats {
int attack;
int defense;
} player1, player2;
int main() {
player1.attack = 100;
player1.defense = 20;
player2.attack = 999;
player2.defense = 124;
std::map <std::string, playerStats* > players;
players["Steve"] = &player1;
players["Bob"] = &player2;
for (std::map<std::string, playerStats* >::iterator it = players.begin(); it != players.end(); it++) {
std::cout << it->first << ": \n\tAttack: " << it->second->attack << "\n\tDefense: " << it->second->defense << std::endl;
}
return 0;
} |
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
 |
Dr.Disrespect Grandmaster Cheater
Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Tue Aug 23, 2016 3:34 pm Post subject: |
|
|
ParkourPenguin wrote: | It's possible to iterate over a specific key using multimap::equal_range (reference & example); however, the values associated with a specific key have no more semantic value than what they already have on their own. In this case, you won't be able to discern a specific player's attack from their defense if you store them as basic integers under the key of the player's name.
Alternatively, using a map of structs or classes might be better.
Code: | #include <iostream>
#include <map>
#include <string>
struct playerStats {
int attack;
int defense;
} player1, player2;
int main() {
player1.attack = 100;
player1.defense = 20;
player2.attack = 999;
player2.defense = 124;
std::map <std::string, playerStats* > players;
players["Steve"] = &player1;
players["Bob"] = &player2;
for (std::map<std::string, playerStats* >::iterator it = players.begin(); it != players.end(); it++) {
std::cout << it->first << ": \n\tAttack: " << it->second->attack << "\n\tDefense: " << it->second->defense << std::endl;
}
return 0;
} |
|
Thank you, Penguin. I have a follow up question:
Code: |
struct playerStats {
int attack;
int defense;
} player1, player2;
|
Are player1 and player2 inside the curly bracket or not? Are they memebers of the struct?
|
|
Back to top |
|
 |
Zanzer I post too much
Reputation: 126
Joined: 09 Jun 2013 Posts: 3278
|
Posted: Tue Aug 23, 2016 5:48 pm Post subject: |
|
|
They are outside of the curly braces.
They are instances of the struct (variables).
The members are attack and defense.
|
|
Back to top |
|
 |
Dr.Disrespect Grandmaster Cheater
Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Tue Aug 23, 2016 6:00 pm Post subject: |
|
|
Zanzer wrote: | They are outside of the curly braces.
They are instances of the struct (variables).
The members are attack and defense. |
Thanks, Zanzer.
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|