set<string>::value_type v1; //这个v1是string类型
set<string>::key_type v2; //这个v2是string类型
map<string, int>::value_type v3; //v3是pair类型pair<const string, int>
map<string, int>::key_type v4; //v4是string类型
map<string, int>::mapped_type v5; //v5是int类型
map<string, size_t> word_count; // empty map from string to size_t
string word;
while (cin >> word)
++word_count[word];
//获得指向word_count中的一个元素的迭代器
auto map_it=word_count.begin();
//*map_it是指向一个pair<const string, size_t>对象的引用
cout<<map_it->first; //打印此元素的关键字
cout<<" "<<map_it->second; //打印此元素的值
// map_it->first="new key"; //错误:关键字是const的
++map_it->second; //正确:我们可以通过迭代器改变元素
//可以用一个set迭代器来读取元素的值,但不能修改
void fun3()
{
set<int> iset={0,1,2,3,4,5,6,7,8,9};
set<int>::iterator set_it=iset.begin();
if(set_it != iset.end())
{
// *set_it=42; //错误!!!
cout<<*set_it<<endl; //正确:可以读关键字
}
}
//遍历关联容器
void fun4()
{
map<string, size_t> word_count; // empty map from string to size_t
string word;
while (cin >> word)
++word_count[word];
//获得指向word_count中的一个元素的迭代器
auto map_it=word_count.cbegin();
//比较当前迭代器和尾后迭代器
while(map_it != word_count.cend())
{
//解引用迭代器,打印关键字-值对
cout<<map_it->first<<" occurs "
<<map_it->second<<" times "<<endl;
++map_it; //递增迭代器,移动到下一个元素
}
}
vector<int> ivec={2,4,6,8,2,4,6,8}; //ivec有8个元素
set<int> set2; //空集合
set2.insert(ivec.cbegin(), ivec.cend()); //set2有4个元素
set2.insert({1,3,5,7,1,3,5,7}); //现在set2有8个元素
//向map添加元素
void fun6()
{
map<string, size_t> word_count; // empty map from string to size_t
string word;
while (cin >> word)
++word_count[word];
//向word_count插入word的4种方法
word_count.insert({word, 1});
word_count.insert(make_pair(word, 1));
word_count.insert(pair<string, size_t>(word, 1));
word_count.insert(map<string, size_t>::value_type(word, 1));
}
//检查insert的返回值
//重写单词计数程序
void fun7()
{
map<string, size_t> word_count; //从string到size_t的空map
string word;
while(cin>>word)
{
//插入一个元素,关键字等于word,值为1
//若word已在word_count中,insert什么也不做
auto ret=word_count.insert({word, 1});
if(!ret.second)
{
++ret.first->second; //递增计数器
}
}
}
//向multimap和multiset添加元素,这个可以重复
void fun8()
{
multimap<string, string> authors;
//插入第一个元素,关键字为Barth, John
authors.insert({"Barth, John", "Sot-Weed Factor"});
//正确:添加第二个元素,关键字也是Barth, John
authors.insert({"Barth, John", "Lost in the Funhouse"});
}
//删除元素
void fun9()
{
map<string, size_t> word_count; // empty map from string to size_t
string word,removal_word;
while (cin >> word)
++word_count[word];
//删除一个关键字,返回删除的元素数量
cin>>removal_word;
if(word_count.erase(removal_word))
cout<<" ok: "<<removal_word<<" remove\n";
else
cout<<" oops: "<<removal_word<<" not found!\n";
}
/**
* 功能:关联容器操作
* 时间:2014年6月26日16:40:51
* 作者:cutter_point
*/
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<string>
using namespace std;
//关联容器额外的类型别名
void fun1()
{
set<string>::value_type v1; //这个v1是string类型
set<string>::key_type v2; //这个v2是string类型
map<string, int>::value_type v3; //v3是pair类型pair<const string, int>
map<string, int>::key_type v4; //v4是string类型
map<string, int>::mapped_type v5; //v5是int类型
}
//关联容器迭代器
void fun2()
{
map<string, size_t> word_count; // empty map from string to size_t
string word;
while (cin >> word)
++word_count[word];
//获得指向word_count中的一个元素的迭代器
auto map_it=word_count.begin();
//*map_it是指向一个pair<const string, size_t>对象的引用
cout<<map_it->first; //打印此元素的关键字
cout<<" "<<map_it->second; //打印此元素的值
// map_it->first="new key"; //错误:关键字是const的
++map_it->second; //正确:我们可以通过迭代器改变元素
}
//可以用一个set迭代器来读取元素的值,但不能修改
void fun3()
{
set<int> iset={0,1,2,3,4,5,6,7,8,9};
set<int>::iterator set_it=iset.begin();
if(set_it != iset.end())
{
// *set_it=42; //错误!!!
cout<<*set_it<<endl; //正确:可以读关键字
}
}
//遍历关联容器
void fun4()
{
map<string, size_t> word_count; // empty map from string to size_t
string word;
while (cin >> word)
++word_count[word];
//获得指向word_count中的一个元素的迭代器
auto map_it=word_count.cbegin();
//比较当前迭代器和尾后迭代器
while(map_it != word_count.cend())
{
//解引用迭代器,打印关键字-值对
cout<<map_it->first<<" occurs "
<<map_it->second<<" times "<<endl;
++map_it; //递增迭代器,移动到下一个元素
}
}
//添加元素
void fun5()
{
vector<int> ivec={2,4,6,8,2,4,6,8}; //ivec有8个元素
set<int> set2; //空集合
set2.insert(ivec.cbegin(), ivec.cend()); //set2有4个元素
set2.insert({1,3,5,7,1,3,5,7}); //现在set2有8个元素
}
//向map添加元素
void fun6()
{
map<string, size_t> word_count; // empty map from string to size_t
string word;
while (cin >> word)
++word_count[word];
//向word_count插入word的4种方法
word_count.insert({word, 1});
word_count.insert(make_pair(word, 1));
word_count.insert(pair<string, size_t>(word, 1));
word_count.insert(map<string, size_t>::value_type(word, 1));
}
//检查insert的返回值
//重写单词计数程序
void fun7()
{
map<string, size_t> word_count; //从string到size_t的空map
string word;
while(cin>>word)
{
//插入一个元素,关键字等于word,值为1
//若word已在word_count中,insert什么也不做
auto ret=word_count.insert({word, 1});
if(!ret.second)
{
++ret.first->second; //递增计数器
}
}
}
//向multimap和multiset添加元素,这个可以重复
void fun8()
{
multimap<string, string> authors;
//插入第一个元素,关键字为Barth, John
authors.insert({"Barth, John", "Sot-Weed Factor"});
//正确:添加第二个元素,关键字也是Barth, John
authors.insert({"Barth, John", "Lost in the Funhouse"});
}
//删除元素
void fun9()
{
map<string, size_t> word_count; // empty map from string to size_t
string word,removal_word;
while (cin >> word)
++word_count[word];
//删除一个关键字,返回删除的元素数量
cin>>removal_word;
if(word_count.erase(removal_word))
cout<<" ok: "<<removal_word<<" remove\n";
else
cout<<" oops: "<<removal_word<<" not found!\n";
}
int main()
{
fun1();
fun2();
fun3();
fun4();
fun5();
fun6();
fun7();
fun8();
fun9();
return 0;
}

【足迹C++primer】38、关联容器操作(1),布布扣,bubuko.com
原文地址:http://blog.csdn.net/cutter_point/article/details/34861343