c++ set容器总结

#include<bits/stdc++.h>
#include<set>
using namespace std;

int main(){
     int x[]={1,2,3,4,4,5,5};
     set<int>y(x,x+7);

     set<int>::iterator z;
     for(z=y.begin();z!=y.end();z++)
        cout<<" "<<*z;

     return 0;
}
#include<bits/stdc++.h>
#include<set>
using namespace std;


int main(){
     char x[]={'1','2','1','2','3','4','5'};
     set<char>y(x,x+7);


     set<char>::iterator z;
     for(z=y.begin();z!=y.end();z++)
        cout<<*z<<" ";


     return 0;
}
///输出  1,2,3,4,5

///set  怎样输出    (begin() ;   和end();)     set 中的元素 不会重复; 而且是自动排好顺序

///  set

#include<bits/stdc++.h>
#include<set>
using namespace std;

int main(){
     set<int>x;
     x.insert(100);
     x.insert(50);
     x.insert(60);
     cout<<x.size()<<endl;
     while(!x.empty()){
        cout<<" "<<*x.begin();
        x.erase(x.begin());
     }
     cout<<endl;
     cout<<x.size()<<endl;

     return 0;
}

输出  3

         50 60 100

         0

///

set::empty

#include<bits/stdc++.h>
#include<set>
using namespace std;

int main(){
     set<int>x;
     set<int>::iterator y;
     for(int i=1;i<=5;i++) x.insert(i*10); ///set(10 20 30 40 50)
     y=x.find(20);
     cout<<*y<<endl;
     x.erase(y);
     x.erase(x.find(40));
     for(y=x.begin();y!=x.end();y++)
         cout<<*y<<" ";
     cout<<endl<<x.size();

     return 0;
}
输出   20

          10   30  50

          3




/// set:: count

#include<bits/stdc++.h>
#include<set>
using namespace std;

int main(){
     set<int>x;
     set<int>::iterator y;
     for(int i=1;i<=5;i++) x.insert(i*2); ///set(2 4 6 8 10)
     for(int i=0;i<=10;i++){
        cout<<i;
        if(x.count(i))  cout<<"  是x中的元素"<<endl;
        else    cout<<"  不是x中的元素"<<endl;
     }

     return 0;
}

输出:

0  不是x中的元素
1  不是x中的元素
2  是x中的元素
3  不是x中的元素
4  是x中的元素
5  不是x中的元素
6  是x中的元素
7  不是x中的元素
8  是x中的元素
9  不是x中的元素
10  是x中的元素




//   set:: lower_bound  up_bound

#include<bits/stdc++.h>
#include<set>
using namespace std;

int main(){
     set<int>x;
     set<int>::iterator y,up,low;
     for(int i=1;i<=10;i++)  x.insert(i);///1 2 3 4 5 6 7 8 9 10
     up=x.upper_bound(6);
     low=x.lower_bound(1);
     x.erase(low,up);
     for(y=x.begin();y!=x.end();y++)
        cout<<*y<<" ";

     return 0;
}输出
输出:  7 8 9 10



///

   set<int>x




   begin()              容器的首项
   end()                容器的末尾项
   ::iterator y         迭代容器的内容
   empty(x)             判断容器是否为空
   x.erase(x.begin())   删除容器内指定某一位置的元素 如 :首项
   x.insert(10)         在容器中插入10这个元素
   x.size()             容器的大小
   x.find(*)            “*”查找你想要的值
   x.count(*)           看”*“是否为x中的元素
   lower_bound/upper_bound   清除这个范围的所有元素

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值