Chinaunix首页 | 论坛 | 博客
  • 博客访问: 519141
  • 博文数量: 96
  • 博客积分: 2102
  • 博客等级: 上尉
  • 技术积分: 1695
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:12
文章分类

全部博文(96)

文章存档

2014年(2)

2012年(94)

分类: C/C++

2012-06-09 09:54:39

关联容器和顺序容器的本质区别:关联容器是通过键存取和读取元素、顺序容器通过元素在容器中的位置顺序存储和访问元素

pair类型

:初始化-------在头文件utility中

pair p1;                       创建一个pair对象,两个元素的类型分别是T1,T2类型,采用初值进行初始化

pair p1(v1,v2);             创建一个pair对象,两个元素的类型分别是T1,T2类型,采用v1,v2分别进行初始化

make_pair(v1,v2);                     以v1、v2的只进创建一个pair对象,类型分别是v1、v2的类型

p.first                                      返回p中第一个公有数据成员

p.second                                 返回p中第二个公有数据成员

例子程序


  1. #include<iostream>
  2. #include<string.h>
  3. #include<vector>
  4. #include<map>
  5. #include<utility>
  6. using std::cout;
  7. using std::string;
  8. using std::cin;
  9. using std::endl;
  10. using std::pair;
  11. using std::make_pair;
  12. using std::vector;
  13. using std::map;
  14. int main()
  15. {
  16.     pair<string,int> next_auth;

  17.     vector<pair<string,int> > all;
  18.     vector<pair<string,int> >::iterator iter;

  19.     string first;
  20.     int last;
  21.     while(cin>>first>>last)
  22.         all.push_back(make_pair(first,last));

  23.     cout<<"the element of the vector "<<endl;
  24.     for(iter=all.begin();iter!=all.end();iter++)
  25.         cout<<iter->first<<" "<<iter->second<<endl;
  26.     return 0;
  27. }
结果:

关联容器

对于关联容器不提供front、push_front、pop_front、back、push_back以及pop_back,此外对于关联容器不能通过容器大小来定义,因为这样的话将无法知道键所对应的值什么。对于关联容器有:map、set、multimap、multiset

@ map

:关联数组,通过键来存储和读取

:初始化

           map   m1              创建一个名m的空的map对形象,对其键和值的类型分别为k、v

           map  m(m2)         创建m2的副本m,m与m2的类型必须相同的键值类型和值类型

           map   m(b,e)         创建map类型的对象m,存储迭代器b和e标记范围内所有元素的副本,元素的类型必须转换为pair

:对于map中的键值要求类型一致并且有一个相关的比较函数,在默认情况下可以理解为键类型数据上的“小于”关系。-----严格的“弱”排序

:map定义的类型

          map::key_type            在map中的做索引的键的类型

          map::mapped_type      在map中的键所关联的值的类型

          map::value_type         一个pair类型,它的first元素具有const map::key_type类型,它的second元素则为map::mapped_type 

对map迭代器解引用将产生pair类型它的第一个元素是const类型的,第二个元素的成员可以修改

:给map添加成员

         1>使用下标访问map对象:在map对象查找键值为x的元素如果找到则返回它的值(类型是map::mapped_type),否则插入一个新的对象键值为x,值为map中v的默认初值

         2>使用Insert对map进行插入:

              m .insert(e)                e是一个用在m上的value_type类型的值,如果e.first不在m中则插入一个值为.second的新元素,否则该键值在m中已经存在则保持不变,该

                                               函数返回一个pair新类型,包含一个指向键值为e.first的元素的map迭代器,以及一个bool类型的对象,表示是否插入该元素               

              m.insert(beg,end)       插入beg、end标记范围内的元素,如果该元素的m.first已经存在则不插入否则插入。返回void类型

              m.insert(iter,e)           如果e.first不在m中,则创建新元素,并以迭代器iter为起点搜索新元素的存储位置,否则返回一个迭代器,指向m中具有的给定键的元素。

        3>查找和读取map中的元素:

            m.count(k)                 返回k在m中出现的次数,在map中只是返回0、1

            m.find(k)                    如果k在m中的键值存在则返回相应的迭代器,否则返回超出来末端迭代器

       4>从map中删除元素

           使用erase与顺序容器功能一样

