How to Sort a Vector in a Map in C++? Last Updated : 09 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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, Sorted Vector: [1 2 4 6 8 ] Key: 8, Sorted Vector: [1 2 5 8 ]Sort Vector in Map of Vectors in C++To sort vectors in a map of vectors, we can directly use the std::sort() algorithm. We first iterator over the map to access each pair. In each pair, we go to the second element (which is vector) and then apply the sort algorithm on it using its iterators. We keep doing it till we reach the end of the map. C++ Program to Sort Vector in Map C++ // C++ Program to Sort Vectors Inside a Map #include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; int main() { // Declare a map with integer keys and vector of // integers as values map<int, vector<int> > sortedMap; // Assign vectors to map keys sortedMap[5] = { 4, 2, 8, 1, 6 }; sortedMap[3] = { 9, 7, 3 }; sortedMap[8] = { 5, 2, 1, 8 }; // Sort vectors inside the map for (auto& tempPair : sortedMap) { sort(tempPair.second.begin(), tempPair.second.end()); } // Display the sorted map cout << "Sorted Map:" << endl; for (const auto& pair : sortedMap) { cout << "Key: " << pair.first << ", Sorted Vector: ["; for (const auto& element : pair.second) { cout << element << " "; } cout << "]" << endl; } return 0; } OutputSorted Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, Sorted Vector: [1 2 4 6 8 ] Key: 8, Sorted Vector: [1 2 5 8 ] Time Complexity: O(M * N * logN), where M is the number of vectors, and N is the average number of elements in the vector.Space Complexity: (logN) Comment More infoAdvertise with us Next Article How to Sort a Vector in a Map in C++? S shivanshmahajan876 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-map CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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 Vectors as Values in a Map? 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 lea 2 min read How to Store Maps in a Map in C++? In C++, a map is a container that stores elements as a key value and a mapped value. A set is a container that stores unique elements in a particular order. In this article, we will learn how to create a map of sets in C++ STL. Example: myMap: { {1, {{1, "Apple"}, {2, "Banana"}}}, {2, {{3, "Cherry"} 2 min read How to Sort Vector in Ascending Order in C++? Sort a vector in ascending order means arranging the elements of vector in such a way that the first element will be smallest, second element will be second smallest and so on. In this article, we will learn different ways to sort the vector in ascending order in C++.The most efficient way to sort t 3 min read How to Insert an Element in a Sorted Vector in C++? In C++, inserting element in a sorted vector should be done such that it preserves the order of elements. In this article, we will learn different methods to insert an element in a sorted vector in C++.The recommended way to insert an element is to first find the position of insertion using lower_bo 4 min read How to Create a Multimap of Vectors in C++? In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is not required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of vectors in C++. For Example, Input:myPa 2 min read How to Convert a Vector of Pairs to a Map in C++? In C++, a vector of pairs can be converted to a map. This can be useful when you want to create a map from a vector of pairs, where each pair contains a key and a value. In this article, we will learn how to convert a vector of pairs to a map in C++. For Example, Input:myVector = {{1, âoneâ}, {2, ât 2 min read How to Convert Map to a Vector of Pairs in C++? In C++, map containers allow us to store the data in key-value pairs. There might be some cases where you want to convert an entire map into a vector of pairs. In this article, we will learn how to convert a map into a vector of pairs. Example Input: map<int,string>mp ={ {1,"one"}, {2,"two"}, 2 min read How to Sort a Vector of Custom Objects in C++? In C++, vectors are dynamic arrays and sorting a vector of custom objects means arranging the objects in a certain order. In this article, we will learn how to sort a vector of custom objects in C++. Sorting a Vector of Custom Objects in C++To sort a vector of custom objects in C++, we can use the s 2 min read How to Maintain the Order of a Vector in a Multiset in C++? In C++, multisets are associative containers that store elements in a sorted manner but unlike sets, multisets allow the users to store duplicate values as well and vectors are dynamic arrays that store data in contiguous memory locations. In this article, we will learn how to maintain the order of 3 min read Like