Sorting Characters Of A C++ String

前端 未结 4 1104
说谎
说谎 2020-12-22 23:19

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\";
<         


        
4条回答
  •  生来不讨喜
    2020-12-22 23:57

    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 
    #include   // 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

提交回复
热议问题