HOWTO · C++
How to Sort a String of Characters in C++
This article demonstrates how to sort a string of characters in C++.
This guide will explain several methods of how to sort a string of characters in C++.
Use the std::sort Algorithm to Sort the String of Characters in C++
In this article, we assume that the sequence of characters is stored in a std::string object. Since the std::string class object is iterable, we can call any range-based STL functions on it. In this case, we use the std::sort function from the STL algorithms on each string. Here, we utilize the simplest overload of the std::sort function, which takes two iterator arguments to traverse the range and sort the elements by default in non-descending order.
#include <algorithm>
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
template <typename T>
void printVector(vector<T> &vec) {
for (const auto &item : vec) {
cout << item << "; " << endl;
}
cout << endl;
}
int main() {
vector<string> vec1 = {"algorithms library", "occurrences",
"implementation-specific", "contribute",
"specialization"};
for (auto &item : vec1) {
sort(item.begin(), item.end());
}
printVector(vec1);
return EXIT_SUCCESS;
}
Output:
aabghiillmorrrsty;
ccceenorrsu;
-acceeefiiiilmmnnoppstt;
bceinorttu;
aaceiiilnopstz;
Alternatively, we can pass the custom comparator function to the std::sort algorithm to sort the elements accordingly. Note that the function prototype should have the following form: bool cmp(const Type1 &a, const Type2 &b);. In the following example code, we utilize the lambda expression to invert the sorting order to descending.
#include <algorithm>
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
template <typename T>
void printVector(vector<T> &vec) {
for (const auto &item : vec) {
cout << item << "; " << endl;
}
cout << endl;
}
int main() {
vector<string> vec1 = {"algorithms library", "occurrences",
"implementation-specific", "contribute",
"specialization"};
for (auto &item : vec1) {
sort(item.begin(), item.end(), [](auto &c1, auto &c2) { return c1 > c2; });
}
printVector(vec1);
return EXIT_SUCCESS;
}
Output:
ytsrrromlliihgbaa ;
usrroneeccc;
ttspponnmmliiiifeeecca-;
uttroniecb;
ztsponliiiecaa;
Use the Custom Function Wrapper to Sort the String of Characters in C++
One noticeable flaw of the previous solution is that it can’t distinguish the punctuation and spacing of characters from the valid alpha-numeric characters. So, we can implement a separate function where we discard all punctuation and space characters from the given string object and then call the std::sort algorithm to conduct the sorting operation. The removal operation is done using the erase-remove_if idiom, which takes a lambda expression to check each character’s type. The isspace and ispunct functions are utilized included from the <locale> header file.
#include <algorithm>
#include <iostream>
#include <locale>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
template <typename T>
void printVector(vector<T> &vec) {
for (const auto &item : vec) {
cout << item << "; " << endl;
}
cout << endl;
}
void sortStringChars(string &s) {
s.erase(std::remove_if(
s.begin(), s.end(),
[](auto &c) { return std::isspace(c) || std::ispunct(c); }),
s.end());
sort(s.begin(), s.end());
}
int main() {
vector<string> vec1 = {"algorithms library", "occurrences",
"implementation-specific", "contribute",
"specialization"};
for (auto &item : vec1) {
sortStringChars(item);
}
printVector(vec1);
return EXIT_SUCCESS;
}
Output:
aabghiillmorrrsty;
ccceenorrsu;
acceeefiiiilmmnnoppstt;
bceinorttu;
aaceiiilnopstz;