How to Sort a Vector in a Map in C++?
Last Updated :
09 Feb, 2024
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)
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 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 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