练习11.27
对于什么问题你会用count来解决?
什么时候你又会选择find呢?
解答:
【引用】应该使用哪个操作依赖与我们要解决什么问题。如果我们所关心的只不过是一个特定元素是否已在容器中,可能find是最佳的选择。对于不允许重复关键字的容器,可能使用find还是count没有什么区别。但对于允许重复关键字的容器,count还会做更多的工作:如果元素在容器中,他还会统计有多少个元素有相同的关键字。如果不需要计数,最好使用find。
练习11.28
对一个string到int的vector的map,定义并初始化一个变量来保存在其上调用find所返回的结果。
解答:
map<string, vector<int>> ref_map;
// ... do something
map<string, vector<int>>::iterator iter = ref_map.find("hello");
练习11.29
如果给定的关键字不在容器中,upper_bound、lower_bound和equal_range分别会返回什么?
解答:
upper_bound和lower_bound的结果是一样的,返回尾后迭代器,也就是c.end()指向的迭代器。
equal_range会返回一个pair,且pair的两个成员均等于c.end()
练习11.30
对于本节最后一个程序中的输出表达式,解释运算对象pos.first->second的含义。
解答:
当关键字存在时,equal_range将会返回一个迭代器pair。第一个迭代器指向第一个与关键字匹配的元素,第二个迭代器指向最后一个匹配元素之后的位置。
所以,这里pos.first指向的就是第一个与关键字匹配的元素的迭代器,pos.first->second就是对这个key进行映射值的获取。
练习11.31
编写程序,定义一个作者及其座屏的multimap。使用find在multimap中查找一个元素并用erase删除它。
确保你的程序在元素不在map中也能正常运行。
解答:
#include <iostream>
#include <map>
#include <string>
using namespace std;
void init_map(multimap<string, string> &map_){
map_.insert({ "Arthur Conan Doyle", "A Study in Scarlet" });
map_.insert({ "Arthur Conan Doyle", "The War in South Africa:Its Cause and Conduct" });
map_.insert({ "Arthur Conan Doyle", "The Sign of Four" });
map_.insert({ "Arthur Conan Doyle", "Silver Blaze" });
map_.insert({ "Agatha Christie", "Murder on the Links" });
map_.insert({ "Agatha Christie", "The ABC Murders" });
map_.insert({ "Agatha Christie", "Murder in the Calais Coach" });
}
void searchBook(multimap<string, string> &map_, string author_name){
auto iter = map_.find(author_name);
if (iter != map_.end()){
map_.erase(iter);
cout << "remove the specific author." << endl;
}
else{
cerr << "Can not find specified author!! Please check the author name." << endl;
}
}
int main(){
multimap<string, string> book_set;
init_map(book_set);
searchBook(book_set, "Agatha Christie");
searchBook(book_set, "Edokawa Ranho");
}
练习11.32
使用上一题定义的multimap编写一个程序,按字典序打印作者列表和他们的作品。
解答:
#include <iostream>
#include <map>
#include <string>
#include <set>
using namespace std;
void init_map(multimap<string, string> &map_){
map_.insert({ "Arthur Conan Doyle", "A Study in Scarlet" });
map_.insert({ "Arthur Conan Doyle", "The War in South Africa:Its Cause and Conduct" });
map_.insert({ "Arthur Conan Doyle", "Silver Blaze" });
map_.insert({ "Arthur Conan Doyle", "The Sign of Four" });
map_.insert({ "Agatha Christie", "Murder on the Links" });
map_.insert({ "Agatha Christie", "The ABC Murders" });
map_.insert({ "Agatha Christie", "Murder in the Calais Coach" });
}
void setPrint(const multimap<string, string>& map_){
set<string> authors, books;
for (auto iter = map_.cbegin(); iter != map_.cend(); ++iter){
authors.insert(iter->first);
}
for (auto iter = authors.cbegin(); iter != authors.cend(); ++iter){
cout << *iter << "'s books list:" << endl;
for (auto pos = map_.equal_range(*iter); pos.first != pos.second; ++pos.first){
books.insert(pos.first->second);
}
for (auto i : books){
cout << "\t" << i << endl;
}
books.clear();
}
}
int main(){
multimap<string, string> book_set;
init_map(book_set);
setPrint(book_set);
}
这里用了set容器来解决排序和除重的工作。当然,范型中的算法也是可以办到的。