• STL学习笔记4--set and multiset


      集合(Set)是一种包含已排序对象的关联容器。多元集合(MultiSets)和集合(Sets)相像,只不过支持重复对象,其用法与set基本相同。

     用法介绍

      1.insert()函数

      首先把头文件set包含进去 #include "set"

        set <int> test1;
        set <int>::iterator it1;
        multiset <int> test2;
        multiset <int>::iterator it2;
        //////////////////////////////////////////////////////////////////////////
        //输入
        it1 = test1.begin();
        it2 = test2.begin();
        for (int j=1;j<=10;j++)
        {
            test1.insert(it1,j);//直接在最后面插入数据
            test2.insert(it2,j*10);
        }

      2、输出的话,可以直接取地址

        cout<<"Elements:";
        for ( it1=test1.begin() ; it1 != test1.end(); it1++ )
            cout << " " << *it1;//直接取地址
        cout<<endl;

      3、find() and erase()查找元素和删除元素,需要注意的是当元素不存在是find会出错

        //查找和删除,当元素不存在是find会出错
        it1 = test1.find(4);//查找某元素位置
        test1.erase(it1);//删除元素
        test1.erase(test1.find(11));//结合起来写
    
        for ( it1=test1.begin() ; it1 != test1.end(); it1++ )
            cout << " " << *it1;
        cout<<endl;

      4、count() 函数,元素计数,set中可用于判断元素是否存在

        //count,计数,可用来判断元素是否存在,因为set中元素不允许重复
        test2.insert(90);
        cout<<"multiset的元素可以重复:"<<test2.count(90)<<endl;
        for (int i=0;i<6; i++)
        {
            cout << i;
            if (test1.count(i)>0)//只能有一个元素
                cout << " is an element of test1.
    ";
            else 
                cout << " is not an element of test1.
    ";
        }

      5、equal_range()函数  返回第一个>=关键字的迭代器和>关键字的迭代器 ,也就是你要查找的元素的指针和它下一个元素的指针。

        //equal_range()  返回第一个>=关键字的迭代器和>关键字的迭代器 
        pair<set<int>::iterator,set<int>::iterator> ret;//注意用法 pair
        ret = test1.equal_range(5);//查找元素5的迭代器和它下一个迭代器,如果不存在,则这两个迭代器都指向是下一个元素
        cout<<"lower bound points to:"<<*ret.first<<endl;
        cout<<"upper bound points to:"<<*ret.second<<endl;//不能越界

      6、 lower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器 

        upper_bound()  返回大于某个值元素的迭代器 

    这两个函数结合在一起,可用来修改或者删除某一个范围内的元素

        set <int>::iterator itlower,itupper;
        itlower = test1.lower_bound(5);//指向大于等于5的迭代器
        itupper = test1.upper_bound(9);//指向大于9的迭代器
        test1.erase(itlower,itupper);//erase data from 5 to 10
        cout<<"Element:";
        for ( it1=test1.begin() ; it1 != test1.end(); it1++ )
            cout << " " << *it1;
        cout<<endl;

      7 swap   交换两个集合变量 中的所有元素

        //swap   交换两个集合变量 中的所有元素
        set <int> test3;
        set <int>::iterator it3;
        it3 = test3.begin();
        for (int j=1;j<=10;j++)
        {
            test3.insert(it3,j*2);//直接在最后面插入数据
    
        }
        swap(test1,test3);

      8、empty(),clear() 清空数据

       test1.clear();
        if (test1.empty())
        {
            cout<<"set is null"<<endl;
        }
        

     

     set集合函数很简单,就这么多常用功能。更复杂的是在使用很灵活

  • 相关阅读:
    【学习总结】Git学习-参考廖雪峰老师教程二-安装Git
    【学习总结】Git学习-参考廖雪峰老师教程一-Git简介
    【学习总结】vi/vim命令是使用
    【学习总结】Git学习-参考廖雪峰老师教程-总
    【kindle笔记】之 《鬼吹灯》-9-20
    【学习总结】win7下安装Ubuntu双系统的日常
    【kindle笔记】之 《明朝那些事儿》-2018-7-1
    【kindle笔记】之 《黑客微百科》-2018-6-17
    【kindle笔记】之 《恶意》-2018-4-20
    【kindle笔记】之 《活着》-2018-2-5
  • 原文地址:https://www.cnblogs.com/songliquan/p/3340801.html
Copyright © 2020-2023  润新知