Convert a map into a vector of pairs in C++
This post will discuss how to convert a map into a vector of pairs in C++. In other words, convert a std::map<int, char> to a std::vector<std::pair<int, char>>.
1. Using copy constructor
An elegant and efficient solution to initialize a vector of pairs with the map entries is using a copy constructor. This solution is simple, short, and efficient. Here’s the sample code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <map> #include <vector> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'} }; std::vector<std::pair<int, char>> values(map.begin(), map.end()); for (const auto &pair: values) { std::cout << "(" << pair.first << ", " << pair.second << ")" << std::endl; } return 0; } |
Output:
(1, A)
(2, B)
(3, C)
(4, D)
Since C++17, we can skip the container’s template arguments and have the compiler derive them:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <map> #include <vector> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'} }; std::vector values(map.begin(), map.end()); for (const auto &pair: values) { std::cout << "(" << pair.first << ", " << pair.second << ")" << std::endl; } return 0; } |
Output:
(1, A)
(2, B)
(3, C)
(4, D)
2. Using std::copy
Another option to copy all elements from a given map to std::vector is using the standard algorithm std::copy from header <algorithm>. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <map> #include <vector> #include <algorithm> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'} }; std::vector<std::pair<int, char>> values(map.size()); std::copy(map.begin(), map.end(), values.begin()); for (const auto &pair: values) { std::cout << "(" << pair.first << ", " << pair.second << ")" << std::endl; } return 0; } |
Output:
(1, A)
(2, B)
(3, C)
(4, D)
We can use std::copy with std::back_inserter, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <map> #include <vector> #include <algorithm> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'} }; std::vector<std::pair<int, char>> values; std::copy(map.begin(), map.end(), back_inserter(values)); for (const auto &pair: values) { std::cout << "(" << pair.first << ", " << pair.second << ")" << std::endl; } return 0; } |
Output:
(1, A)
(2, B)
(3, C)
(4, D)
3. Using std::vector::assign
The vector::assign member function replaces the current vector elements with the elements in the specified range. It can be used as follows, to convert a map into a vector of pairs:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <map> #include <vector> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'} }; std::vector<std::pair<int, char>> values; values.assign(map.begin(), map.end()); for (const auto &pair: values) { std::cout << "(" << pair.first << ", " << pair.second << ")" << std::endl; } return 0; } |
Output:
(1, A)
(2, B)
(3, C)
(4, D)
To avoid overriding any existing values in the vector with the map entries, consider using the vector::insert member function. It is recommended to allocate memory for the vector using the std::vector::reserve function before calling insert, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <map> #include <vector> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'} }; std::vector<std::pair<int, char>> values; // add values to vector values.reserve(values.size() + map.size()); values.insert(values.end(), map.begin(), map.end()); for (const auto &pair: values) { std::cout << "(" << pair.first << ", " << pair.second << ")" << std::endl; } return 0; } |
Output:
(1, A)
(2, B)
(3, C)
(4, D)
4. Using Loop
Finally, we can write custom logic for converting a map into a vector of pairs using a range-based for-loop. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <map> #include <vector> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'} }; std::vector<std::pair<int, char>> values; values.reserve(map.size()); for (auto const &kv: map) { values.emplace_back(kv); } for (const auto &pair: values) { std::cout << "(" << pair.first << ", " << pair.second << ")" << std::endl; } return 0; } |
Output:
(1, A)
(2, B)
(3, C)
(4, D)
That’s all about converting a map into a vector of pairs 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 :)