关联容器和顺序容器都是容器,所以都支持容器操作。就是表格9.2所示的操作。以下以map为例测试表格中的操作是否满足关联容器。
1.类型别名:
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <iterator>
#include <fstream>
using namespace std;
int main()
{
string f = "file";
ifstream in(f);
map<string,unsigned> words_count;
string str;
while(in >> str)
{
words_count[str] ++;
}
//iterator
map<string,unsigned>::iterator it;
for(it = words_count.begin(); it != words_count.end();++it)
{
cout << it->first << " occurs " << it->second << (it-&