C++ STL中的set容器用法 
时间: 2023-05-27 15:02:41 浏览: 71
STL中的set容器是一种集合,它可以存储一组元素,并确保它们按照一定的次序排列。set容器可以存储各种类型的数据,包括基本数据类型、结构体、类对象等。
set容器的用法如下:
1. 导入头文件
#include <set>
2. 定义set容器
set<data_type> s;
3. 插入元素
s.insert(value);
其中,data_type表示set容器中存储的数据类型,value表示要插入的元素。
4. 遍历set容器中的元素
可以使用迭代器来遍历set容器中的元素:
for(auto it = s.begin(); it != s.end(); ++it)
{
// do something
}
其中,auto关键字是C++11中引入的,它可以自动推导迭代器类型。
5. 删除元素
可以使用erase()函数删除set容器中的元素:
s.erase(value);
其中,value表示要删除的元素。
6. 查询元素
可以使用find()函数在set容器中查找元素:
auto it = s.find(value);
如果元素存在,返回一个指向该元素的迭代器;如果不存在,返回set容器的end迭代器。
总之,set容器在C++ STL中是一种非常有用的容器,它可以帮助我们快速存储和管理一组元素,并支持快速的插入、删除和查询操作。
相关问题
c++ set容器基本操作
C++ STL库中的set容器是一个用于存储元素的关联容器,它的元素默认按照升序排列,并且保证元素的唯一性。set容器的基本操作包括:
1. 插入元素:使用insert()函数向set容器中插入元素。插入元素的时间复杂度为O(log n)。
```c++
#include <iostream>
#include <set>
int main() {
std::set<int> s;
s.insert(3);
s.insert(1);
s.insert(4);
s.insert(1); // 重复元素不会被插入
s.insert(5);
for (auto x : s) {
std::cout << x << " ";
}
std::cout << std::endl;
return 0;
}
```
上述代码输出:1 3 4 5
2. 删除元素:使用erase()函数删除set容器中的元素,可以指定要删除的元素值,也可以指定一个迭代器范围。删除元素的时间复杂度为O(log n)。
```c++
#include <iostream>
#include <set>
int main() {
std::set<int> s{3, 1, 4, 1, 5};
s.erase(1); // 删除元素值为1的元素
for (auto x : s) {
std::cout << x << " ";
}
std::cout << std::endl;
return 0;
}
```
上述代码输出:3 4 5
3. 查找元素:使用find()函数和count()函数查找set容器中的元素,find()函数返回一个指向要查找的元素的迭代器,如果元素不存在,则返回end()迭代器。count()函数返回要查找的元素在set容器中出现的次数,因为set容器中的元素保证唯一性,所以count()函数的返回值只能是0或1。查找元素的时间复杂度为O(log n)。
```c++
#include <iostream>
#include <set>
int main() {
std::set<int> s{3, 1, 4, 1, 5};
auto it = s.find(4);
if (it != s.end()) {
std::cout << "4 is found" << std::endl;
} else {
std::cout << "4 is not found" << std::endl;
}
std::cout << "1 appears " << s.count(1) << " times" << std::endl;
std::cout << "2 appears " << s.count(2) << " times" << std::endl;
return 0;
}
```
上述代码输出:4 is found,1 appears 1 times,2 appears 0 times。
除了上述基本操作外,set容器还支持迭代器操作和比较操作。更详细的使用方法可以参考C++ STL库的set容器文档。
c++中unordered_set用法
### 回答1:
unordered_set是C++ STL中的一个容器,用于存储一组不重复的元素,其内部实现是基于哈希表的。unordered_set中的元素是无序的,但是可以通过哈希函数快速查找元素。
unordered_set的用法和其他STL容器类似,可以使用insert()函数向其中插入元素,使用erase()函数删除元素,使用find()函数查找元素等。另外,unordered_set还提供了一些其他的成员函数,如size()、empty()、clear()等。
unordered_set的定义方式如下:
```c++
#include <unordered_set>
using namespace std;
unordered_set<int> mySet; //定义一个存储int类型元素的unordered_set
```
在使用unordered_set时,需要注意以下几点:
1. unordered_set中的元素必须是可哈希的,即需要定义哈希函数和相等比较函数。
2. unordered_set中的元素是无序的,不能通过下标访问元素。
3. unordered_set中的元素不允许重复,如果插入重复元素会被忽略。
下面是一个使用unordered_set的例子:
```c++
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<int> mySet;
mySet.insert(1);
mySet.insert(2);
mySet.insert(3);
mySet.insert(2); //插入重复元素,会被忽略
cout << "mySet size: " << mySet.size() << endl; //输出元素个数
if (mySet.find(2) != mySet.end()) //查找元素2
cout << "2 is in mySet" << endl;
else
cout << "2 is not in mySet" << endl;
mySet.erase(3); //删除元素3
for (auto it = mySet.begin(); it != mySet.end(); ++it) //遍历元素
cout << *it << " ";
cout << endl;
mySet.clear(); //清空元素
cout << "mySet size: " << mySet.size() << endl; //输出元素个数
return ;
}
```
### 回答2:
unordered_set是STL提供的一种高效的哈希表容器,它能够快速地将数据插入到哈希表中,同时也能够快速地进行查找和删除操作。
使用unordered_set需要包含头文件<unordered_set>。和其他STL容器一样,定义一个unordered_set对象需要指定元素类型和哈希函数,例如:
unordered_set<int> my_set; //其中元素类型是int
unordered_set<string> my_set2; //其中元素类型是string
如果需要自定义哈希函数,可以使用unordered_set提供的hash模板函数,例如:
struct MyHashFunc {
size_t operator()(const my_struct& s) const {
return hash<string>()(s.key) ^ hash<int>()(s.value);
}
};
unordered_set<my_struct, MyHashFunc> my_set; //其中元素类型是my_struct,哈希函数是MyHashFunc
下面是unordered_set的常用操作:
1.插入元素
unordered_set提供了insert()函数,可以将元素插入到unordered_set中,例如:
unordered_set<int> my_set;
my_set.insert(1);
my_set.insert(2);
my_set.insert(3);
2.查找元素
unordered_set提供了find()函数,可以在unordered_set中查找元素,它返回一个迭代器,指向第一个等于查找值的元素,如果没有找到,则返回unordered_set::end()。例如:
auto it = my_set.find(2);
if (it == my_set.end()) {
cout << "2 not found in my_set" << endl;
}
3.删除元素
unordered_set提供了erase()函数,可以在unordered_set中删除元素,它可以接受元素值或迭代器作为参数。例如:
my_set.erase(2);
4.遍历元素
unordered_set可以使用迭代器来遍历所有的元素,例如:
for (auto it = my_set.begin(); it != my_set.end(); ++it) {
cout << *it << endl;
}
因为unordered_set是一个哈希表,所以元素的顺序是无序的。此外,unordered_set还提供了size()函数可以返回unordered_set中元素的个数,empty()函数可以判断unordered_set是否为空。
### 回答3:
unordered_set是C++ STL中的一种容器,它是一个哈希表,可以被用来存储唯一的元素。unordered_set内部元素的排列顺序并不是按照插入顺序,而是按照哈希值决定的。
unordered_set 的定义方式如下:
```c++
unordered_set<int> s; // 存储int类型的unordered_set
```
与其他的STL容器一样,unordered_set也提供了许多操作:插入元素、删除元素、查找元素等等。下面简要介绍unordered_set的基本用法:
1. 插入元素
插入元素可以用insert()函数实现:
```c++
unordered_set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
```
上述代码中,将元素1、2、3插入unordered_set中。
2. 删除元素
删除元素可以用erase()函数实现:
```c++
unordered_set<int> s{1, 2, 3};
s.erase(2);
```
上述代码中,从unordered_set中删除元素2。
3. 查找元素
查找元素可以用find()函数实现:
```c++
unordered_set<int> s{1, 2, 3};
if (s.find(2) != s.end()) {
cout << "found" << endl;
} else {
cout << "not found" << endl;
}
```
上述代码中,查找unordered_set中是否存在元素2,如果存在,输出“found”,否则输出“not found”。
unordered_set还有其他许多函数,比如size()函数获取unordered_set的元素数量,empty()函数判断unordered_set是否为空等等。此外,我们也可以自定义哈希函数和判断相等的函数,以便unordered_set能够正确处理自定义类型的元素。
相关推荐
















