Retrieve all keys from a map in C++
This post will discuss how to retrieve all keys from a map in C++.
1. Using Loop
We can write custom logic for retrieving all keys from the map. The idea is to iterate over the map using a range-based for loop or iterator-based for-loop and insert each key into a container.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <string> #include <map> #include <vector> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'B'} }; std::vector<int> keys; for (auto it = map.begin(); it != map.end(); it++) { keys.push_back(it->first); } for (const auto &key: keys) { std::cout << key << std::endl; } return 0; } |
Output:
1
2
3
4
Starting with C++17, we can use the range-based for-loop with structured bindings:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <algorithm> #include <map> #include <vector> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'B'} }; std::vector<int> keys; for (const auto& [k, v] : map) { keys.push_back(k); } for (const auto &key: keys) { std::cout << key << std::endl; } return 0; } |
Output:
1
2
3
4
2. Using BOOST_FOREACH
The BOOST_FOREACH simplifies loops in C++ by avoiding dealing with iterators or predicates directly. The following program iterates over the contents of a map using BOOST_FOREACH and fetches all keys.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <string> #include <map> #include <vector> #include <boost/foreach.hpp> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'B'} }; std::vector<int> keys; std::pair<int, char> entry; BOOST_FOREACH(entry, map) { keys.push_back(entry.first); } for (const auto &key: keys) { std::cout << key << std::endl; } return 0; } |
Output:
1
2
3
4
Alternatively, we can use the range adaptor provided by the boost library. Here’s an example of its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <string> #include <map> #include <vector> #include <boost/range/adaptor/map.hpp> #include <boost/range/algorithm/copy.hpp> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'B'} }; std::vector<int> keys; boost::copy(map | boost::adaptors::map_keys, std::back_inserter(keys)); for (const auto &key: keys) { std::cout << key << std::endl; } return 0; } |
Output:
1
2
3
4
3. Using std::transform
Another alternative is to use the std::transform algorithm to convert the map to a vector of keys. It applies an operation to the specified range and stores the result in another range.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <algorithm> #include <map> #include <vector> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'B'} }; std::vector<int> keys; std::transform(map.begin(), map.end(), std::back_inserter(keys), [](decltype(map)::value_type const &pair) { return pair.first; }); for (const auto &key: keys) { std::cout << key << std::endl; } return 0; } |
Output:
1
2
3
4
Starting with C++14, we can replace decltype()::value_type with auto.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <algorithm> #include <map> #include <vector> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'B'} }; std::vector<int> keys; std::transform(map.begin(), map.end(), std::back_inserter(keys), [](const auto &pair){ return pair.first; }); for (const auto &key: keys) { std::cout << key << std::endl; } return 0; } |
Output:
1
2
3
4
4. Using ranges library
With C++20, the standard library introduced the ranges library. It comes with view adaptors that are lazy views over containers and their transformations. To get a keys view of a map using ranges library, do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <map> #include <vector> #include <ranges> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'B'} }; auto kv = std::views::keys(map); std::vector<int> keys{ kv.begin(), kv.end() }; for (const auto &key: keys) { std::cout << key << std::endl; } return 0; } |
Output:
1
2
3
4
That’s all about retrieving all keys from a map in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)