Convert vector of pairs to a map in C++
This post will discuss how to convert a vector of pairs to a map in C++.
1. Using std::copy
A simple option to copy all elements from a vector to a map is using the std::copy standard algorithm from header <algorithm>. Its usage is demonstrated 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::vector<std::pair<std::string, int>> values = { {"one", 1}, {"two", 2}, {"three", 3} }; std::map<std::string, int> map; std::copy(values.begin(), values.end(), std::inserter(map, map.begin())); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{one, 1}
{three, 3}
{two, 2}
2. Using copy constructor
Alternatively, we can use the copy constructor to initialize a map from the vector of pairs. This is the shortest and the most idiomatic way to convert a vector of pairs to a map. 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 |
#include <iostream> #include <map> #include <vector> int main() { std::vector<std::pair<std::string, int>> values = { {"one", 1}, {"two", 2}, {"three", 3} }; std::map<std::string, int> map(values.begin(), values.end()); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{one, 1}
{three, 3}
{two, 2}
3. Using std::transform
Another alternative is to use the std::transform algorithm to transform std::vector<std::pair<int, char>> into std::map<int, char>. The std::transform algorithm is defined in the <algorithm> header, and apply a given 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 25 |
#include <iostream> #include <map> #include <vector> #include <algorithm> int main() { std::vector<std::pair<std::string, int>> values = { {"one", 1}, {"two", 2}, {"three", 3} }; std::map<std::string, int> map; std::transform(values.begin(), values.end(), std::inserter(map, map.end()), [](std::pair<std::string, int> const &p) { return p; }); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{one, 1}
{three, 3}
{two, 2}
We can also use the std::transform algorithm to zip two separate vectors of keys and values together into a map. 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 22 |
#include <iostream> #include <map> #include <vector> #include <algorithm> int main() { std::vector<std::string> keys = {"one", "two", "three"}; std::vector<int> values = {1, 2, 3}; std::map<std::string, int> map; std::transform(keys.begin(), keys.end(), values.begin(), std::inserter(map, map.end()), [](std::string const &s, int i) { return std::make_pair(s, i); }); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{one, 1}
{three, 3}
{two, 2}
That’s all about converting a vector of pairs to 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 :)