版权声明:本文为博主原创文章。未经博主同意不得转载。

https://blog.csdn.net/cutter_point/article/details/35244805

关联容器操作(2)

map的下标操作

map的下标操作
map和unordered_map容器提供了下标运算符合一个相应的at函数
对于一个map使用下标操作,其行为与数组或vector上的下标操作非常不同样:
使用一个不再容器中的keyword作为下标。会加入一个此keyword的元素到map中
map和unordered_map的下标操作
c[k] 返回keyword为k的元素。假设keywordk不再c中,加入一个keyword为k的元素,对其进行值初始化
c.at(k) 訪问keyword为k的元素,带參数检測,假设k不再c重那么返回一个异常,out_of_range
与vector和string不同。map下标运算符返回的类型与解引用map迭代器得到的类型不同。

訪问元素

假设我们关心的仅仅只是是特定元素是否在容器中的时候,泛型算法find是最佳的选择
void fun1()
{
set<int> iset={0,1,2,3,4,5,6,7,8,9};
iset.find(1); //返回一个迭代器。指向key==1的元素
iset.find(11); //返回一个迭代器,值指向iset.end()
iset.count(1); //返回1
iset.count(11); //返回0
}

今天上传的有点晚了,放假了,人也懒了- -

对map使用find取代下标操作

下标操作会自己主动加入元素,不好
void fun2()
{
map<string, size_t> word_count;
string word;
while(cin>>word)
++word_count[word]; if(word_count.find("foobar") == word_count.end())
cout<<"foobar is not in the map "<<endl;
}

在multimap和multiset中查找元素

当我们遍历multimap和multiset的时候。保证能够得到序列中全部具有给定keyword的元素
void fun3()
{
multimap<string, string> authors;
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返回迭代器可能指向一个具有给定keyword的元素。但也可能不指向。
假设keyword不在容器中,则lower_bound会返回keyword的第一个安全插入点--不影响
容器中元素顺序的插入位置
void fun4()
{
//authors和search_item的定义,和前面的程序一样
//beg和end表示相应此作者的元素的范围
multimap<string, string> authors;
string search_item("Alain de Botton"); //要查找的作者 for(auto beg=authors.lower_bound(search_item),
end=authors.upper_bound(search_item) ; beg != end; ++beg)
cout<<beg->second<<endl; //打印每一个题目 //upper_bound调用将end指向最后一个匹配search_item的元素的后一位
}

有木有发现,我如今博客越来越顺畅啦,相信大家也看看出来了吧,嘿嘿,我每次都打好底稿。然后再上传,边传边改,这样就不会像曾经那样了,非常乱,并且有点赶时间的嫌疑。

equal_range函数

