0

I have some data stored in vector ;

 vector<string> data ;

I want to sort strings in data, alphabetically.Are there any sorting algorithm or complex library which you can recommend me?

Ex ;

assume in data ;
 aaaa
 ccc
 bbb
 acb
after sorting, data ;
 aaaa
 acb
 bbb
 ccc 
luqui
60.7k12 gold badges151 silver badges210 bronze badges
asked Mar 7, 2011 at 7:28
4
  • 2
    What is Vector? Did you write it yourself? Did you mean vector (lowercase) from the STL? It's hard to know how to help you without more details about this type. Commented Mar 7, 2011 at 7:31
  • 1
    Out of curiosity, why would you explicitly be asking for a complex library to solve a simple problem? Commented Mar 7, 2011 at 7:36
  • @Space_COcbOy, because I will use it with more complex problem.Also, for some easy part of the problem,I will use basic "standart library sort function" to get rid of efficiency problem .( Now I have searching all of the sorting algorithm before starting to write program. ) Commented Mar 7, 2011 at 7:40
  • 2
    Why don't you just use std::sort and forget about the issue until you recognize it as a problem? Note that your set of requirements might be ill-defined, if you demonstrate that you do need something faster than std::sort, then you most probably don't want to use a std::vector (BTW, if you prepend standard types with std:: in questions, it will be clear that you refer to std::vector and not homebrew::vector) Commented Mar 7, 2011 at 8:50

4 Answers 4

8

The Standard library's <algorithm> header has a sort function you can use, see: http://www.cplusplus.com/reference/algorithm/sort/

answered Mar 7, 2011 at 7:31

Comments

1

Since you are aiming for efficiency even before starting to write a program, do it other way around : first write a correct program, then profile it and optimize bottlenecks.

Therefore, I can only recommend what other already did : std::sort

answered Mar 7, 2011 at 8:02

Comments

1

Normal std::sort should do the job for you, see this snippet on ideone.

answered Mar 7, 2011 at 7:36

Comments

0

You can use trie trees http://en.wikipedia.org/wiki/Trie

answered Mar 7, 2011 at 7:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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