4

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 very simple, since vector has iterator and can be used in STL sort function which is the code below: But what if the container is an array? Array has no iterator and cannot use sort() to sort the array directly. I would like to know is there any way to create a array iterator so that I can use sort() to sort the array directly? Thank you !

#include <iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

bool compare(string s1, string s2){
  sort(s1.begin(), s1.end());  //sort return void, not the sorted result!!!!!!!!!!
  sort(s2.begin(), s2.end());
  return s1<=s2;
}


void sort_string(vector<string> &v){
    sort(v.begin(), v.end(), compare);
}
#
If I want to use array itertor:

bool compare(string s1, string s2){
        sort(s1.begin(), s1.end());
        sort(s2.begin(), s2.end());
        return s1<=s2;
}


int sortStrarr(string strarr[], int len){

    //sort(strarr's iterator.begin, strarr's iterator.end, compare); ???
}

6 Answers 6

8

Pointers can act as iterators, so you just need pointers to the beginning and just past the end of the array.

Sign up to request clarification or add additional context in comments.

1 Comment

that is, sort(strarr, strarr+len) will do, thanks to pointer-arithmetic
6

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<std::size_t N>
int sortStrarr(std:array<string, N>& strarr, int len){
    sort(strarr.begin(), strarr.end(), compare);
}

1 Comment

I'm not yet converted to C'11.. but are you sure that's strarr[] with brackets..? It would make the type std::array<string,N>[] which looks for me as an array-of-stdarray..
3

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

Example:

#include <iostream>
#include <string>
#include <algorithm>
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

Comments

0
int sortStrarr(string strarr[], int len){
     sort(&strarr[0],&strarr[len],compare) ;
    //sort(strarr's iterator.begin, strarr's iterator.end, compare); ???
}

Comments

0

We have to use sort function to sort the string array.

int main()
{
   string arr[5] = {"e", "b", "c", "d", "a'};
   sort(arr, arr+5);
   for(int i=0; i<5; i++)
   {
     cout<<arr.at(i)<<" ";
   }
   return 0;
}

Comments

0

The free functions std::begin() and std::end() (since C++11) are specialized for array types, but the size must be known to the compiler:

template <typename T, std::size_t N>
int sortStrarr(T array[N])
{
    // 'using' allows ADL to select best overload
    using std::begin;
    using std::end;
    std::sort(begin(array), end(array));
}

If you have only the start and (run-time) size, then you can use pointers as the iterators:

template <typename T>
int sortStrarr(T array[], size_t len)
{
    std::sort(array, array+len);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.