例子程序


  1. #include<iostream>
  2. #include<string.h>
  3. #include<vector>
  4. #include<map>
  5. #include<utility>
  6. using std::cout;
  7. using std::string;
  8. using std::cin;
  9. using std::endl;
  10. using std::pair;
  11. using std::make_pair;
  12. using std::vector;
  13. using std::map;
  14. int main()
  15. {
  16.     pair<string,int> next_auth;

  17.     vector<pair<string,int> > all;
  18.     vector<pair<string,int> >::iterator iter;

  19.     string first;
  20.     int last;
  21.     cout<<"the element of the map "<<endl;
  22.     map<string,int> ok;//对于map中值的存储是根据若排序进行的,因此,ok的begin的对象始终是jk--3/2
  23.     ok["jk"]=3;
  24.     ok["jk"]=2;
  25.     ok["ok"]=4;
  26.     if(!ok.empty()) {
  27.         map<string,int>::iterator map_it=ok.begin();
  28.         cout<<map_it->first<<" "<<map_it->second<<endl;
  29.     }
  30.     string key;
  31.     while(cin>>key)//对于map中所有值得初值是1,如果多次出现相同的string类型的值,则从1开始累加
  32.         ++ok[key];

  33.     ok.insert(map<string,int>::value_type("anna",67));
  34.     ok.insert(make_pair("gyeve",5));

  35.     while(cin>>key)
  36.     {
  37.         pair<map<string,int>::iterator,bool > ret = ok.insert(make_pair(key,1));
  38.         if(!ret.second)
  39.             ++ret.first->second;
  40.     }
  41.     cin>>key;
  42.     cout<<"the avaiable:"<<endl;
  43.     cout<<ok.count(key)<<endl;
  44.     map<string,int>::iterator it=ok.find("gyeve");
  45.     if(it!=ok.end()){
  46.         cout<<it->second<<endl;
  47.         ok.erase(it);
  48.      cout<<ok.count("gyeve")<<endl;
  49.     }
  50.     /*查找map中的值:下标、count、find 其中如果在使用下标查找的过程中没有该值就会添加*/

  51.     return 0;
  52. }
结果:

@set

:大小可变的集合,支持通过键值实现的快速读取。对于单纯想知道一个值是否存在则set容器最适用

:在set容器中value_type不是pair类型,而是与key_value的类型相同,它的键值类型也是唯一的且不能修改

:在set也支持find、count、insert、erase操作

例子程序


  1. #include<set>
  2. #include<vector>
  3. #include<iostream>
  4. #include<string>
  5. using std::cin;
  6. using std::cout;
  7. using std::set;
  8. using std::vector;
  9. int main()
  10. {
  11.     vector<int> vec;
  12.     for(vector<int>::size_type i=0;i<10;i++)
  13.     {
  14.         vec.push_back(i);
  15.         vec.push_back(i);
  16.     }
  17.     set<int> iset(vec.begin(),vec.end());
  18.     cout<<"the len of the vector "<<vec.size()<<std::endl;
  19.     cout<<"the len of the set "<<iset.size()<<std::endl;
  20.     return 0;
  21. }
结果:

@multimap

:支持同一个键多次出现的map类型,multimap不能支持下标操作

@multiset

:支持同一个键值多次出现的set类型

在multimap、multiset 中查找元素------注意对多情况的处理

1>使用find、count操作

-----count将对应键值的数目反馈

-----将与键值第一个匹配的元素的迭代器反馈

