using sort() in STL to sort an array

后端 未结 6 2130
无人共我
无人共我 2020-12-10 19:31

I am writing code for a question which is: Write a method to sort an array of strings so that all the anagrams are next to each other. If my container is vector, it will be

6条回答
  •  旧巷少年郎
    2020-12-10 19:49

    You can use sort() for an array. Pointers act as iterators.

    Example:

    #include 
    #include 
    #include 
    using namespace std;
    
    int main()
    {
        string arr[5]={"BBB","AAA","CCC","FFF", "EEE"};
        sort(arr,arr+5);
        for(string i: arr)
        {
            cout << i << endl;
        }
    }
    

    and the output is:

    AAA
    BBB
    CCC
    EEE
    FFF
    

提交回复
热议问题