Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


(C++) How to implement this data type?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Aug 23, 2016 11:25 am    Post subject: (C++) How to implement this data type? Reply with quote

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
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4702

PostPosted: Tue Aug 23, 2016 1:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Aug 23, 2016 3:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Tue Aug 23, 2016 5:48 pm    Post subject: Reply with quote

They are outside of the curly braces.
They are instances of the struct (variables).
The members are attack and defense.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Aug 23, 2016 6:00 pm    Post subject: Reply with quote

Zanzer wrote:
They are outside of the curly braces.
They are instances of the struct (variables).
The members are attack and defense.


Thanks, Zanzer. Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites