STL—set(二)

简介: s.find(value);返回set中对应值为value的迭代器,时间复杂度是O(logN),N为set内的元素个数

(2)find()

s.find(value);返回set中对应值为value的迭代器,时间复杂度是O(logN),N为set内的元素个数

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int> s;
    for (int i = 1; i <= 5; i ++ ) s.insert(i);
    set<int>::iterator it = s.find(3);
    cout << *it;
    return 0;
}

输出结果为:3

当然也可以简写成:

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int> s;
    for (int i = 1; i <= 5; i ++ ) s.insert(i);
    cout << *(s.find(3));
    return 0;
}

(3)erase()

删除单个元素

s.erase(it);it为所需要删除元素的迭代器,时间复杂度为O(1),可以结合find()去使用

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int> s;
    for (int i = 1; i <= 5; i ++ ) s.insert(i);
    for (set<int>::iterator it = s.begin(); it != s.end(); it ++ )
        cout << *it << ' ';
    cout << endl;
    s.erase(s.find(3));
    for (set<int>::iterator it = s.begin(); it != s.end(); it ++ )
        cout << *it << ' ';
    return 0;
}

输出结果为:

1 2 3 4 5

1 2 4 5

s.erase(value);value为所需要删除元素的值,时间复杂度为O(logN),N为set内的元素个数

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int> s;
    for (int i = 1; i <= 5; i ++ ) s.insert(i);
    for (set<int>::iterator it = s.begin(); it != s.end(); it ++ )
        cout << *it << ' ';
    cout << endl;
    s.erase(3);
    for (set<int>::iterator it = s.begin(); it != s.end(); it ++ )
        cout << *it << ' ';
    return 0;
}

输出结果为:

1 2 3 4 5

1 2 4 5


删除一个区间内的所有元素

s.erase(first, last);可以删除一个区间内的所有元素,其中农first是所需要删除区间的起始迭代器,而last为所需要删除区间的末尾迭代器的下一个地址,即删除[first, last),时间复杂度为O(last - first)

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int> s;
    s.insert(2);
    s.insert(-3);
    s.insert(7);
    s.insert(2);
    s.insert(4);
    s.insert(-10);
    s.erase(s.find(-3), s.find(4));
    for (set<int>::iterator it = s.begin(); it != s.end(); it ++ ) 
        cout << *it << ' ';
    return 0;
}

输出结果为:-10 4 7

(4)size()

s.size();用来获得set内元素的个数,时间复杂度为O(1)

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int> s;
    s.insert(2);
    s.insert(-3);
    s.insert(7);
    s.insert(2);
    s.insert(4);
    s.insert(-10);
    for (set<int>::iterator it = s.begin(); it != s.end(); it ++ ) 
        cout << *it << ' ';
    cout << endl;
    cout << s.size();
    return 0;
}

输出结果为:

-10 -3 2 4 7

5

(5)clear()

s.clear();用来清空set内的所有元素,时间复杂度为O(N),N为set内元素的个数

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int> s;
    s.insert(2);
    s.insert(-3);
    s.insert(7);
    s.insert(2);
    s.insert(4);
    s.insert(-10);
    for (set<int>::iterator it = s.begin(); it != s.end(); it ++ ) 
        cout << *it << ' ';
    cout << endl;
    s.clear();
    cout << s.size();
    return 0;
}

输出结果为:

10 -3 2 4 7

0


目录
相关文章
|
7月前
|
存储 编译器 程序员
用同一棵红黑树实现map和set【STL】
用同一棵红黑树实现map和set【STL】
17 0
|
7月前
|
存储 C++ 容器
map、set、multimap和multiset的使用【STL】
map、set、multimap和multiset的使用【STL】
17 0
|
1天前
|
存储 C++ 容器
C++:STL - set & map
C++:STL - set & map
10 4
|
4天前
|
存储 Serverless C++
【C++入门到精通】哈希 (STL) _ unordered_map _ unordered_set [ C++入门 ]
【C++入门到精通】哈希 (STL) _ unordered_map _ unordered_set [ C++入门 ]
8 1
|
17天前
|
存储 搜索推荐 C++
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
|
29天前
|
存储 C++ 容器
【C++初阶】STL详解(十)set、map、multiset、multimap的介绍及使用
【C++初阶】STL详解(十)set、map、multiset、multimap的介绍及使用
29 0
|
1月前
|
存储 C语言 C++
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
|
2月前
|
存储 算法 数据处理
【C++ STL容器set 】set 容器的全方位解析
【C++ STL容器set 】set 容器的全方位解析
124 0
|
2月前
|
存储 算法 C语言
【C++入门到精通】C++入门 —— set & multiset (STL)
探索C++ STL中的重要成员:`set`与`multiset`。`set`是实现有序、不重复元素集合的容器,基于红黑树,提供高效操作。`multiset`则允许元素重复,两者均支持插入、删除、查找等操作。`set`强调元素唯一性,而`multiset`允许元素重复。两者在插入、查找、删除上的时间复杂度均为O(logN)。使用迭代器可遍历元素,但不支持下标访问。了解它们的特点和选择适用场景是关键。
36 0
|
7月前
|
算法 Serverless C++
用同一个哈希表实现unordered_map和unordered_set(C++实现)【STL】
用同一个哈希表实现unordered_map和unordered_set(C++实现)【STL】
16 0