STL关联容器之multiset和multimap

1、基本概念

  由于set和map不允许插入相同的元素,stl使用multi版本来满足这一需求,原版与multi版的特性是一致的,唯一的区别是在插入这里,这引起以下几点不同:
1. []访问。set不支持[]直接对元素进行访问,其对应的multi版也是不支持的;map支持[],但是multi版是不支持的。
2. 插入返回值。不同于原始版本可能插入失败,multi版插入是必然成功的,因而返回值只返回插入后元素的迭代器。

2、综合测试

  multi版本的用法与原版是一致的,以下是测试代码:

int list[5] = {0,1,2,3,4};
multiset<int> a(list, list + 5);

cout<<"size= "<<a.size()<<endl;  //size= 5
cout<<"3 count is "<<a.count(3)<<endl; //3 count is 1

multiset<int>::iterator r= a.insert(3);
cout<<*r<<endl; //3
cout<<"3 count is "<<a.count(3)<<endl; //3 count is 2,可以插入相同的元素

a.insert(5);
cout<<"size= "<<a.size()<<endl; //size= 7

a.erase(1);
cout<<"size= "<<a.size()<<endl; //size= 6

cout<<"set list:";
for (multiset<int>::iterator iter = a.begin();iter != a.end();++iter)
{
    cout<<*iter<<" ";
}
cout<<endl;  //set list:0 2 3 3 4 5

multiset<int>::iterator temp = a.find(3);
if (temp != a.end())
{
    cout<<"find 3 in the set"<<endl;  //find 3 in the set
}

//*temp = 9; 编译不过,不能给常量赋值

temp = a.find(1);
if (temp == a.end())
{
    cout<<"not find 1 in the set"<<endl; //not find 1 in the set
}

multimap<int,string> a;
//a[1] = "a";  编译报错,键值可以相同,不能使用符号[]

a.insert(pair<int,string>(1,"a"));
a.insert(pair<int,string>(5,"b"));
a.insert(pair<int,string>(7,"c"));
a.insert(pair<int,string>(9,"x"));

pair<int,string> value(3,"d");
a.insert(value);

for (multimap<int,string>::iterator iter = a.begin();iter !=a.end();++iter)
{
    cout<<iter->first<<" "<<(iter->second).c_str()<<endl;
    /*1 a
       3 d
       5 b
       7 c
       9 x
    */
}

multimap<int,string>::iterator iter = a.find(8);
if (iter == a.end())
{
    cout<<"key 8 not find the value"<<endl;
}

iter = a.find(3);
if (iter != a.end())
{
    cout<<"key 3 find the value:"<<iter->second.c_str()<<endl;
}

//iter->first = 9; 不可更改
iter->second = "e";
cout<<iter->second.c_str()<<endl; //e

pair<int,string> temp(7,"f");
multimap<int,string>::iterator r = a.insert(temp);
cout<<a.count(7)<<endl;

for (multimap<int,string>::iterator iter = a.begin();iter !=a.end();++iter)
{
    cout<<iter->first<<" "<<(iter->second).c_str()<<endl;
    /*
       1 a
       3 e
       5 b
       7 c
       7 f
       9 x
    */
}

cout<<"*******************"<<endl;
//multimap<int,string>::iterator  x = a.lower_bound(7);
//multimap<int,string>::iterator  y = a.upper_bound(7);
multimap<int,string>::iterator  x = a.equal_range(7).first;
multimap<int,string>::iterator  y = a.equal_range(7).second;
for (multimap<int,string>::iterator iter = x;iter !=y;++iter)
{
    cout<<iter->first<<" "<<(iter->second).c_str()<<endl;
    /*
       7 c
       7 f
    */
}

cout<<"*******************"<<endl;
a.erase(7);
//a.erase(x);
for (multimap<int,string>::iterator iter = a.begin();iter !=a.end();++iter)
{
    cout<<iter->first<<" "<<(iter->second).c_str()<<endl;
    /*
       1 a
       3 e
       5 b
       9 x
    */
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值