c++unordered_set判断值是否存在用empty
时间: 2023-06-08 14:07:59 浏览: 56
()函数还是使用count()函数比较好?
首先,要明确一点的是,从语义上讲,empty() 和 count()两个函数并不具有相同的功能:empty()函数用于判断集合是否为空,而count()函数用于统计集合中某个元素出现的次数,或者判断某个元素是否在集合中出现过。
因此,如果你只是想简单地判断某个元素是否在集合中出现过,那么使用count()函数更为合适;而如果你需要判断整个集合是否为空,那么则需要使用empty()函数。
另外,从算法复杂度的角度考虑,两个函数的复杂度都是O(1),因此在实际应用中,两种方式的效率差别较小,可以自由选择使用。
综上所述,如果你需要实现的功能是判断某个元素是否在集合中出现过,那么可以使用count()函数;如果你需要判断整个集合是否为空,则应使用empty()函数。
相关问题
c++ unordered_set
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 ;
}
```
C++ unordered_set
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 ;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)