代码


  1. #include<map>
  2. #include<iostream>
  3. #include<string>
  4. #include<utility>
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::string;
  9. using std::multimap;
  10. using std::pair;

  11. typedef multimap<string,string>::iterator author_it;

  12. int main()
  13. {
  14.     string name,book;
  15.     multimap<string,string> author;

  16.     author.insert(std::make_pair(string("Barth"),string("Sot-weed, Factor")));
  17.     author.insert(std::make_pair(string("Barth"),string("Lost in the Funhouse")));
  18.     author.insert(std::make_pair(string("gyeve"),string("old and yonger")));
  19.     author.insert(std::make_pair(string("susan"),string("black and red")));
  20.     author.insert(std::make_pair(string("gyeve"),string("sky girl")));
  21.     author.insert(std::make_pair(string("susan"),string("goods")));
  22.     author.insert(std::make_pair(string("susan"),string("data fame")));

  23.     string key("Barth");
  24.     multimap <string,string>::size_type cnt;
  25.     cnt = author.erase(key);
  26.     cout<<"delete the :"<<key<<" is : "<<cnt<<endl;

  27.     cout<<"find the key form author (count,find)"<<endl;
  28.     cnt = author.count("susan");
  29.     multimap<string,string>::iterator it = author.find("susan");
  30.     for(multimap<string,string>::size_type in=0;in!=cnt;in++,it++)
  31.         cout<<"the key :"<<it->first<<" the value : "<<it->second<<endl;
  32. return 0;
  33. }
结果:

2>使用lower_bound、upper_bound

-----m.lower_bound(k)返回一个迭代器,指向键值不小于k的第一个元素

-----m.upper_bound(k)返回一个迭代器,指向键值不大于k的第一个元素

例子程序

  1. #include<map>
  2. #include<iostream>
  3. #include<string>
  4. #include<utility>
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::string;
  9. using std::multimap;
  10. using std::pair;

  11. typedef multimap<string,string>::iterator author_it;

  12. int main()
  13. {
  14.     string name,book;
  15.     multimap<string,string> author;

  16.     author.insert(std::make_pair(string("Barth"),string("Sot-weed, Factor")));
  17.     author.insert(std::make_pair(string("Barth"),string("Lost in the Funhouse")));
  18.     author.insert(std::make_pair(string("gyeve"),string("old and yonger")));
  19.     author.insert(std::make_pair(string("susan"),string("black and red")));
  20.     author.insert(std::make_pair(string("gyeve"),string("sky girl")));
  21.     author.insert(std::make_pair(string("susan"),string("goods")));
  22.     author.insert(std::make_pair(string("susan"),string("data fame")));
  23. cout<<"find the key form author (lower_bound,upper_bound)"<<endl;
  24.     author_it beg = author.lower_bound(string("susan"));
  25.     author_it end = author.upper_bound(string("susan"));
  26.     while(beg!=end)
  27.     {
  28.         cout<<"the key :"<<beg->first<<" the value : "<<beg->second<<endl;
  29.         beg++;
  30.     }
  31. return 0;
  32. }
结果:

3>使用equal_range

-----m.equal_range(k)返回一个迭代器的对象,它的first成员等价于m.lower_bound(k)、他的second成语等价于m.upper_bound(k)

例子程序

  1. #include<map>
  2. #include<iostream>
  3. #include<string>
  4. #include<utility>
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::string;
  9. using std::multimap;
  10. using std::pair;

  11. typedef multimap<string,string>::iterator author_it;

  12. int main()
  13. {
  14.     string name,book;
  15.     multimap<string,string> author;

  16.     author.insert(std::make_pair(string("Barth"),string("Sot-weed, Factor")));
  17.     author.insert(std::make_pair(string("Barth"),string("Lost in the Funhouse")));
  18.     author.insert(std::make_pair(string("gyeve"),string("old and yonger")));
  19.     author.insert(std::make_pair(string("susan"),string("black and red")));
  20.     author.insert(std::make_pair(string("gyeve"),string("sky girl")));
  21.     author.insert(std::make_pair(string("susan"),string("goods")));
  22.     author.insert(std::make_pair(string("susan"),string("data fame")));

  23.     cout<<"find the key form author (equal_range)"<<endl;
  24.     pair< multimap<string,string>::iterator, author_it > result;
  25.     result = author.equal_range(string("susan"));
  26.     while(result.first!=result.second)
  27.     {
  28.             cout<<"the key :"<<result.first->first<<" the value : "<<result.first->second<<endl;
  29.             result.first++;
  30.     }
  31.     return 0;
  32. }
结果:
阅读(2325) | 评论(0) | 转发(0) |
0

上一篇:C 的顺序容器

下一篇:‘复制人’

给主人留下些什么吧!~~