11.24
若key为0的键值对不存在,则将该键值对创建
若存在,则重新赋值为1
11.25
下标越界,程序运行到此位置会停止,但编译器不会别报错
11.26
#include <iostream>
#include<set>
#include<vector>
#include<map>
#include<fstream>
using namespace std;
int main()
{
map<string, size_t> word_count;
string word;
ifstream ifs("C:\\study\\c++test\\endless.txt");
while(ifs>>word){
auto ret = word_count.insert({word,1});
if(!ret.second){
++ret.first->second;
}
}
//返回关键字的值是1
cout<<word_count.at("zhen");
//解引用迭代器,返回map的value_type
auto iter=word_count.begin();
cout<<iter->first<<" :"<<iter->second<<endl;
return 0;
}
11.27
若是元素不重复的关联容器,使用find较为方便
但是若是允许重复的关联容器,使用count计数后再进行遍历较为方便
11.28
#include <iostream>
#include<set>
#include<vector>
#include<map>
#include<fstream>
using namespace std;
int main()
{
//11.28
string str="leifeng";
vector<int> v = {1,2,3,4};
map<string,vector<int>> map_1128;
map_1128.insert({str,v});
//调用find返回一个迭代器
auto iter_1128 = map_1128.find(str);
//遍历
cout<<iter_1128->first<<" :";
for(auto c:iter_1128->second){
cout<<c<<" ";
}
return 0;
}
11.29
若是不在map中
lower_round和upper_round会返回一个相同的迭代器------都指向给定关键字的插入点,能保持容器中元素顺序的插入位置
举个例子
下面这个例子展示返回什么
#include <iostream>
#include<set>
#include<vector>
#include<map>
#include<fstream>
using namespace std;
int main()
{
multimap<string,string> mmap;
mmap.insert({"feng","hao"});
mmap.insert({"zhen","fei"});
mmap.insert({"liu","fei"});
mmap.insert({"zhen","fei"});
mmap.insert({"zhe","fei"});
mmap.insert({"zhen","mei"});
mmap.insert({"hai","fei"});
auto index=mmap.count("zhen");
cout<<index<<endl;
auto beg = mmap.lower_bound("kk");
auto en = mmap.upper_bound("kk");
//此时kk可以插入的位置为
cout<<"-------"<<endl;
cout<<beg->first<<endl;
cout<<en->first<<endl;
cout<<"-------"<<endl;
for(auto pos = mmap.equal_range("zhen");pos.first!=pos.second;pos.first++){
cout<<pos.first->first<<" :"<<pos.first->second<<endl;
}
return 0;
}
[图片上传失败...(image-f012f4-1558771487431)]
但是,这是在能在序列中见插入的情况,若是只能在末尾插入,则程序显示插入位置会出现问题
equal_range会返回一个pair,pair的两个位置是相同的,都指向能插入的位置,基本与lower_bound和upper_bound雷同,这里不在展示
11.30
pos代表使用返回的pair
pos.first表示返回的pair中的key值,就是第一个匹配的元素
pos.first->second就是第一个key的value值
11.31-11.32
不存在在map中也可以运行,因为multmap是重复有序的,所以添加元素的时候已经算是按照字典排序好
#include <iostream>
#include<set>
#include<vector>
#include<map>
#include<fstream>
using namespace std;
int main()
{
//11.31
multimap<string,string> map_1131;
map_1131.insert({"JJ","jiangnan"});
map_1131.insert({"ZJL","bulageguangchang"});
map_1131.insert({"WYT","tufanhaoxiangni"});
map_1131.insert({"JJ","buchao"});
map_1131.insert({"JJ","meirenyu"});
map_1131.insert({"ZJL","yequ"});
map_1131.erase("xxx");
for(auto c: map_1131){
cout<<c.first<<" :"<<c.second<<endl;
}
return 0;
}
11.33
书上版本
#include <iostream>
#include<set>
#include<vector>
#include<map>
#include<fstream>
#include<string>
#include<strstream>
#include<sstream>
using namespace std;
map<string,string> bulidMap(ifstream &);
const string & transform(const string &, const map<string,string> &);
void word_transform(ifstream &,ifstream &);
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<<transform(word,trans_map);//打印输出
}
}
cout<<endl;
}
}
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){//查看当前单词是否有转换规则
trans_map[key] = value.substr(1); //跳过前导空格
}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);//寻找s是否在转化规则中
if(map_it!= m.cend()){
return map_it->second;//在转化规则中就返回mapped_type
}else{
return s;
}
}
int main()
{
ifstream ifs_t("C:\\study\\c++test\\endless.txt");
ifstream ifs_r("C:\\study\\c++test\\rule.txt");
word_transform(ifs_r,ifs_t);
return 0;
}
11.34
若是遇到没有转换规则的单词就会插入一个在元素转换map中元素,再次遇到,就会插入一个空格
11.35
若是多次出现同个转换规则
trans_map[key] = value.substr(1); 会保存最后一次的转换规则
trans_map.insert({key, value.substr(1)});只保存第一次插入时候的转换规则
11.36
没有对应规则
则规则map的该key对应的value值为空,它的size()也为0,会抛出("no rule for"+key)
11.37
无序容器性能好
有序容器按照字典有序
11.38
#include <iostream>
#include<set>
#include<vector>
#include<map>
#include<fstream>
#include<unordered_map>
using namespace std;
int main()
{
set<int> iset={0,1,2,3,4,5,6,7,8,9,10};
set<int>::iterator set_it=iset.begin();
cout<<*set_it<<endl;
unordered_map<string, size_t> word_count;
string word;
ifstream ifs("C:\\study\\c++test\\endless.txt");
while(ifs>>word){
word_count[word]++;
}
for(auto c:word_count){
cout<<c.first<<" : "<<c.second<<endl;
}
//创建一个指向首元素的迭代器
auto map_it = word_count.begin();
while(map_it!=word_count.end()){
cout<<map_it->first<<" occursn "<<map_it->second<<(map_it->second>1?" times":" time")<<endl;
map_it++;
}
cout<<"-----"<<endl;
//在第22个桶
cout<<word_count.bucket("pic")<<endl;
//桶的平均元素数量
cout<<word_count.load_factor();
return 0;
}
网友评论