To sort a list of strings in C++ using `std::list`, you can use the `std::list.sort` function.
In this example,
std::list
.std::list
named words1
with some strings.sort
function of std::list
to sort the list in dictionary order.#include <iostream>
#include <list>
#include <algorithm>
int main() {
std::list<std::string> words1 = {"banana", "apple", "grape", "orange"};
words1.sort();
for (const auto& word : words1) {
std::cout << word << " ";
}
return 0;
}
apple banana grape orange
In this example,
std::list
.std::list
named words2
with different strings.sort
function of std::list
to sort this list in dictionary order.#include <iostream>
#include <list>
#include <algorithm>
int main() {
std::list<std::string> words2 = {"pear", "kiwi", "lemon", "apple"};
words2.sort();
for (const auto& word : words2) {
std::cout << word << " ";
}
return 0;
}
apple kiwi lemon pear
In this tutorial, we learned How to Sort a List of Strings in Dictionary Order in C++ language with well detailed examples.