X
xuatla
Hi,
I want to define a map:
std::map<string, int> myMap;
e.g., the score of students. Then I can assign the value as follows:
myMap["stud1"] = 90;
myMap["stud2"] = 60;
....
My question now is: can I assign the name of many students in one line?
e.g., for array we have the following way:
int myArray[] = { 1, 3, 4, 5 };
Do we have similar way for map?
std::map<string, int> myMap = { ("stud1", 90), ("stud2", 60) };
// wrong code
Another question: how can I print the score of a given student's name?
void getScore(std::map<string, int> myMap, const std::string& stuName)
{
return myMap.find(stuName)->second();
}
Is this correct? Any better solution?
Thanks in advance!
-X
I want to define a map:
std::map<string, int> myMap;
e.g., the score of students. Then I can assign the value as follows:
myMap["stud1"] = 90;
myMap["stud2"] = 60;
....
My question now is: can I assign the name of many students in one line?
e.g., for array we have the following way:
int myArray[] = { 1, 3, 4, 5 };
Do we have similar way for map?
std::map<string, int> myMap = { ("stud1", 90), ("stud2", 60) };
// wrong code
Another question: how can I print the score of a given student's name?
void getScore(std::map<string, int> myMap, const std::string& stuName)
{
return myMap.find(stuName)->second();
}
Is this correct? Any better solution?
Thanks in advance!
-X