C++ set 容器使用

/*
g++ set.cc  --std=c++17
 
set 容器使用

set 内部是使用红黑树实现的,是一种平衡二叉树,所以对其插入\查找效率是非常高的,其时间复杂度是log2(n)

set是STL中一种标准关联容器。它底层使用平衡的搜索树——红黑树实现,插入删除操作时仅仅需要指针操作节点即可完成,
不涉及到内存移动和拷贝,所以效率比较高。set,顾名思义是“集合”的意思,在set中元素都是唯一的,而且默认情况下
会对元素自动进行升序排列,支持集合的交(set_intersection),差(set_difference) 并(set_union),
对称差(set_symmetric_difference) 等一些集合上的操作,如果需要集合中的元素允许重复那么可以使用multiset。
*/

#include <set>
#include <iostream>
#include <string>

using namespace std;

class Student
{
public:    
    string name;
    int age;
    int sn;
    
    Student()
    {
        cout << "Student ctor" << endl;
    }
    
    ~Student()
    {
        cout << "Student dtor:"<<name << endl;
    }
};


int main(void)
{
    int i;

    typedef std::pair<int, string> intString;
    typedef std::set<intString> IntStringSet;
    IntStringSet int_sting_set;
    

    int a[] = { 1, 2, 3, 4, 5};
    set<int> s(a, a+5);



    for(i=0;i<10;i++)//1~5几个插入会失败
    {
        std::pair<set<int>::iterator, bool> result = s.insert(i);
        if(result.second)
        {
            cout<<"insert "<<i<<"   ok"<<endl;
        }
        else
        {
            cout<<"insert "<<i<<"  fail"<<endl;
        }
    }

    cout<<*s.lower_bound(2)<<endl;//返回指向首个不小于给定键的元素的迭代器
    cout<<*s.upper_bound(8)<<endl;//返回指向首个大于给定键的元素的迭代器
    cout<<"s.size()="<<s.size()<<endl;

    for(set<int>::iterator it = s.begin(); it!= s.end(); it++)
    {
        cout<<*it<<" ";
    }

    string str;
    char buffer[128];
    for(i=0;i<10;i++)
    {
        sprintf(buffer,"I am Index=%d",i);
        str = buffer;
        intString  cc(i,str);
        int_sting_set.insert(cc);
        //int_sting_set.insert(intString(i,str));
    }

    cout<<endl;
    for(IntStringSet::iterator it = int_sting_set.begin(); it!= int_sting_set.end(); it++)
    {
        cout<<"   *it.first="<<it->first<<"   *it.second="<<it->second<<endl;
    }


    typedef std::pair<int, Student *> StudentPair;//这里要Student * 指针才可以studentSet.insert(xx),不然会报错
    typedef std::set<StudentPair> StudentSet;
    StudentSet studentSet;
    Student *student;
    
    for(i=1;i<10;i++)
    {
        student = new Student;
        
        sprintf(buffer,"name %d",i);
        student->name = buffer;
        student->age = 18 + rand()%3;
        student->sn = i + 10;
        StudentPair xx=make_pair(i,student);
        //StudentPair xxx(i,student);
        
        auto result1 =  studentSet.insert(xx);
         
        if(result1.second)
        {
            cout<<"insert "<<i<<"   ok"<<endl;
        }
        else
        {
            cout<<"insert "<<i<<"  fail"<<endl;
        }
    }
    
    for (std::set<StudentPair>::iterator it=studentSet.begin(); it!=studentSet.end();++it)
    {
        cout<<"(*it).first="<<(*it).first<<"  sn="<<(*it).second->sn<<"  name="<<(*it).second->name<<"  age="<<(*it).second->age<<endl;
    }
    
    if (1)//两种方式都可以
    {
        for (const StudentPair& it : studentSet)
        {
            delete it.second;
        }
    }
    else
    {
        for (std::set<StudentPair>::iterator it=studentSet.begin(); it!=studentSet.end();++it)
        {
            delete (*it).second;
        }
    }
    
    
    cout<<"----------------------end---------------------------"<<endl;
        
    return 0;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值