1

I am trying to sort char array like this:

#include<algorithm>
...
char x[] = "coast";
sort(x, x+5);
std::cout<<x;

But it displays garbage. Integer arrays are sorted properly however.

1
  • Just to be complete you have to do std::cout<<x<<std::endl; so you flush buffers. Commented Dec 25, 2010 at 13:36

1 Answer 1

3

Works fine over here

[~]> clang++ test.cpp
[~]> ./a.out 
acost
Sign up to request clarification or add additional context in comments.

5 Comments

Er, gcc 4.4.3 on ubuntu 10.4 doesnt do it properly.
@Abhi: Please paste the exact and full program (main() and all) you tried, and (if possible) the exact result. This should work.
Works with gcc 4.4.1 on Ubuntu 9.10
Actually, in the code there are other things which i had to comment out. Now it works with only those statements. Thanks.
@Abhi: Which is exactly why you need to create a proper repro case that we can paste into our editors and try ourselves. IMO, in 80% of all cases you find your problem while you create that repro case. For the other 20% you have a perfect piece of code to ask about.

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.