vendredi 22 septembre 2017

unordered_map

Where is my code.

#include <iostream>
#include <string>
#include <unordered_map>


using namespace std;

class Token {
    string strVar;
    int intVar;
    bool boolVar;

public:
    Token(string strVar, int intVar ,bool boolVar):
        strVar(strVar), intVar(intVar) ,boolVar(boolVar){
    }

    string getStrVar()  {return this->strVar;}
    int getIntVar()     {return this->intVar;}
    bool getBoolVar()   {return this->boolVar;}

};

int main(){
    unordered_map<int , int> mymap;
        //I WANT -> unordered_map<int , Token> mymap;

    mymap[1]= 11;
        //I WANT -> // mymap[1] = Token("string1", 1, true);
    mymap[2]= 12;
        //I WANT -> // mymap[2] = Token("string2", 2, false);
    mymap[3]= 13;
        //I WANT -> //mymap[3] = Token("string3", 3, true);

    int tk = mymap[1];
        //I WANT -> //Token tk = mymap[1];
    mymap[3] = tk;

    for (auto& x: mymap) {
        cout << x.first << ": " << x.second << endl;
        //I WANT -> print all Var from MyClass,
        //for example, x.second.getStrVar() << x.second.getIntVar() << x.second.getBoolVar() << endl;
    }

    return 0;
}

Atual OUTPUT:

3: 11 
2: 12
1: 11

As I explained in the comments, how can I use the unordered_map<> function with my class and loop through your elements so that I can use the methods of the class???

I need this output:

string1 1 true
string2 2 true
string1 1 true

Aucun commentaire:

Enregistrer un commentaire