#include <map>
using std::map;
#include <string>
using std::string;
#include <utility>
using std::pair;
#include <cstddef>
using std::size_t;
#include <iostream>
using std::cin; using std::cout; using std::endl;
int main()
{
// count the number of times each word occurs in the input
map<string, size_t> word_count; // empty map from string to size_t
string word;
while (cin >> word)
++word_count[word];
for (const auto &w : word_count)
cout << w.first << " occurs " << w.second << " times" << endl;
// get an iterator positioned on the first element
auto map_it = word_count.cbegin();
// compare the current iterator to the off-the-end iterator
while (map_it != word_count.cend()) {
// dereference the iterator to print the element key--value pairs
cout << map_it->first << " occurs "
<< map_it->second << " times" << endl;
++map_it; // increment the iterator to denote the next element
}
return 0;
}
map和set采用默认初始化
对于有序容器,必须定义比较的方法。默认情况下,标准库使用<来比较两个关键字(<要满足严格弱序所有条件)
定义一个Mutiset
mutiset
string search_item("Alain de Botton");
auto entries=authors.count(search_item);
auto iter=authors.find(search_item);
while(entries)
{
cout<<iter->second<<endl;
++iter;
--entries;
}
//当然lower_bound与upper_bound方法同样也可以
//如果元素不存在与容器之中,则lower_bound与Upper_bound返回同样的值。
for(auto beg=authors.lower_bound(search_item),end=authors.upper_bound(search_item);beg!=end;++beg)
cout<<beg->second<<endl;
//此外,还有equal_range,该方法返回一个pair,第一个迭代器返回首个元素,第二个迭代器指向最后一个元素之后的位置。
for(auto pos=authors.equal_range(search_item);
pos.first!=pos.second;++pos.first)
cout<<pos.first->second<<endl;
无序容器实际上使用的是哈希相关方法。或者更确切的说,是链地址法。
因而,使用.begin,.end函数时要加入参数int,指明是第几个链条上的迭代器。
#include "Version_test.h"
#include <unordered_map>
using std::unordered_map;
#include <unordered_set>
using std::unordered_set; using std::unordered_multiset;
#include <string>
using std::string;
using std::hash;
#include <iostream>
using std::cin; using std::cout; using std::endl;
#include <cstddef>
using std::size_t;
#include "Sales_data.h"
// unordered_map version of the word count program
int main()
{
// count occurrences, but the words won't be in alphabetical order
unordered_map<string, size_t> word_count;
string word;
while (cin >> word)
++word_count[word]; // fetch and increment the counter for word
for (const auto &w : word_count) // for each element in the map
// print the results
cout << w.first << " occurs " << w.second
<< ((w.second > 1) ? " times" : " time") << endl;
return 0;
}
// how to override default hash and equality operator on key_type
size_t hasher(const Sales_data &sd)
{
return hash<string>()(sd.isbn());
}
bool eqOp(const Sales_data &lhs, const Sales_data &rhs)
{
return lhs.isbn() == rhs.isbn();
}
// type alias using our functions in place of hash<key_type> and ==
#ifdef TYPE_ALIAS_DECLS
using SD_multiset = unordered_multiset<Sales_data,
decltype(hasher)*, decltype(eqOp)*>;
#else
typedef
unordered_multiset<Sales_data, decltype(hasher)*, decltype(eqOp)*> SD_multiset;
#endif
// bookstore can hold multiple Sales_data with the same ISBN
// arguments are the bucket size
// and pointers to the hash function and equality operator
SD_multiset bookstore(42, hasher, eqOp);
// how to override just the hash function;
// Foo must have ==
struct Foo { string s; };
// we'll see how to define our own operators in chapter 14
bool operator==(const Foo& l, const Foo&r) { return l.s == r.s; }
size_t FooHash(const Foo& f) { return hash<string>()(f.s); }
// use FooHash to generate the hash code; Foo must have an == operator
unordered_set<Foo, decltype(FooHash)*> fooSet(10, FooHash);
当然,如果类定义了==操作,那么eqop这个方法是不必的。