您的位置:首页 > 编程语言 > C语言/C++

重新学习《C++Primer5》第11章-关联容器

2016-05-18 11:11 330 查看

11.1 使用关联容器

void Chapter11In1()
{
/*vector和map的不同:vector相当于一个数组,采用顺序存储
优点:不需要分配存储空间也可以向数组一个存取,并且可以动态操作,节省空间
缺点:插入、删除操作效率低,添加只能在最后
map由一个健值和一个映照数据一一对应关系组成,数据结构采用红黑树实现,健值不允许重复,比较元素只对健值比较
list:采用链式存储,存储不连续,可在两端操作push,pop
缺点:不支持[]取操作,相对vector占用内存多
deque:综合了vector和list优缺点
*/
string word;
map<string,int> word_count;
while(cin>>word)
{
transform(word.begin(),word.end(),word.begin(),tolower);
for(string::size_type i=0;i!=word.size();i++)
{
if(ispunct(word[i]))
{

word.erase(i);
i--;
}
}
word_count[word]++;
}
for(map<string,int>::iterator it=word_count.begin();it!=word_count.end();it++)
{
cout<<it->first<<":"<<it->second<<endl;
}
}


11.2 关联容器概述

1.multi允许重复

//11.2.1
void Chapter11In2To1()
{
//map和set的区别:set是集合,不允许重复
map<string,vector<string>> Famliy;
int choose,count;
string xm,na;
vector<string> name;
while(1)
{
cout<<"*   1:添加新的家庭   *"<<endl;
cout<<"*   2:向已有家庭添加成员   *"<<endl;
cin>>choose;
switch(choose)
{
case 1:
name.clear();
cout<<"请输入家庭姓名和成员个数:"<<endl;
cin>>xm;
cin>>count;
cout<<"请输入成员名字:"<<endl;
for(int i=0;i<count;i++)
{

cin>>na;
name.push_back(na);
}
Famliy[xm]=name;
break;
case 2:
cout<<"请输入要添加的家庭姓名:"<<endl;
cin>>xm;
name=Famliy[xm];
cout<<"请输入要添加的成员名字:"<<endl;
cin>>na;
name.push_back(na);
Famliy[xm]=name;
break;
default:
for(map<string,vector<string>>::iterator it=Famliy.begin();it!=Famliy.end();it++)
{
cout<<it->first<<":";
for(int i=0;i!=it->second.size();i++)
cout<<it->second[i]<<" ";
cout<<endl;
}

cout<<"输入错误,请重新输入"<<endl;
continue;
}
}
}


2.传递给排序算法的可调用对象,必须满足与关联容器中关键字一样的类型要求

有序容器关键字类型

自定义比较操作必须在关键字类型上定义一个严格弱排序,在实际编程中,如果一个类型定义了“行为正常”的<运算符,则它可以用作关键字类型。

multiset<Sales_data,decltype(compareIsbn*)>bookstore(compareIsbn);


3.pair类型

11.3关联容器操作

set<string>::vaule_type s1;
set<string>::key_type s2;//s1和s2都为string
map<string,int>::value_type v1;//v1是pair
map<string,int>::key_type v2;//v2是string
map<string,int>::mapped_type v3;//v3是int


11.3.1 关联容器迭代器

1.map的value_type是pair类型,可以改变pair的值,但不能改变关键字成员的值。

auto it=word_count.begin();
it->first="new key";//错误:关键字是const
++it->second;//正确


2.set的迭代器都是const类型

3.通常不对关联容器使用泛型算法。

对于修改或重排容器元素的算法,set元素是const,map的key是const,所以不能用

对于只读元素算法,需要搜索序列,一般不用。它自己会定义比如find成员,这种专用的find会比泛型快得多。

如果要对一个关联容器使用泛型算法,要么将它当中源序列,要么当中目的位置。比如copy,

//11.3.1
void Chapter11In2To3()
{
pair<string,int> pair1;
vector<pair<string,int>> xulie;
string str;
int no;
while(cin>>str)
{
cin>>no;
pair<string,int> pair (str,no);
pair1=make_pair(str,no);
pair1.first=str;
pair1.second=no;
xulie.push_back(pair1);
}
}
//11.3.1
void Chapter11In3To1()
{
//mapped_type代表每个关键字对应的类型test(vector),key_type代表此容器类型的关键字类型test(int),value_type代表pair<const key_type,mapped_type>
//map<int,vector<int>> test;
//auto map_it=test.cbegin();//cbegin()返回值是const类型,只读迭代器,不可以修改
//while(map_it!=test.cend())
//{

//}

multiset<string> c;
vector<string> v;
copy(c.begin(),c.end(),inserter(v,v.end()));
}


13.3.2 添加元素

1.set插入操作:会去重复元素

2.map插入操作:以pair的形式插入

word_count.insert({word,1});
word_count.insert(make_pair(word,1));
word_count.insert(pair<string,int>(word,1));


3.insert的返回值为pair,first成员为插入位置迭代器,second为bool,表示是否插入成功

while(cin>>word)
{
auto ret=word_count.insert({word,1});
if(!ret.second)
++ret.first->second;
}


13.3.3删除元素

1.关联容器自带的erase

接受一个key_type参数,返回实际删除的数量;

对允许重复关键字的容器,删除元素的数量可能大于1;

13.3.4map的下标操作

1.使用下标赋值,如果没有这个关键字则会添加并初始化。为了防止这种情况,可以使用c.at(k),不存在元素会抛出异常

13.3.5访问元素

1.map使用find代替下标

if(word_count.find("foobar")==word_count.end())


2.在multi中查找元素

string search_item("Alain");
auto entries=authors.count(search_item);
auto iter=authors.find(search_item);
while(entries)
{
cout<<iter->second<<endl;
++iter;
--entries;
}


3.使用lower_bound和upper_bound来解决上述问题

for(auto beg=authors.lower_bound(search_item),end=authors.upper_bound(search_item);beg!=end;++beg)
{
cout<<beg->second;
}


4.equal_range函数

返回pair,first为第一个和关键字匹配的元素,second为最后一个与关键字匹配的元素

for(auto pos=authors.equal_range(search_item);pos.first!=pos.second;++pos.first)
{}


13.3.6 一个转换单词的map

void Chapter11In3To6()
{
ifstream in("test.txt");
if(!in.is_open())
{
cout<<"open error"<<endl;
exit(1);
}
map<string,string> mapt;
char line[20];
string linestr;
string key;
string value;
while(!in.eof())
{
in.getline(line,20);
linestr=line;
key=linestr.substr(0,linestr.find(" "));
value=linestr.substr(linestr.find(" ")+1,linestr.length()-linestr.find(" ")+1);
mapt[key]=value;
}
cout<<"请输入一段简写字符串"<<endl;
string str;
while(cin>>str)
{
auto map_it=mapt.find(str);
if(map_it!=mapt.end())
cout<<map_it->second<<'\t';
else
cout<<str<<'\t';
}
}


11.4 无序容器

1.使用哈希函数和关键字类型的==运算符组织

2.无序容器提供了与有序容器相同的操作,可以用一个无序容器替换对于的有序容器,反之亦然;

3.管理桶

桶接口

桶迭代

哈希策略

4.无序容器对关键字类型的要求
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: