How to Store Vectors as Values in a Map? Last Updated : 16 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, vectors are similar to arrays, but unlike arrays, the vectors are dynamic in nature. They can modify their size during the insertion or deletion of elements. On the other hand, Maps are containers that allow the users to store data in the form of key-value pairs. In this article, we will learn how to store vectors as values in maps in C++. Example: Input: myVector1 = {40,50,60} myVector2 = {10,20,30} Output: myMap = { {1: {40,50,60}}, {2: {10,20,30}} }Store Vectors as Values in a Map in C++ To store vectors as values in maps, we have to first declare the map of vectors as: map<key_type, vector<type>> Then we will add the vector with the keys of your preference. C++ Program to Store Vectors as Values in a Map C++ // C++ program to demonstrate how to insert vector as values // in a map #include <iostream> #include <map> #include <vector> using namespace std; int main() { // create vectors vector<int> myVector1 = { 1, 2, 3 }; vector<int> myVector2 = { 10, 20, 30, 40 }; // Create a map with string keys and vector<int> values map<string, vector<int> > myMap; // Insert multiple vectors into the map myMap["Vector 1"] = myVector1; myMap["Vector 2"] = myVector2; cout << "Vector Values: "; // Access and print the vectors stored in the map for (const auto& pair : myMap) { cout << pair.first << ": "; for (int num : pair.second) { cout << num << " "; } cout << endl; } return 0; } Outputvector1: 1 2 3 vector2: 10 20 30 40 Time Complexity: O(N), where N is the total number of elements in all vectors inserted into the map.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Store Vectors as Values in a Map? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-array cpp-map CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Sort a Vector in a Map in C++? In C++, we can create a map container where the values associated with keys is a vector. In this article, we will learn how to sort a vector within a map in C++. Example Input: myMap = { {3, {9, 7, 3}}, {5, {4, 2, 8, 1, 6}}, {8, {1, 2, 5, 8}} }; Output: Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, S 2 min read How to Store Vectors as Keys in a Multimap in C++? In C++, the std::multimap is a container that stores elements in a key-value pair, whereas std::vector is a sequence container that stores elements in contiguous memory. In this article, we will learn how to store vectors as keys in a multimap in C++. Example: Input:myVector ={1,2,3};myVector ={4,5, 2 min read How to Store Data Triplet in a Vector in C++? Given a vector, how can we store 3 elements in one cell of vector.ExampleInput: { {2, 31, 102}, {5, 23, 114}, {9, 10, 158} }Output: 2 31 102 // In first cell of vector 5 23 114 // In second cell of vector 9 10 158 // In second cell of vectorOne solution is to create a user defined class or structure 3 min read How to Create a Map with Vectors as Keys and Sets as Values? In C++, maps, vectors, and sets all are the data storage containers that are provided by the C++ STL. We can nest these containers to create a more complex data structure. In this article, we will explore how to create a Map with vectors as keys and sets as values in C++. Example Input: myVector1 = 3 min read How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in 2 min read Like