STL---set和multiset

简介: #include <iostream> #include <set> using namespace std; int main() { set<int>set1; for(int i=9; i>0; i--) set1.insert(i); //有序插入 set1.erase(3);
#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int>set1;
    for(int i=9; i>0; i--)
        set1.insert(i);  //有序插入
    set1.erase(3);
    for(set<int>::iterator p=set1.begin(); p!=set1.end(); ++p)
        cout<<*p<<"";
    cout<<endl;
    if(set1.insert(3).second)
        //把3插入到set1中
        //插入成功则set1.insert(3).second返回1,否则返回0
        //此例中,set不能插入重复元素集中已经有3这个元素了,所以插入将失败
        cout<<"set insert success";
    else
        cout<<"set insert failed";
    cout<<endl;
    for(set<int>::iterator p=set1.begin(); p!=set1.end(); ++p)
        cout<<*p<<"";
    cout<<endl;
    int a[] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0};
    multiset<int> A;
    A.insert(set1.begin(),set1.end());
    for(set<int>::iterator p=A.begin(); p!=A.end(); ++p)
        cout<<*p<<"";
    cout<<endl; 
    A.insert(a,a+10);
    for(set<int>::iterator p=A.begin(); p!=A.end(); ++p)
        cout<<*p<<"";
    cout<<endl;
    return 0;
}

  

目录
相关文章
|
存储 C++ 容器
set容器-set和multiset区讲解
set容器-set和multiset区讲解
259 0
|
存储 C++
【C++】map/multimap/set/multiset的经典oj例题 [ 盘点&全面解析 ] (28)
【C++】map/multimap/set/multiset的经典oj例题 [ 盘点&全面解析 ] (28)
|
存储 C++ 容器
map、set、multimap和multiset的使用【STL】
map、set、multimap和multiset的使用【STL】
273 0
|
存储 编译器 容器
set、map、multiset、multimap的介绍及使用以及区别,注意事项
set是按照一定次序存储元素的容器,使用set的迭代器遍历set中的元素,可以得到有序序列。set当中存储元素的value都是唯一的,不可以重复,因此可以使用set进行去重。set默认是升序的,但是其内部默认不是按照大于比较,而是按照小于比较。set中的元素不能被修改,因为set在底层是用二叉搜索树来实现的,若是对二叉搜索树当中某个结点的值进行了修改,那么这棵树将不再是二叉搜索树。
437 0
|
存储 算法 C++
【c++丨STL】set/multiset的使用
本文深入解析了STL中的`set`和`multiset`容器,二者均为关联式容器,底层基于红黑树实现。`set`支持唯一性元素存储并自动排序,适用于高效查找场景;`multiset`允许重复元素。两者均具备O(logN)的插入、删除与查找复杂度。文章详细介绍了构造函数、迭代器、容量接口、增删操作(如`insert`、`erase`)、查找统计(如`find`、`count`)及`multiset`特有的区间操作(如`lower_bound`、`upper_bound`、`equal_range`)。最后预告了`map`容器的学习,其作为键值对存储的关联式容器,同样基于红黑树,具有高效操作特性。
695 3
|
机器学习/深度学习 C++ 容器
STL_set/multiset
STL_set/multiset
148 1
|
C++ 容器
C++之set/multiset容器
C++之set/multiset容器
185 1
|
存储 自然语言处理 C++
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
188 0
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
|
存储 C语言 容器
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别(下)
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别
538 1
|
存储 C语言 容器
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别(中)
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别
397 1