set与map容器

        分类:             C/C++              2013-08-25 19:21     560人阅读     评论(0)     收藏     举报    

首先来看看set集合容器:

set集合容器实现了红黑树的平衡二叉树数据结构,在插入元素时它会自动调整二叉树的排列,把该元素放到适当的位置,并且保证左右子树平衡。平衡二叉检索树采用中序遍历算法。

对于set,vector,map等等,它们的前向迭代器定义是这样的(以set为例):

set<int>::iterator it;

for(it=s.begin();it!=s.end();it++){}

那么反向迭代器呢?

set<int>::reverse_iterator rit;

for(rit=s.rbegin();rit!=s.rend();rit++){}

常用方法:insert(),erase(),find(),clear()

find()方法是查找元素方法,如果找到就返回该元素迭代器的位置,否则就返回最后一个元素后面的一个位置。

这样来使用:

it=s.find(5);

if(it!=s.end()) {}

else            {}

关于比较函数:

如果元素不是结构体,而是基本数据类型,那么就自己定义一个比较函数:

  1. struct cmp
  2. {
  3. bool operator()(const int &a,const int &b)
  4. {
  5. return a>b;
  6. }
  7. };
  8. int main()
  9. {
  10. set<int,cmp> s;
  11. for(int i=1;i<10;i++)
  12. s.insert(i);
  13. set<int,cmp>::iterator it;
  14. for(it=s.begin();it!=s.end();it++)
  15. cout<<*it<<" ";
  16. return 0;
  17. }
struct cmp
{
bool operator()(const int &a,const int &b)
{
return a>b;
}
}; int main()
{
set<int,cmp> s;
for(int i=1;i<10;i++)
s.insert(i);
set<int,cmp>::iterator it;
for(it=s.begin();it!=s.end();it++)
cout<<*it<<" ";
return 0;
}

如果元素是结构体,那么比较函数直接可以写在结构体里面。

  1. struct Student
  2. {
  3. string name;
  4. double score;
  5. bool operator < (const Student &a) const
  6. {
  7. return a.score<score;
  8. }
  9. };
  10. set<Student> s;
struct Student
{
string name;
double score;
bool operator < (const Student &a) const
{
return a.score<score;
}
}; set<Student> s;

对于set容器来说,它不能有关键字相同的元素同时存在,如果要保留相同的元素,就用multiset多重集合容器。

其基本操作跟set集合容器差不多一样。定义方法是:multiset<string> ms;

map映照容器:

map映照容器的元素数据是由一个键值和一个映照数据组成的,键值与映照数据之间具有一一对应关系。map也是用红黑树实现

的。同set一样,map不允许插入元素键值相同,而multimap多重应照集合可以允许键值重复。

使用方法:

  1. int main()
  2. {
  3. map<string,double> m;
  4. m["Jack"]=98.0;
  5. map<string,double>::iterator it;
  6. for(it=m.begin();it!=m.end();it++)
  7. cout<<(*it).first<<" "<<(*it).second<<endl;
  8. return 0;
  9. }
int main()
{
map<string,double> m;
m["Jack"]=98.0;
map<string,double>::iterator it;
for(it=m.begin();it!=m.end();it++)
cout<<(*it).first<<" "<<(*it).second<<endl;
return 0;
}

主要方法有:insert(),clear(),erase(),find()等等,基本跟set一样。对于multimap使用方法跟map差不多一样。

std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型。

例如 std::pair<int,float> 或者 std::pair<double,double>等。pair实质上是一个结构体,其主要的两个成员变量是first和second,这两个变量可以直接使用。初始化一个pair可以使用构造函数,也可以使用std::make_pair函数。

make_pair函数的定义如下:

  1. template pair make_pair(T1 a, T2 b)
  2. {
  3. return pair(a, b);
  4. }
template pair make_pair(T1 a, T2 b)
{
return pair(a, b);
}

所以m.insert(pair<string,double>("Luce",88.5));

与 m.insert(make_pair("Luce",88.5)); 是同样的效果。

