To sort a string array in C++ in dictionary order, you can use the `sort()` function from the `
In this example,
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words = {"banana", "apple", "orange", "grape"};
std::sort(words.begin(), words.end());
std::cout << "Sorted Array Ascending:\n";
for (const std::string& word : words) {
std::cout << word << "\n";
}
return 0;
}
Sorted Array Ascending: apple banana grape orange
In this example,
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words = {"banana", "apple", "orange", "grape"};
std::sort(words.begin(), words.end(), std::greater<std::string>());
std::cout << "Sorted Array Descending:\n";
for (const std::string& word : words) {
std::cout << word << "\n";
}
return 0;
}
Sorted Array Descending: orange grape banana apple
In this tutorial, we learned How to Sort a String Array in Dictionary Order in C++ language with well detailed examples.