using sort() in STL to sort an array

后端 未结 6 2159
无人共我
无人共我 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:54

    As (like in C) array can be casted to pointer to the first element (but please, do not confuse array with pointer) you can use pointers to determine begin and end, so you write:

    sort(strarr, strarr + len, compare);
    

    or if you use C++11 (or Boost) you can use array class:

    template
    int sortStrarr(std:array& strarr, int len){
        sort(strarr.begin(), strarr.end(), compare);
    }
    

提交回复
热议问题