11.1 map是两种类型的一一对应,vector是一种类型的容器。
11.2 略
11.3
int main(int argc, char* argv[])
{
map<string, int> stupidmap;
string strtemp;
while (cin >> strtemp)
++stupidmap[strtemp];
for (const auto& c : stupidmap)
cout << c.first << " " << c.second << endl;
return 0;
}
11.4 略
11.5 map关键字和值一一对应,set只记关键字
11.6 顺序或按位置访问成员用list
检查成员是否在容器中用set
11.7
void add_lastname(map<string, vector<string>> &maptemp, const string &lastname)
{
maptemp[lastname];
}
void add_firstname(map<string, vector<string>> &maptemp, const string &lastname, const string &firstname)
{
maptemp[lastname].push_back(firstname);
}
int main(int argc, char* argv[])
{
map<string, vector<string>> stupidmap;
string firsttemp, secondtemp;
cout << "输入姓氏+空格+名字" << endl;
while (cin >> firsttemp)
{
cin >> secondtemp;
add_firstname(stupidmap, firsttemp, secondtemp);
}
for (const auto &c : stupidmap)
{
for (const auto &b : c.second)
cout << c.first << " " << b << endl;
}
return 0;
}
11.8 快速找出元素是否在容器内
11.9 map
int main(int argc, char* argv[])
{
vector<pair<string, int>> vectemp;
string strtemp;
int inttemp;
cout << "输入string和int序列" << endl;
while (cin >> strtemp)
{
cin >> inttemp;
vectemp.push_back({ strtemp,inttemp });
}
for (const auto &c : vectemp)
cout << c.first << " " << c.second << endl;
return 0;
}
11.13-11.14略
11.15 vector,int,map