• std::set 中内部元素有序条件删除的理解


    std::set 中内部元素有序条件删除的理解

    1. std::set中的元素是有序排列的

      注意:Set集合中的元素通过iterator的引用,但是不能修改。

    元素排序:

    (1)元素中实现比较operator < ,

    (2)Set构造中,输入仿函数(实现元素比较排序)

      基于上述的规则,因此:如果要更新Set中元素的修改,只能将元素erase删除,然后将更新后的元素insert中,则自动保证insert中的相应位置

     

      

    2. 如果Set中的元素是object,不是指针。

      删除元素的代码:

      因为, 如果用iterator删除当前所指的元素,只能用s.erase(it++) 这样的形势, 在it删除之后,其实已经更新为下一个的迭代器了。

    #include <set>
    using std::set;
     
    int main(int argc,char *argv[])
    {
        set<int> s;
        set<int>::iterator it;
     
        s.insert(1);
        s.insert(2);
        s.insert(3);
     
        for(it=s.begin();it!=s.end();){
            if((*it)%2==0)
                s.erase(it++);
            else
                it++;
        }
     
        system("pause");
     
        return 0;
    }
     
    STL/C++__中 set(集合)  删除元素, set的erase不会返回迭代器,这点需要注意。

    3. 如果Set中的元素是:指针。

      因为元素是指针,因此同样要要提供:比较函数。

      这样可以突破了:虽然不能修改元素内容,因为元素指针不能修改,但是元素指针所指的内存可以修改。但是注意:此时的“顺序”不能保证

      注意,在Set中,插入指针类型,如果释放资源,可以直接delete (*it), 注意, 此时的it还是有效的,可以使用it++

    #include "stdafx.h"
    #include <set>
    
    #include <iostream>
    using namespace std;
    
    class Name
    {
    public:
        Name(int _a, int _b) :a(_a), b(_b){};
        int a;
        int b;
    };
    
    class CMP
    {
    public:
    
        bool operator()(Name* _p1, Name* _p2)
        {
            if (_p1->a < _p2->a)
                return true;//前一个元素,小于,后一个元素,升序
            return false;
    
        }
    };
    typedef std::set<Name*, CMP> ContainerType;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        ContainerType container;
        container.insert(new Name(44, 22));
        container.insert(new Name(33, 22));
        container.insert(new Name(11, 22));
        container.insert(new Name(42, 22));
        container.insert(new Name(99, 22));
        container.insert(new Name(66, 22));
    
        ContainerType::iterator it;
        //显示
        for (it = container.begin(); it != container.end(); ++it)
        {
            //更新内部状态
            cout << (*it)->a << "   " << (*it)->b << endl;
        }
    
        cout << "---------------------" << endl;
    
        for (it = container.begin(); it != container.end(); ++it)
        {
            //更新内部状态
            if ((*it)->a > 50)
                (*it)->a -= 10;
            else
            {
                (*it)->a += 1000;
            }
            
            
        }
        container.insert(new Name(0, 22));
        container.insert(new Name(300, 22));
        //显示
        for (it = container.begin(); it != container.end(); ++it)
        {
            //更新内部状态
            cout << (*it)->a << "   " << (*it)->b << endl;
        }
    
    
        //释放资源
        for (it = container.begin(); it != container.end(); ++it)
        {
            //更新内部状态
            delete *it;
        }
    
        system("pause");
        return 0;
    }

      

       

     

     

     

     

     

     

    endl;

  • 相关阅读:
    20172302 201720182 《程序设计与数据结构》实验二报告
    20172302 201720182 《程序设计与数据结构》实验一报告
    20172302 《程序设计与数据结构》第五周学习总结
    POJ 1061 青蛙的约会
    hdu 2485 Highways
    UVA 10608
    hdu 1213 how many tables
    java类static成员加载顺寻
    C# virtual,override或者new
    vs无法在WEB服务器上启动调试
  • 原文地址:https://www.cnblogs.com/icmzn/p/9669613.html
Copyright © 2020-2023  润新知