最近在使用map时,有的地方要用到一对二的关系保存起来,这样方便后面取出对应的值进行操作,下面记录一下用法。
class Student{
public:
Student(string m_name, int m_age)
{
name = m_name;
age = m_age;
}
private:
string name;
int age;
}
bool func()
{
int i = 0;
vector vec;
map<string , vector> mp;
vec.push_back(Student(“张三” , 14));
vec.push_back(Student(“李四” , 15));
mp.insert(make_pair(“key_1” , vec)); //将整个vector放到map里面
//将值取出来
for (map<string, vector>::iterator iter = mp.begin(); iter != mp.end(); iter++){
vector vec1 = iter->second;
string name = vec1[i++].name;
int age = vec1[i++].age;
}
菜鸟记录,如有错误请指出。