![]() |
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}}, 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 MapC++
Output
Sorted 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. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |