c++中set,map用法

下面用具体的代码演示,大家可以自行体会,都比较经典

set:

#include <iostream>
#include<set>
using namespace std;
main()
{
 typedef set<int>::iterator tt;
 int a[5]={3,4,6,1,2};
 set<int> st(a,a+5);  //st是12346
 pair<tt,bool> re; 
 re = st.insert(5);  //st变成123456 
 if(re.second)
  cout<<*re.first <<"inserted"<<endl;  //输出:5inserted
 if(st.insert(5).second)  cout<<*re.first<<endl;
 else
  cout<<*re.first<<"already exist"<<endl; //5 already exist
 
 pair<tt,tt> bounds = st.equal_range(4);   
 cout<<*bounds.first<<","<<*bounds.second;  //4,5
 return 0;
}

运行结果:

5inserted
5already exist
4,5
--------------------------------
Process exited after 1.875 seconds with return value 0
请按任意键继续. . .

————————————————————————————————————

map:

#include <iostream>
#include<map>
using namespace std;
template<class Key,class Value> //模板 
ostream & operator <<(ostream & o, const pair<Key,Value> & p)
{
 o <<"("<<p.first <<","<<p.second <<")";
 return o; 
}
main()
{
 typedef map<int,double,less<int> > mm;
 mm pairs;
 cout<<"1)"<<pairs.count(15) <<endl;
 pairs.insert(mm::value_type(15,2.6));
 pairs.insert(make_pair(15,56.8)); //make_pair生成一个pair对象,map中不能有相同first,所以这句实现不了
 cout<<"2)"<<pairs.count(15) <<endl;
 pairs.insert(mm::value_type(20,5.6));
 mm::iterator i;
 cout<<"3)";
 for(i = pairs.begin();i!= pairs.end();i++)
  cout<<*i<<",";
 cout<< endl;
 cout<<"4)";
 int n = pairs[40] ;   //如果没有关键字为40的元素,则插入一个 
 for(i = pairs.begin();i!= pairs.end();i++)
  cout<<*i<<",";
 cout<< endl;
 cout<<"5)";
 pairs[15] = 6.59;   //把关键字为15的元素值改为6.59 
 for(i = pairs.begin();i!= pairs.end();i++)
 cout <<*i<<","; 
 return 0;
}

运行结果:

1)0
2)1
3)(15,2.6),(20,5.6),
4)(15,2.6),(20,5.6),(40,0),
5)(15,6.59),(20,5.6),(40,0),
--------------------------------
Process exited after 1.96 seconds with return value 0
请按任意键继续. . .

例子:学生信息系统


#include <iostream>
#include<map> //使用multimap需要包含此头文件 
using namespace std;
class Cstudent
{
public:
  struct Cinfo //类的内部还可以定义类 
  {
   int id;
   string name;
   };
  int score;
  Cinfo info; // 学生的其他信息 
}; 
typedef multimap<int, Cstudent::Cinfo> mm;
main()
{
 mm mp;
 Cstudent st;
 string cmd;
 while( cin>> cmd){
  if(cmd == "Add"){
   cin>> st.info.name >>st.info.id>>st.score;
   mp.insert(mm::value_type(st.score,st.info));
  }else if(cmd == "Query"){
   int score;
   cin>>score;
   mm::iterator p = mp.lower_bound(score);
   if(p!= mp.begin()){
    --p;
    score = p->first;//比要查询分数低的最高分 
    mm::iterator maxp = p;
    int maxId = p->second.id;
    for(;p!=mp.begin()&&p->first==score;--p)
    {
    //遍历所有成绩和score相等学生 
     if(p->second.id> maxId)
     {
      maxp = p;
      maxId = p->second.id;
     }
     } 
    if(p->first == score){
    //如果上面循环是因为 p== mp.begin() 
    //而终止,则指向的元素还要处理 
     if(p->second.id > maxId){
      maxp = p;
      maxId = p->second.id;
     }
    }
    cout<<maxp->second.name<<
    " "<<maxp->second.id<<" "
    <<maxp->first<< endl; 
   }
   else
   //lower_bound结果是begin,说明没人分数比查询分数低 
    cout<< "Nobody"<< endl;
  } 
 } 
 return 0;
}

mp.insert(make_pair(st.score,st.info)); 也可以
上面的multimap与map的区别是,它的first可以是相同的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值