不用lower_bound和upper_bound直接调用equal_range就可以。
这个函数接受一个keyword。返回一个迭代器pair,第一个迭代器指向
第一个和keyword匹配的元素,第二个指向和最后一个匹配的元素的后一个位置
void fun5()
{
//authors和search_item的定义,和前面的程序一样
//pos保存迭代器对,表示keyword匹配的元素范围
multimap<string, string> authors;
string search_item("Alain de Botton"); //要查找的作者 pair<multimap<string, string>::iterator, multimap<string, string>::iterator> pos;
for(pos=authors.equal_range(search_item); pos.first != pos.second ; ++pos.first)
cout<<pos.first->second<<endl; //打印每一个题目 }

一个单词转换的map

施主。这次能看懂多少看个人造化了^_^。
单词转换文件。我把它叫做map_file.txt
brb be right back
k okay?
y why
r are
u you
pic picture
thk thanks!
18r later
我们希望转换的文本,这个叫做input.txt
where r u
y dont u send me a pic
k thk 18r
程序输出生成这样:
where are you
why dont you send me a picture
okay?

thanks! later

#include<iostream>
#include<fstream>
#include<sstream>
#include<map>
#include<set>
#include<string>
#include<utility> //pair using namespace std; map<string, string> bulidMap(ifstream &map_file)
{
map<string, string> trans_map; //保存转换规则
string key; //要转换的单词
string value; //替换后的内容
//读取第一个单词存入key中,行中剩余内容存入value
while(map_file>>key && getline(map_file, value))
{
if(value.size() > 1) //坚持是否有转换规则
{
value=value.substr(1); //跳过前导空格
//插入map
trans_map.insert({key, value});
}
else
throw runtime_error(" no rule for "+key);
}
return trans_map;
} const string & transform(const string &s, const map<string, string> &m)
{
//实际转换工作;此部分是程序核心
auto map_it=m.find(s);
//假设单词在转换规则map中
if(map_it != m.cend())
{
return map_it->second; //使用替换短语
}
else
{
return s; //返回原值
} } void word_transform(ifstream &map_file, ifstream &input)
{
auto trans_map=bulidMap(map_file); //保存转换规则
string text; //保存输入中的没一行
while(getline(input, text)) //读取输入一行
{
istringstream stream(text); //读取每一个单词
string word;
bool firstword=true; //控制是否打印空格 while(stream>>word)
{
if(firstword)
firstword=false;
else
cout<<" "; //在单词之间打印一个空格 //transform返回它的第一个參数或其转换之后的形式
// cout<<endl<<"-------------------------------------"<<endl;
// cout<<"这个单词是:"<<word<<endl;
// cout<<"-------------------------------------"<<endl;
cout<<transform(word, trans_map); //打印输出
}
cout<<endl; //完毕一行的转换
}
} int main()
{
ifstream map_file,input;
map_file.open("map_file.txt");
input.open("input.txt"); word_transform(map_file, input); map_file.close();
input.close(); return 0;
}

好的劳动了一上午的成果!!

/**
* 功能:关联容器操作
* 时间:2014年6月27日09:55:55
* 作者:cutter_point
*/ #include<iostream>
#include<fstream>
#include<sstream>
#include<map>
#include<set>
#include<string>
#include<utility> //pair using namespace std; //map的下标操作
//map和unordered_map容器提供了下标运算符合一个相应的at函数
//对于一个map使用下标操作。其行为与数组或vector上的下标操作非常不同样:
//使用一个不再容器中的keyword作为下标。会加入一个此keyword的元素到map中 /*
map和unordered_map的下标操作
c[k] 返回keyword为k的元素。假设keywordk不再c中,加入一个keyword为k的元素,对其进行值初始化
c.at(k) 訪问keyword为k的元素,带參数检測。假设k不再c重那么返回一个异常,out_of_range
*/ //与vector和string不同,map下标运算符返回的类型与解引用map迭代器得到的类型不同。 //訪问元素
//假设我们关心的仅仅只是是特定元素是否在容器中的时候,find是最佳的选择
void fun1()
{
set<int> iset={0,1,2,3,4,5,6,7,8,9};
iset.find(1); //返回一个迭代器。指向key==1的元素
iset.find(11); //返回一个迭代器,值指向iset.end()
iset.count(1); //返回1
iset.count(11); //返回0
} //对map使用find取代下标操作
//下标操作会自己主动加入元素,不好
void fun2()
{
map<string, size_t> word_count;
string word;
while(cin>>word)
++word_count[word]; if(word_count.find("foobar") == word_count.end())
cout<<"foobar is not in the map "<<endl;
} //在multimap和multiset中查找元素
//当我们遍历multimap和multiset的时候,保证能够得到序列中全部具有给定keyword的元素
void fun3()
{
multimap<string, string> authors;
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返回迭代器可能指向一个具有给定keyword的元素,但也可能不指向。 假设keyword不在容器中。则lower_bound会返回keyword的第一个安全插入点--不影响
容器中元素顺序的插入位置
*/
void fun4()
{
//authors和search_item的定义,和前面的程序一样
//beg和end表示相应此作者的元素的范围
multimap<string, string> authors;
string search_item("Alain de Botton"); //要查找的作者 for(auto beg=authors.lower_bound(search_item),
end=authors.upper_bound(search_item) ; beg != end; ++beg)
cout<<beg->second<<endl; //打印每一个题目 //upper_bound调用将end指向最后一个匹配search_item的元素的后一位
} //equal_range函数
/*
不用lower_bound和upper_bound直接调用equal_range就可以。
这个函数接受一个keyword。返回一个迭代器pair。第一个迭代器指向
第一个和keyword匹配的元素,第二个指向和最后一个匹配的元素的后一个位置
*/
void fun5()
{
//authors和search_item的定义,和前面的程序一样
//pos保存迭代器对。表示keyword匹配的元素范围
multimap<string, string> authors;
string search_item("Alain de Botton"); //要查找的作者 pair<multimap<string, string>::iterator, multimap<string, string>::iterator> pos;
for(pos=authors.equal_range(search_item); pos.first != pos.second ; ++pos.first)
cout<<pos.first->second<<endl; //打印每一个题目 } //一个单词转换的map
//施主。这次能看懂多少看个人造化了^_^。
/*
单词转换文件
brb be right back
k okay?
y why
r are
u you
pic picture
thk thanks!
18r later
我们希望转换的文本
where r u
y dont u send me a pic
k thk 18r
程序输出生成这样:
where are you
why dont you send me a picture
okay? thanks! later
*/
map<string, string> bulidMap(ifstream &map_file)
{
map<string, string> trans_map; //保存转换规则
string key; //要转换的单词
string value; //替换后的内容
//读取第一个单词存入key中,行中剩余内容存入value
while(map_file>>key && getline(map_file, value))
{
if(value.size() > 1) //坚持是否有转换规则
{
value=value.substr(1); //跳过前导空格
//插入map
trans_map.insert({key, value});
}
else
throw runtime_error(" no rule for "+key);
}
return trans_map;
} const string & transform(const string &s, const map<string, string> &m)
{
//实际转换工作。此部分是程序核心
auto map_it=m.find(s);
//假设单词在转换规则map中
if(map_it != m.cend())
{
return map_it->second; //使用替换短语
}
else
{
return s; //返回原值
} } void word_transform(ifstream &map_file, ifstream &input)
{
auto trans_map=bulidMap(map_file); //保存转换规则
string text; //保存输入中的没一行
while(getline(input, text)) //读取输入一行
{
istringstream stream(text); //读取每一个单词
string word;
bool firstword=true; //控制是否打印空格 while(stream>>word)
{
if(firstword)
firstword=false;
else
cout<<" "; //在单词之间打印一个空格 //transform返回它的第一个參数或其转换之后的形式
// cout<<endl<<"-------------------------------------"<<endl;
// cout<<"这个单词是:"<<word<<endl;
// cout<<"-------------------------------------"<<endl;
cout<<transform(word, trans_map); //打印输出
}
cout<<endl; //完毕一行的转换
}
} int main()
{
ifstream map_file,input;
map_file.open("map_file.txt");
input.open("input.txt"); word_transform(map_file, input); map_file.close();
input.close(); return 0;
}

这个

bulidMap(ifstream &map_file)
里面的return记得要放到while循环的外面哦
我但是为了这个吃了不少苦头。
PS:放假了。我预计最多把今天写完了,明天開始大概要停更了。大家不要停止学习哦。一起加油,一起努力。这本书我还是会看下去的,仅仅是博客可能更不了了。

【足迹C++primer】38、关联容器操作(2)的更多相关文章

  1. 【C++ Primer 第11章】2. 关联容器操作

    练习答案 一.访问元素 关联容器额外类型别名  key_type 此容器类型的关键字类型 mapped_type 每个关键字关联的类型,只 适用于map mapped_type 对于set,与key_ ...

  2. C++ Primer 笔记——关联容器

    1.关联容器支持高效的关键字查找和访问,标准库提供8个关联容器. 2.如果一个类型定义了“行为正常”的 < 运算符,则它可以用作关键字类型. 3.为了使用自己定义的类型,在定义multiset时 ...

  3. c++ primer 10 关联容器

    关联容器和顺序容器的本质差别在于:关联容器通过键(key)存储和读取元素,顺序容器则通过元素在容器中的位置顺序存储和访问元素 关联容器类型 map 关联数组:元素通过键来存储和读取 set 大小可变的 ...

  4. 【C++ Primer 第11章 练习答案】2. 关联容器操作

    11.3.1节练习 [练习11.16]代码 map<int, int> m; auto iter = m.begin(); iter ->second = ;

  5. C++ Primer笔记7_STL之关联容器

    关联容器 与顺序容器不同,关联容器的元素是按keyword来訪问和保存的.而顺序容器中的元素是按他们在容器中的位置来顺序保存的. 关联容器最常见的是map.set.multimap.multiset ...

  6. [C++ Primer] : 第11章: 关联容器

    目录 使用关联容器 关联容器概述 关联容器操作 无序容器 使用关联容器 关联容器与顺序容器有着根本的不同: 关联容器中的元素是按关键字来保存和访问的, 按顺序容器中的元素是按它们在容器中的位置来顺序保 ...

  7. C++ 关联容器

    <C++ Primer 4th>读书笔记 关联容器和顺序容器的本质差别在于:关联容器通过键(key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素. 关联容器(Ass ...

  8. 关联容器——map、set

    map类型通常被称为关联数组,与正常数组类似,不同之处在于其下标不必是整数.我们通过一个关键字而不是位置来查找值(键值对). 与之相对,set就是关键字的简单集合.当只是想知道一个值是否存在时,set ...

  9. CH11 关联容器

    关联容器与顺序容器有着根本的不同:关联容器中的元素是按关键字来保存和访问的,而顺序容器是按它们在容器中的位置来顺序保存和访问的.两个主要的关联容器:map和set map 中的元素的是一个key-va ...

随机推荐

  1. am335x 内核频率 ddr3频率 电压调整

    由Makefile可知,SPL的入口在u-boot-2011.09-psp04.06.00.08\arch\arm\cpu\armv7\start.S中 SPL的功能无非是设置MPU的Clock.PL ...

  2. svn:ignore 的用处

    用svn管理代码,一直以来都受到一件不爽事情的困扰: 1)有些文件或文件夹不想在commit的时候看到,虽然他们是non-versioned,比如*.bak.*.class,*.scc(vss文件), ...

  3. Hdu 2236 无题II 最大匹配+二分

    题目链接: pid=2236">Hdu 2236 解题思路: 将行和列理解为二分图两边的端点,给出的矩阵即为二分图中的全部边, 假设二分图能全然匹配,则说明 不同行 不同列的n个元素 ...

  4. git error Another git process seems to be running in this repository

    How to fix error Another git process seems to be running in this repository When you use Git, you se ...

  5. 多个return和一个return

    //一个returnnamespace CleanCSharp.Methods.Dirty { class MethodExitPoints { public string GenerateAgeAp ...

  6. Laravel5.1 表单验证

    当我们提交表单时 通常会对提交过来的数据进行一些验证.Laravel在Controller类中使用了一个traint:ValidatesRequest.方便我们在控制器中使用验证器. 下面我们就来看一 ...

  7. 多线程下的神奇的IOCP

    https://blog.csdn.net/lijia626482312/article/details/40858061 一个人从接到项目到昨天终于完成,用了差不多4个月,其中各种心酸和眼泪.我的项 ...

  8. SSH电力项目一 搭建Hibernate框架

    Hibernate所需要的基本文件: ElectText.java ElecText.hbm.xml hibernate.cfg.xml 第一步:创建测试表Elec_Text: create tabl ...

  9. SQL使用union合并查询结果(转载)

    1.UNION的作用  UNION 指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION 的一 ...

  10. Web端测试

    一.功能测试 1.链接测试      1)所有链接是否按指示的那样,链接正确?      2)所有链接是否存在?      3)保证Web应用系统上没有孤立的页面? 在线链接测试地址:http://v ...