C++ Primer 第10章 关联容器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fuck51cto/article/details/80921273

C++ Primer 第10章 关联容器



关联容器和顺序容器的本质差别在于:关联容器通过键(key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素。
两个基本的关联容器类型是 map set。map 的元素以键-值(key-value)对的形式组织:键用作元素在 map 中的索引,而值则表示所存储和读取的数据。set 仅包含一个键,并有效地支持关于某个键是否存在的查询。

如果一个键必须对应多个实例,则需使用 multimap 或 multi set,这两种类型允许多个元素拥有相同的键。

关联容器类型
map  关联数组:元素通过键来存储和读取
set 大小可变的集合,支持通过键实现的快速读取
multimap  支持同一个键多次出现的 map 类型
multiset 支持同一个键多次出现的 set 类型



2. pair 类型

pair 包含两个数据值。与容器一样,pair 也是一种模板类型。但又与之前介绍的容器不同,在创建 pair 对象时,必须提供两个类型名:pair 对象所包含的两个数据成员各自对应的类型名字,这两个类型必相同。

pair<string,string> anon;
    pair<string, int> word_count;
    pair<string,vector<int>> line;

pair类成员:first 和 second
if (author.first == "James" && author.second == "Joyce") {
cout<<author.first<<"  "<<author.second<<endl;
}


创建新的pair类型
pair<string, string> nex_auth;
string first, last;

while(cin >> first >> last) {
nex_auth = make_pair(first,last);

}





3. map 类型

map 是键-值对的集合。map 类型通常可理解为关联数组,可使用键作为下标来获取一个值。
map<string,int> word_count;  定义一个map
 
对迭代器进行解引用将获得一个 pair 对象,它的 first 成员存放键,为 const,而 second 成员则存放值
map<string,int> word_count;
word_count.insert(make_pair("a1",1));
word_count.insert(make_pair("a2",2));
word_count.insert(make_pair("a3",3));

map<string, int>::iterator map_it = word_count.begin();

cout<<map_it->first;
cout<<" "<<map_it->second;
//map_it->first = "new key"; //error

给map添加元素
word_count["Anna"] = 1;

使用下标操作符,注意使用下标,程序会先查找有没有一个下标,如果没有则新建一个
word_count["Anna"] = 1;
cout<<word_count["a1"]<<endl;
++word_count["Anna"];
cout<<word_count["Anna"]<<endl;

insert添加元素
word_count.insert(map<string,int>::value_type("jacks",2));
word_count.insert(make_pair("a3",3));

查找并读取 map 中的元素
使用下标存在一个很危险的副作用:如果该键不在 map 容器中,那么下标操作会插入一个具有该键的新元素。
map 容器提供了两个操作:count 和 find,用于检查某个键是否存在而不会插入该键。
m.count(k)  返回 m 中 k 的出现次数
m.find(k) 如果 m 容器中存在按 k 索引的元素,则返回指向该元素的迭代器。如果不存在,则返回超出末端迭代器

cout<<word_count.count("Anna")<<endl;  //返回1
cout<<word_count.count("Anna1")<<endl; //返回0

int occurs = 0;
if (word_count.count("foobar"))
occurs = word_count["foobar"];
在执行 count 后再使用下标操作符,实际上是对元素作了两次查找。如果希望当元素存在时就使用它,则应该用 find 操作。

erase 删除元素
与顺序容器一样,可向 erase 传递一个或一对迭代器,来删除单个元素或一段范围内的元素。其删除功能类似于顺序容器

从 map 对象中删除元素
m.erase(k)  从m中删除元素k,并返回删除元素的个数。
m.erase(p)  从 m 中删除迭代器 p 所指向的元素。p 必须指向 m 中确实存在的元素,而且不能等于 m.end()。返回 void
m.erase(b, e)  从 m 中删除一段范围内的元素,该范围由迭代器对 b 和 e 标记。



    cout<<word_count.erase("a3")<<endl; //返回1

int occurs = 0;
map<string,int>::iterator it = word_count.find("jacks");
if (it!=word_count.end()) {
word_count.erase(it);
}

cout<<word_count.count("jacks");  //返回0


word_count.erase(word_count.begin(),word_count.end());
cout<<word_count.size()<<endl;  //返回0

map遍历
与其他容器一样,map 同样提供 begin 和 end 运算,以生成用于遍历整个容器的迭代器。
map<string, int>::const_iterator map_it = word_count.begin();

while (map_it!=word_count.end()) {

cout<<map_it->first<<"  "<<map_it->second<<endl;
++map_it;
}



4.  set 类型

set 容器只是单纯的键的集合,与map 容器一样,set 容器的每个键都只能对应一个元素。key是唯一的。
 
vector<int> ivec;

for (int i = 0; i!=10; ++i) {
ivec.push_back(i);
ivec.push_back(i);
}
set<int> iset(ivec.begin(),ivec.end());
cout<<ivec.size()<<endl; //20
cout<<iset.size()<<endl; //

inset插入元素
set<string> set1;
set1.insert("the");
set1.insert("and");
set1.insert(ivec.begin(),ivec.end());  //插入一段元素
set的查找同map一样
  // set_it refers to the element with key == 1
     set<int>::iterator set_it = iset.find(1);
     cout << *set_it << endl;    // ok: can read the key

















 

猜你喜欢

转载自blog.csdn.net/fuck51cto/article/details/80921273