contains for Associative Containers
Understand the details of the 'contains' function.
We'll cover the following...
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
Press + to interact
#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) ...