std::set 和 std::map 的基本用法

 

std::set成员函数及简要使用方法
函数 声明 说明
insert pair<iterator,bool> insert(const value_type& x)
iterator insert(iterator position, const value_type& x) 1、向集合中添加一个元素
2、在迭代器指向的位置上放置指定的元素
count size_type count(const key_type& x) 计算元素在容器中的个数,对于std::set为1(存在)或者0(不存在)。可用于判断元素是否存在
find 查找指定
empty bool empty() 判断当前容器是否为空
size size_type size() 取得当前容器内元素个数
clear void clear() 清空当前容器
begin 声明
end 声明
rbegin 声明
rend 声明
erase void erase(iterator position)
void erase(iterator first, iterator last)
size_type erase(const key_type& x)
begin 声明
begin 声明
begin 声明
begin 声明
begin 声明
begin 声明
begin 声明
清空当前当前容器
如果当前容易中的元素是自己new出来的对象的指针,那么在调用clear函数之前,需要自己先释放掉这部分内存。比如进行如下的操作。
myclass *p1 = new myclass();
myclass *p2 = new myclass();
myclass *p3 = new myclass();
std::set<myclass *> set_class;
set_class.insert(p1);
set_class.insert(p2);
set_class.insert(p3);
set_class.clear();
如果程序只进行如上的步骤,那么p1、p2、p3其实没有别真正的释放,如果是在SVR中运行的话,会造成内存泄露。实际应该如下所示。
myclass *p1 = new myclass();
myclass *p2 = new myclass();
myclass *p3 = new myclass();
std::set<myclass *> set_class;
set_class.insert(p1);
set_class.insert(p2);
set_class.insert(p3);
for (std::set<myclass *>::iterator it = set_class.begin(); it != set_class.end(); it++)
delete *it;
set_class.clear();
对于erase操作也是如此。在删除元素之前,如有必要先自己释放内存。
获取数据
1、std::set不提供下表操作符;
2、如果只是判断元素是否存在,可以使用count函数检查返回值;
3、如果需要获取元素值,可使用迭代器。*iterator就是该迭代器指向的值。
std::set<std::string> set_limit;
set_limit.insert(“User@123”);
set_limit.insert(“User@124”);
set_limit.insert(“User@125”);
//判断"User@124”是否在集合中
if (set_limit.count(“User@124”) == 1)
cout << “User@124存在于集合中"<< std::endl;
else
cout << “User@124不在集合中" << std::endl;
//输出集合中所有的元素值
for (std::set<std::string>::iterator it = set_limit.begin(); it != set_limit.end(); it++)
cout << *it << std::endl;
注意:在获得指向set中某元素的迭代器后,只能对其做读操作,而不能做写操作。
std::set类型定义
typedef Key key_type;
typedef Key value_type;
typedef typename rep_type::const_pointer pointer;
typedef typename rep_type::const_pointer const_pointer;
typedef typename rep_type::const_reference reference;
typedef typename rep_type::const_reference const_reference;
typedef typename rep_type::const_iterator iterator;
typedef typename rep_type::const_iterator const_iterator;
typedef typename rep_type::const_reverse_iterator reverse_iterator;
typedef typename rep_type::const_reverse_iterator const_reverse_iterator;
typedef typename rep_type::size_type size_type;
typedef typename rep_type::difference_type difference_type;
注:深入STL的源代码看,可以看到size_type的类型其实是size_t,但也有可能因实现不同而不同。
std::set使用例子
std::set的声明
std::set<std::string> set_string; //元素类型是std::string,*迭代器得到的数值类型是std::string
std::set<int> set_int; //元素类型是int,*迭代器得到的数值类型是int
应用场景:兑换物品限制每个用户只能兑换一次,保存已兑换的用户。
 
 

map的基本用法:如插入、查找、删除、遍历等等,同时告诉你如何实现双键map,包括

(1) 只有两个键都匹配才命中目标
(2) 两个键中任意一个匹配就命中目标

可以扩展到多键


(一) 介绍
特点:
1.map将Key的object和T的Object绑定到一起,因此是一种Pair Associative Container, 表示其value type为 pair。
2.它同时也是Unique Associative Container,表示没有两个元素具有相同的Key。
3.它还是一种Sorted Associative Container,因此第三个参数只能是less,greater之类的functor, 相比较而言,
  hash table是 equal_to, not_equal_to之类的functor。
(二) 基本用法
通过以下范例,可以看出map的一些基本用法: 插入、查找、删除、遍历、遍历循环中删除等等。
 
#if defined (_MSC_VER)
#pragma warning(disable: 4786)
#endif
#include <iostream>
#include <map>
#include <algorithm>
int main(int argc, char *argv[])
{
    
    std::map<int,double> _map;
    
    
    _map.insert( std::map<int,double>::value_type(0, 32.8) );
    _map.insert( std::map<int,double>::value_type(1, 33.2) );
    _map.insert( std::map<int,double>::value_type(2, 35.8) );
    _map.insert( std::map<int,double>::value_type(3, 36.4) );
    _map.insert( std::map<int,double>::value_type(4, 37.8) );
    _map.insert( std::map<int,double>::value_type(5, 35.8) );
    
    
    _map[7] = 245.3;
    
    
    std::map<int,double>::iterator itr;
    itr = _map.find(4);
    
    if( itr != _map.end() )
    {
        std::cout  << "Item:"  << itr->first << " found, content: " << itr->second << std::endl;
    }
    
    std::cout  << std::endl;
    
    
    if( itr != _map.end() )
    {
        _map.erase(itr);
    }
    
    
    std::map<int,double>::iterator itr1  =  _map.begin();
    for(  ;  itr1  !=  _map.end();)
    { 
 //travel delete item from map 
if(itr1->first == 2) 
 { 
  _map.erase(it++);
  continue;   } 
  
       std::cout  << "Item:"  << itr1->first << ", content: " << itr1->second << std::endl;
  ++it;     }
    
    std::cout  << std::endl;
    
    
    _map.clear();
    
    return 0;
} 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值