Search⌘ K
AI Features

contains for Associative Containers

Explore how the contains function in C++20 improves checking element existence in associative containers like set and map. Understand why it's more convenient and efficient compared to traditional find or count methods specific to associative containers.

Thanks to the function contains, you can easily check if an element exists in an associative container. Stop, you may say, we can already do this with find or count. No, both functions are not beginner-friendly and have their downsides.

Erasing elements from a container

C++
#include <set>
#include <iostream>
int main() {
std::cout << '\n';
std::set mySet{3, 2, 1};
if (mySet.find(2) != mySet.end()) {
std::cout << "2 inside" << '\n';
}
std::multiset myMultiSet{3, 2, 1, 2};
if (myMultiSet.count(2)) {
std::cout << "2 inside" << '\n';
}
std::cout << '\n';
}

There are issues with both calls. The find call (line 8) ...