C++ Sort a Vector Based on Lengths
You can use the std::sort
function from the algorithm library with a custom comparison function to sorting a vector based on the lengths of its elements.
Syntax of std::sort
The syntax to import the required libraries and use the std::sort function with the custom comparison function is as shown in the following.
#include <algorithm>
#include <vector>
#include <string>
// Sort a vector based on lengths using a custom comparison function
std::sort(vector.begin(), vector.end(), [](const auto& a, const auto& b) {
return a.size() < b.size();
});
Examples
Example 1: Sort a Vector of Strings by Length in Ascending Order of Lengths
This example demonstrates sorting a vector of strings in ascending order of their lengths using a lambda expression as the comparison function.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main() {
std::vector<std::string> words = {"apple", "banana", "kiwi", "cherry", "grape"};
// Sort the vector based on lengths in ascending order
std::sort(words.begin(), words.end(), [](const std::string& a, const std::string& b) {
return a.size() < b.size();
});
std::cout << "Strings sorted by length (ascending):" << std::endl;
for (const auto& word : words)
std::cout << word << " ";
std::cout << std::endl;
return 0;
}
Output:
Strings sorted by length (ascending):
kiwi apple grape banana cherry
Explanation:
-
Initialize the vector:
A
std::vector
namedwords
is created and initialized with strings:{"apple", "banana", "kiwi", "cherry", "grape"}
. This vector contains words of varying lengths to demonstrate sorting by their lengths. -
Sort the vector by string length:
The
std::sort
function is called with three arguments:words.begin()
: Specifies the starting point of the range to be sorted.words.end()
: Specifies the end of the range to be sorted.- A lambda expression: Defines the custom sorting logic that compares the lengths of two strings,
a
andb
. It returnstrue
if the length ofa
is less than the length ofb
, ensuring ascending order by length.
-
Print the sorted vector:
A range-based
for
loop iterates over the sorted vector, printing each word to the console usingstd::cout
. The words are displayed separated by spaces, preceded by the message"Strings sorted by length (ascending):"
.
Example 2: Sort a Vector of Strings by Length in Descending Order of Lengths
This example demonstrates sorting a vector of strings in descending order of their lengths using a lambda expression.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main() {
std::vector<std::string> words = {"apple", "banana", "kiwi", "cherry", "grape"};
// Sort the vector based on lengths in descending order
std::sort(words.begin(), words.end(), [](const std::string& a, const std::string& b) {
return a.size() > b.size();
});
std::cout << "Strings sorted by length (descending):" << std::endl;
for (const auto& word : words)
std::cout << word << " ";
std::cout << std::endl;
return 0;
}
Output:
Strings sorted by length (descending):
banana cherry apple grape kiwi
Example 3: Stable Sorting a Vector of Strings by Length
You can use std::stable_sort
to sort a vector of strings by their lengths while preserving the relative order of strings with equal lengths.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main() {
std::vector<std::string> words = {"kiwi", "apple", "pear", "banana", "grape"};
// Stable sort the vector based on lengths
std::stable_sort(words.begin(), words.end(), [](const std::string& a, const std::string& b) {
return a.size() < b.size();
});
std::cout << "Strings stably sorted by length:" << std::endl;
for (const auto& word : words)
std::cout << word << " ";
std::cout << std::endl;
return 0;
}
Output:
Strings stably sorted by length:
kiwi pear apple grape banana
Example 4: Sorting a Vector of Numbers Based on Length
This example demonstrates sorting a vector of numbers, where each number is represented as a string, based on the length of the strings. The shorter strings appear first in ascending order of their lengths.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main() {
// Initialize a vector of numbers represented as strings
std::vector<std::string> numbers = {"123", "45", "7", "8901", "56", "789", "2"};
// Sort the vector based on the length of the strings
std::sort(numbers.begin(), numbers.end(), [](const std::string& a, const std::string& b) {
return a.size() < b.size(); // Ascending order of string lengths
});
std::cout << "Numbers sorted by length:" << std::endl;
for (const auto& number : numbers)
std::cout << number << " ";
std::cout << std::endl;
return 0;
}
Output:
Numbers sorted by length:
7 2 45 56 123 789 8901
Explanation:
Sort the vector: The std::sort
function is used with a custom lambda expression to sort the strings by their lengths. The lambda function compares the lengths of two strings, an
and b
, and returns true
if the length of a
is less than the length of b
. This results in sorting the vector in ascending order of string lengths.