set 的 contains
语法:
set<int> num_set;
st.contains(num);
在 C++ 中,!num_set.contains(num - 1)
这行代码通常用于检查一个集合(num_set
)中是否不存在某个值(num - 1
)。以下是对这行代码的详细解释:
-
num_set
:num_set
应该是一个支持contains
成员函数的集合类对象,比如std::set
或std::unordered_set
。这些容器用于存储唯一的元素,并且提供了快速的查找操作。 -
contains
函数:contains
是std::set
和std::unordered_set
等容器的成员函数,用于检查容器中是否包含指定的元素。它接受一个参数(这里是num - 1
),并返回一个布尔值。如果容器中包含该元素,则返回true
;否则返回false
。 -
num - 1
:这是传递给contains
函数的参数,表示要在集合中查找的元素。它是num
减去 1 后的结果。 -
!
运算符:这是逻辑非运算符,它将contains
函数返回的布尔值取反。如果contains(num - 1)
返回true
(即集合中包含num - 1
这个元素),那么!num_set.contains(num - 1)
就会返回false
;反之,如果contains(num - 1)
返回false
(即集合中不包含num - 1
这个元素),那么!num_set.contains(num - 1)
就会返回true
。
.contains()用法拓展:
contains
方法在 C++ 标准库中的关联容器(如 std::set
、std::map
、std::unordered_set
、std::unordered_map
)以及 C++20 引入的范围库等场景中有多种用途,下面为你详细介绍除检查数字是否存在之外的一些用法。
1. 检查自定义类型对象是否存在
如果你使用自定义类型作为容器元素,也可以使用 contains
来检查容器中是否存在某个特定的对象。不过,需要确保自定义类型正确重载了比较运算符(对于 std::set
、std::map
)或者提供了哈希函数(对于 std::unordered_set
、std::unordered_map
)。
#include <iostream>
#include <unordered_set>
#include <string>
// 自定义类型
class Person {
public:
Person(const std::string& name, int age) : name(name), age(age) {}
// 重载 == 运算符,用于 unordered_set 比较元素
bool operator==(const Person& other) const {
return name == other.name && age == other.age;
}
std::string name;
int age;
};
// 自定义哈希函数
struct PersonHash {
std::size_t operator()(const Person& p) const {
return std::hash<std::string>()(p.name) ^ std::hash<int>()(p.age);
}
};
int main() {
std::unordered_set<Person, PersonHash> personSet;
personSet.insert(Person("Alice", 25));
personSet.insert(Person("Bob", 30));
if (personSet.contains(Person("Alice", 25))) {
std::cout << "Found Alice!" << std::endl;
}
return 0;
}
在上述代码中,定义了自定义类型 Person
,并重载了 ==
运算符,同时提供了自定义哈希函数 PersonHash
,这样就能使用 contains
方法检查 std::unordered_set
中是否存在特定的 Person
对象。
2. 在 std::map
中检查键是否存在
std::map
和 std::unordered_map
的 contains
方法用于检查指定的键是否存在于映射中,这在处理键值对数据时非常有用。
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> scores;
scores["Alice"] = 90;
scores["Bob"] = 85;
if (scores.contains("Alice")) {
std::cout << "Alice's score is: " << scores["Alice"] << std::endl;
}
return 0;
}
此代码通过 contains
方法检查 std::map
中是否存在键 "Alice"
,若存在则输出对应的值。
3. 结合范围库进行元素检查
在 C++20 及以后版本,范围库提供了强大的功能,contains
也可以和范围库一起使用来检查范围中是否包含特定元素。
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
auto evenNumbers = numbers | std::views::filter([](int n) { return n % 2 == 0; });
if (std::ranges::contains(evenNumbers, 4)) {
std::cout << "The range of even numbers contains 4." << std::endl;
}
return 0;
}
这里使用范围库对 std::vector
进行过滤得到偶数序列,然后使用 std::ranges::contains
检查该序列中是否包含数字 4
。
4. 处理字符串集合
在处理字符串集合时,contains
可以用于检查某个字符串是否在集合中。
#include <iostream>
#include <unordered_set>
#include <string>
int main() {
std::unordered_set<std::string> words = {"apple", "banana", "cherry"};
if (words.contains("banana")) {
std::cout << "The set contains 'banana'." << std::endl;
}
return 0;
}
代码中使用 std::unordered_set
存储字符串,通过 contains
检查集合中是否存在字符串 "banana"
。