2

Anyone knows how to sort a char* using std::sort() STL function? In c++

If i use sort like sort(str.begin(),str.end());. An error is coming -'request for member begin and end in str, which is of non-char type 'char*'.

I know how to sort a string using std::sort() in #include<algorithm>

3
  • 2
    All std::sort needs is a "pointer" to the first element, and a "pointer" to one beyond the last element. For a null-terminated byte string it's very easy to get both of these pointers. Commented May 24, 2020 at 16:23
  • If i use sort like sort(str.begin(),str.end());. An error is coming -'request for member begin and end in str, which is of non-char type 'char*'. Commented May 24, 2020 at 16:27
  • 1
    Note the word pointer in my comment... Commented May 24, 2020 at 16:36

2 Answers 2

5

If you have a pointer to the beginning of a string, you can pass it directly to std::sort as the first parameter.

Then the second parameter needs to be the pointer to end of the string. You can get it by adding string length to that pointer.

std::sort(ptr, ptr + std::strlen(ptr));
Sign up to request clarification or add additional context in comments.

6 Comments

Can you also tell how to implement std::unique() for the same
@silkynene Just about all algorithm functions deal with ranges using a "begin" iterator and an "end" iterator. For a C-style null-terminated string the "begin" iterator is the pointer to the first character, and the "end" iterator is a pointer to the terminator. Using the that information, together with the information from this answer, what does that tell you? How do you think you should call e.g. std::unique (which expects basically the same arguments)?
std::unique(ptr, ptr+std::strlen(ptr)); but how to return this?
@silkynene It will return a pointer, so char *new_end = std::unique(...);. The new string size is then new_end - ptr. You might also want to insert a null-terminator at the new end (*new_end = '\0';).
How to return this value?
|
1

You can sort char* strings, but you can't modify string literals, so you need to have the string as a char array to be able to modify it (including sort):

//this is a string literal that can't be modified
const char* string_literal = "this can't be modified";

// this is a char[] that is initialized from a string literal
char str1[] = { "can modify" };
//array can be converted to pointer to first element
char* str_ptr = str1;
//str1 can be directly passed to sort, i just added str_ptr to make the conversion explicit
std::sort(str_ptr, str_ptr + std::strlen(str_ptr));

//the method with std::begin only works for char[], doesn't work for char*
//this could be initialized with a string literal as in previous example
char str2[] = { 'c', 'a', 'n', ' ', 'm', 'o', 'd', 'i', 'f', 'y', '\0' };

//end -1 because we want to keep the last character in place (the end string null termination character)
std::sort(std::begin(str2), std::end(str2) - 1);

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.