How to Use the STL Set Container in C++
-
Use
std::set
to Declare Set Container Object in C++ -
Use the
insert
Member Function to Insert an Element Into Set in C++ -
Use the
find
Member Function to Retrieve the Iterator of the Element With the Given Key in C++ -
Use the
contains
Member Function to Check if the Element With the Given Key Exists in a Set in C++
This article will demonstrate multiple methods about how to use the STL set
container in C++.
Use std::set
to Declare Set Container Object in C++
The std::set
command implements a sorted set of unique objects as an associative container. Elements are sorted with the std::less
comparison function by default, but the user can supply the custom function as the second template argument. The same header also provides the std::multiset
container that can store the multiple values without filtering duplicates.
Note that we create both set objects with the initializer list constructor in the following example. We also utilize the count
member function, which retrieves the number of elements that exist in the set with the given key. This function is more intrinsic to the std::multiset
container, but you can check if the element exists with the same function when invoked from the std::set
object.
#include <iostream>
#include <set>
using std::cout;
using std::endl;
using std::multiset;
using std::set;
template <typename T>
void printSet(set<T> s) {
for (const auto &item : s) {
cout << item << "; ";
}
cout << endl;
}
template <typename T>
void printMultiSet(multiset<T> s) {
for (const auto &item : s) {
cout << item << "; ";
}
cout << endl;
}
#define STR(num) #num
int main() {
std::set<int> s1 = {1, 1, 1, 2, 2, 3};
printSet(s1);
std::multiset<int> s2 = {1, 1, 1, 2, 2, 3};
printMultiSet(s2);
std::cout << STR(s2) << " contains " << s2.count(1) << "ones" << endl;
std::cout << STR(s2) << " contains " << s2.count(2) << "twos" << endl;
return EXIT_SUCCESS;
}
Output:
1; 2; 3;
1; 1; 1; 2; 2; 3;
s2 contains 3 ones
s2 contains 2 twos
Use the insert
Member Function to Insert an Element Into Set in C++
The insert
function has multiple overloads, but we utilize the version that takes a single argument representing the element to be added to the set. This overload of insert
returns the std::pair
object of iterator and the bool
.
The latter indicates if the insertion was successful, and if so, it has a true
value. On the other hand, the iterator points to the inserted element when the operation succeeds. If it fails, it points to the element that prevented the insertion. Note that insertion operation has logarithmic complexity in the size of the container.
#include <cassert>
#include <iostream>
#include <set>
using std::cout;
using std::endl;
using std::multiset;
using std::set;
int main() {
std::set<int> s1 = {1, 1, 1, 2, 2, 3};
auto ret = s1.insert(5);
assert(*ret.first == 5);
if (ret.second) std::cout << "Inserted!" << endl;
ret = s1.insert(1);
assert(*ret.first == 1);
if (!ret.second) std::cout << "Not inserted!" << endl;
return EXIT_SUCCESS;
}
Output:
Inserted!
Not inserted!
Use the find
Member Function to Retrieve the Iterator of the Element With the Given Key in C++
The find
function is another useful member function of the std::set
container that can return the iterator to the element with the given key. If there’s no such element in the set, the past-the-end iterator is returned; this helps the user verify the successful function call.
#include <iostream>
#include <set>
using std::cout;
using std::endl;
using std::multiset;
using std::set;
int main() {
std::set<int> s1 = {1, 1, 1, 2, 2, 3};
auto search = s1.find(2);
if (search != s1.end()) {
cout << "Found " << (*search) << endl;
} else {
cout << "Not found" << endl;
}
return EXIT_SUCCESS;
}
Output:
Found 2
Use the contains
Member Function to Check if the Element With the Given Key Exists in a Set in C++
Since the C++20 language standard, std::set
provides the member function, contains
, which is the simpler interface to check if the element with the given key exists in the set object. The function returns the boolean value to indicate if such an element is present in the set. The Running time complexity of this function is also logarithmic in the size of the container.
#include <iostream>
#include <set>
using std::cout;
using std::endl;
using std::multiset;
using std::set;
int main() {
std::set<int> s3 = {91, 123, 63, 122, 22, 53};
for (int x : {22, 23, 53, 54}) {
if (s3.contains(x)) {
cout << x << ": Found\n";
} else {
cout << x << ": Not found\n";
}
}
return EXIT_SUCCESS;
}
Output:
22: Found
23: Not found
53: Found
54: Not found
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook