C++ std::unordered_set是C++标准库中的一个容器,用于存储唯一的元素集合。它是基于哈希表实现的,具有快速的查找、插入和删除操作的特点。
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> mySet = {1, 2, 3, 4, 5};
int targetKey = 3;
auto iter = mySet.find(targetKey);
if (iter != mySet.end()) {
std::cout << "Found key " << targetKey << " in the unordered_set." << std::endl;
} else {
std::cout << "Key " << targetKey << " not found in the unordered_set." << std::endl;
}
return 0;
}
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> mySet = {1, 2, 3, 4, 5};
int targetKey = 3;
int count = mySet.count(targetKey);
std::cout << "The count of key " << targetKey << " is " << count << std::endl;
return 0;
}
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> mySet = {1, 2, 3, 4, 5};
int targetKey = 3;
bool contains = (mySet.count(targetKey) > 0);
if (contains) {
std::cout << "The unordered_set contains key " << targetKey << std::endl;
} else {
std::cout << "The unordered_set does not contain key " << targetKey << std::endl;
}
return 0;
}
C++ std::unordered_set的优势:
std::unordered_set的应用场景:
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云