zoukankan      html  css  js  c++  java
  • std::set用法(转)

    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
    应用场景:兑换物品限制每个用户只能兑换一次,保存已兑换的用户。


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/baund/archive/2010/04/20/5505260.aspx

  • 相关阅读:
    关于PHP引用(符号&)的用法
    inline元素相关
    内联元素的margin属性
    chrome 开发者工具使用详情
    闭包 by 5wilon
    容易失忆的css
    题目:吃西瓜
    题目:装箱问题
    题目:最小乘车费用
    题目:竞赛真理
  • 原文地址:https://www.cnblogs.com/mazhenyu/p/2089299.html
Copyright © 2011-2022 走看看