示例:

  1. int main()
  2. {
  3. typedef pair<string,double> lesson;
  4. typedef multimap<string,lesson> stu;
  5. stu a;
  6. a.insert(make_pair("Jack",lesson("math",90.5)));
  7. a.insert(make_pair("Jack",lesson("history",85.5)));
  8. a.insert(make_pair("Luce",lesson("math",99.0)));
  9. map<string,lesson>::iterator it;
  10. for(it=a.begin();it!=a.end();it++)
  11. cout<<(*it).first<<" "<<(*it).second.first<<" "<<(*it).second.second<<endl;
  12. return 0;
  13. }

set和map和pair 转自ACdreamers的更多相关文章

  1. C++ map.insert: pair和make_pair区别

    C++ map.insert: pair和make_pair区别 \*********************************\ map<uint32_t, string> tem ...

  2. STL之map与pair与unordered_map常用函数详解

    STL之map与pair与unordered_map常用函数详解 一.map的概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称 ...

  3. c++map按value排序--将map的pair对保存到vector中,然后写比较仿函数+sort完成排序过程。

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行 ...

  4. CF988 C. Equal Sums【map+pair/hash/任选两个序列,两个序列都除去他们中的一个数,使的总和相同】

    [链接]:CF988C [题意]:在n个序列中任选两个序列,两个序列都除去他们中的一个数,使的总和相同 [分析]:map<int,pair<int,int> > mp,从0~m ...

  5. Codeforces Round #486 (Div. 3) C "Equal Sums" (map+pair<>)

    传送门 •题意 给k个数列,从中k个数列中找出任意2个数列 i ,j 使得数列i删除第x个数,和数列j删除第y个数的和相等 若存在,输出 i ,x 和 j,y •思路 每个数列之间的联系为数列的和之间 ...

  6. map set区别

    STL中的容器有顺序容器 (vector,list,deque),关联容器(map,set)还有一些其他容器.根据不同的场合选择不同的容器,会有意想不到的收获. Map是单词mapping(映射)的缩 ...

  7. codeforces 651C(map、去重)

    题目链接:http://codeforces.com/contest/651/problem/C 思路:结果就是计算同一横坐标.纵坐标上有多少点,再减去可能重复的数量(用map,pair存一下就OK了 ...

  8. C++回顾map的用法

    map<T, T>是C++的STL中存储key-value键值对数据结构的最基础的模板类,相对于multimap可以重复的key值,map的key是非重复的. C++的reference这 ...

  9. 4.3 map和multimap

    使用map multimap必须包含头文件map *:multimap 1)multimap定义 template<class Key,class Pred=less<Key>,cl ...

随机推荐

  1. JS中基本window.document对象操作以及常用事件!

    一.找到元素 1.document.getELementById("id"):根据id找,最多找一个. var a=document.getELementById("id ...

  2. Windows与Linux共享文件夹互相访问

    [原文]  首先安装并配置软件samba [html] view plain copy sudo yum install samba samba-client vim /etc/samba/smb.c ...

  3. log4j常用配置以及日志文件保存位置

    log4j.rootLogger=INFO,CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender ...

  4. Java高级之虚拟机加载机制

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 1.0版本:2016-05-21 SubClass!! 执行结果说明一个问题:子类调用父类变量的时候 ...

  5. OpenSSL-for-IOS

    http://download.csdn.net/download/kokjuis/9723109

  6. [LeetCode]题解(python):031-Next Permutation

    题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges nu ...

  7. svn利用TortoiseSVN忽略文件或文件夹

    忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有 自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作. ...

  8. 关于前后台交互生成json区别

    如何返回[object{xx:{}}]这种数组型json在服务器端return $arr[]=m;像这种都可以产生[Object { 0="9", 1="8", ...

  9. get 与 post

    <form action="Default.aspx" method="get"> get  服务器端 用request.querystring来获 ...

  10. 超爱http://www.runoob.com/菜鸟编程

    超爱http://www.runoob.com/菜鸟编程 http://www.runoob.com/