11.27
当我想知道知道,对应关键字的元素有几个的时候,我会使用count
当我只想知道它在不在的时候,我会使用find
11.28
std::map<std::string ,std::vector<int>> hh{ {"p",{0,1,2,3}} }
auto iter= hh.find("p");
11.29
lower_bound和upper_bound 会返回一样的迭代器,指向关键字的插入点
equal_range会返回一个pair,两个迭代器和上面一样
11.30
pos是一个pair
pos.first 是一个迭代器指向关键字匹配的元素
pos.first->second 是元素的第二项,是value部分
11.31
@pezy
#include <map>
#include <string>
#include <iostream>
using std::string;
int main()
{
std::multimap<string, string> authors{
{ "alan", "DMA" },
{ "pezy", "LeetCode" },
{ "alan", "CLRS" },
{ "wang", "FTP" },
{ "pezy", "CP5" },
{ "wang", "CPP-Concurrency" }
};
// want to delete an element that author is [Alan], work is [112].
string author = "pezy";
string work = "CP5";
auto found = authors.find(author);
auto count = authors.count(author);
while (count) {
if (found->second == work) {
authors.erase(found);
break;
}
++found;
--count;
}
for (const auto &author : authors)
std::cout << author.first << " " << author.second << std::endl;
}
11.32
@pezy
#include <map>
#include <set>
#include <string>
#include <iostream>
using std::string;
int main()
{
std::multimap<string, string> authors{
{ "alan", "DMA" },
{ "pezy", "LeetCode" },
{ "alan", "CLRS" },
{ "wang", "FTP" },
{ "pezy", "CP5" },
{ "wang", "CPP-Concurrency" }
};
std::map<string, std::multiset<string>> order_authors;
for (const auto &author : authors)
order_authors[author.first].insert(author.second);
for (const auto &author : order_authors) {
std::cout << author.first << ": ";
for (const auto &work : author.second)
std::cout << work << " ";
std::cout << std::endl;
}
}