If i have a string is there a built in function to sort the characters or would I have to write my own?
for example:
string word = "dabc";
I would want to change it so that:
string sortedWord = "abcd";
Maybe using char is a better option? How would I do this in C++?
std::sort(str.begin(), str.end());
See here
You have to include sort
function which is in algorithm
header file which is a standard template library in c++.
Usage: std::sort(str.begin(), str.end());
#include <iostream>
#include <algorithm> // this header is required for std::sort to work
int main()
{
std::string s = "dacb";
std::sort(s.begin(), s.end());
std::cout << s << std::endl;
return 0;
}
OUTPUT:
abcd
Source: Stackoverflow.com