11.5
set中保存一个key值。同时它也value值,
map不仅有一个key值,还有一个与它相关联的value值,如果你需要另一个 ,你就需要使用map,如果不需要就使用set
11.6
@http://stackoverflow.com/questions/2302681/c-stl-list-vs-set
List
Searching (linear time).
Inserting, deleting, moving (takes constant time).
Elements may be ordered.
Elements may be sorted.
Elements may be duplicate.
Set
Searching (logarithmic in size).
Insert and delete (logarithimic in general).
Elements are un-ordered.
Elements are always sorted from lower to higher.
Elements are unique.
11.7
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
using std::string;
using std::vector;
using std::map;
using std::cin;
using std::cout;
using std::endl;
using Families = map<string, vector<string>>;
Families make_families()
{
Families families;
string in1,in2;
cout << "姓:" << endl;
cin >> in1;
cout << "孩子:" << endl;
while (cin >> in2) {
families[in1].push_back(in2);
}
return families;
}
int main() {
auto mf = make_families();
for (const auto &a : mf) {
cout << "姓"<<a.first << ":\n";
for (const auto &b : a.second) {
cout << b << " ";
}
}
return 0;
}
11.8
@Yue Wang Sep
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::string> exclude = { "aa", "bb", "cc", "dd", "ee", "ff" };
for (std::string word; std::cout << "Enter plz:\n", std::cin >> word; )
{
auto is_excluded = std::binary_search(exclude.cbegin(), exclude.cend(), word);
auto reply = is_excluded ? "excluded" : "not excluded";
std::cout << reply << std::endl;
}
return 0;
}
使用vector需要自己去寻找单词是不是出现过,而set就不用