![]() |
VOOZH | about |
In C++, a map can be converted to a set of pairs. This can be useful when you want to create a set of pairs from a map, where each pair contains a key and a value. In this article, we will learn how to convert a map to a set of pairs in C++.
Example
Input: myMap = {{1, “one”}, {2, “two”}, {3, “three”}}; Output: Set of Pairs is : {(1, “one”), (2, “two”), (3, “three”)}
To convert a std::map to a std::set of std::pair, we can use the range constructor of std::set that takes two iterators, one pointing to the beginning and the other to the end of the map.
The below example demonstrates the use of the range constructor to convert a map to a set of pairs in C++ STL.
(1, one) (2, two) (3, three)
Time Complexity: O(N log N), where N is the size of the map.
Auxiliary Space: O(N)