How to copy a vector: In the previous article, we have discussed about CPP std::Vector and Iterator Invalidation Example. Let us learn how to Copy all Values from a Map to a Vector in C++ Program.
Map:
Maps are associative containers that store items in a mapped manner. A key-value pair is used to store all of the elements in a map, with each key being unique. Keys are used to sort the data, and each key has a value associated with it. As and when required, values can be inserted and removed.
Vector:
A C++ Vector is a dynamic array that can automatically resize itself. The resizing happens after an element is added or removed from the vector. The container handles the storage automatically. A vector’s elements are contained in contiguous storage. This enables C++ programmers to use iterators to reach and traverse the vector components.
At the tail of a vector, new data is inserted. This necessitates a time difference. The removal of an element from a vector takes a fixed amount of time. The explanation for this is that the vector does not need to be resized. Inserting or removing an element from the vector’s beginning takes linear time.
Copy all Values from a Map to a Vector
There are several ways to copy values from map to vector some of them are:
- Using Copy function
- Using range based for loop(Copying values)
- Using for_each and Lambda function(copying values)
- Using transform and Lambda function(copying values)
Method #1:Using Copy function
The standard algorithm std::copy() is recommended if we already have a vector with enough space to hold all of the map entries.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; template <typename K, typename V> vector<pair<K, V> > convertMaptoVector(const map<K, V>& given_map) { vector<pair<K, V> > vect; // resize the veector with given map size vect.resize(given_map.size()); // copying all contents of map to vector using copy copy(given_map.begin(), given_map.end(), vect.begin()); return vect; } int main() { // create and intialize map with strings as // keys and integer as values map<std::string, int> stringmap({ { "Hello", 50 }, { "This", 100 }, { "is", 150 }, { "BTech", 220 }, { "Geeks", 250 } }); // passing map to convertMaptoVector function vector<pair<string, int> > vect; vect = convertMaptoVector(stringmap); for (auto value : vect) cout << value.first << " " << value.second << endl; return 0; }
Output:
BTech 220 Geeks 250 Hello 50 This 100 is 150
Method #2: Using range based for loop(Copying values)
We can copy all values of given map to vector using range based for loop.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // create and intialize map with strings as // keys and integer as values map<std::string, int> stringmap({ { "Hello", 50 }, { "This", 100 }, { "is", 150 }, { "BTech", 220 }, { "Geeks", 250 } }); vector<int> vect; // using range based for loop to copy all values from // map to vector for (auto value : stringmap) vect.push_back(value.second); // printing the vector values for (auto value : vect) cout << value << endl; return 0; }
Output:
220 250 50 100 150
Method #3:Using for_each and Lambda function(copying values)
Iterate through the map entries, calling the lambda function on each one. Which will add the second field to the vector from the given pair.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // create and intialize map with strings as // keys and integer as values map<std::string, int> stringmap({ { "Hello", 50 }, { "This", 100 }, { "is", 150 }, { "BTech", 220 }, { "Geeks", 250 } }); vector<int> vect; vect.reserve(stringmap.size()); // using for_each and lambda function for_each(stringmap.begin(), stringmap.end(), [&](pair<const string, int>& ele) { vect.push_back(ele.second); }); // printing the vector values for (auto value : vect) cout << value << endl; return 0; }
Output:
220 250 50 100 150
Method #4:Using transform and Lambda function(copying values)
Iterate through the map entries, calling the lambda function on each one. Which will add the second field to the vector from the given pair.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // create and intialize map with strings as // keys and integer as values map<std::string, int> stringmap({ { "Hello", 50 }, { "This", 100 }, { "is", 150 }, { "BTech", 220 }, { "Geeks", 250 } }); vector<int> vect; vect.reserve(stringmap.size()); // using transform and lambda function transform(stringmap.begin(), stringmap.end(), back_inserter(vect), [](pair<string, int> const& pair) { return pair.second; }); // printing the vector values for (auto value : vect) cout << value << endl; return 0; }
Output:
220 250 50 100 150
Related Programs:
- how to erase elements from a list in cpp using iterators
- java how to get all keys by a value in hashmap search by value in map
- python how to create a list of all the values in a dictionary
- cpp how to find duplicates in a vector
- how to remove substring from a string in cpp
- how to fill a vector with random numbers in cpp
- how to check if a given key exists in a map cpp