Thread: sort question

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    sort question

    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <string> 
    using namespace std;
    int main() 
    {
      std::vector<string> vec;
      vec.push_back("dd"); vec.push_back("adfd"); vec.push_back("aaa"); 
      vec.push_back("aa"); 
      std::sort(vec.begin(), vec.end());
      for (int i = 0; i < vec.size(); ++i) 
         std::cout << vec[i] << ' ';
    }
    I want to output
    aa
    dd
    aaa
    adfd


    How to design codes?

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    What does that code output now?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should write a custom comparator that takes the length of the strings being compared into consideration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by MutantJohn View Post
    What does that code output now?
    aa aaa adfd dd

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you tried my suggestion?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by zcrself View Post
    aa aaa adfd dd
    Ah, I see. Then you should without a doubt follow laserlight's advice and use a custom comparator that incorporates string length. I think std::sort() actually has an implementation of this, doesn't it? It's something with an argument list like std::sort(iterator & A, iterator & B, comparison_function), isn't it? That way you can sort by string length as well.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it has an overloaded function that takes a comparator as a 3rd argument.
    std::sort - cppreference.com
    The other alternative would be to overload operator < for the type you're trying to sort (unless it's a built-in such as int, std::string, etc).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by laserlight View Post
    Have you tried my suggestion?
    I do appreciate your suggestion, thanks a lot
    Code:
    bool cmp1(string a, string b)
    {
        if(a.length()==b.length())
        {
             return a < b;
        }else
        {
             return a.length() < b.length();
        }
    }
    
    std::sort(vec.begin(), vec.end(),cmp1);

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Elysia View Post
    Yes, it has an overloaded function that takes a comparator as a 3rd argument.
    std::sort - cppreference.com
    The other alternative would be to overload operator < for the type you're trying to sort (unless it's a built-in such as int, std::string, etc).
    thanks for your suggestion!

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    More succinctly put, this seems to work:

    return a.length() <= b.length() && a < b;
    Last edited by whiteflags; 08-28-2013 at 08:36 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    More succinctly put, this seems to work:

    return a.length() <= b.length() && a < b;
    Assuming an ASCII-based character set, zcrself's code will return true for cmp1("b", "ab"). Your expression will evaluate to false instead. Furthermore, your expression will also evaluate to false for cmp1("ab", "b"), i.e., it indicates that "b" and "ab" are equal according to your comparator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by whiteflags View Post
    More succinctly put, this seems to work:

    return a.length() <= b.length() && a < b;
    Code:
    bool cmp1(string a, string b)
    {
         return a.length() <= b.length() && a < b;
    }
    int main()
    {
      std::vector<string> vec;
      vec.push_back("ab"); vec.push_back("b"); vec.push_back("aaa");
      vec.push_back("aa");
      std::sort(vec.begin(), vec.end(),cmp1);
      for (int i = 0; i < vec.size(); ++i)
         std::cout << vec[i] << ' ';
    }
    output is:
    Code:
    aa ab b aaa

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by zcrself View Post
    output is:
    Code:
    aa ab b aaa
    I think the output is supposed to be
    Code:
    b aa ab aaa
    which what zcrself's version does.
    Last edited by rcgldr; 08-29-2013 at 09:04 PM.

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by rcgldr View Post
    I think the output is supposed to be
    Code:
    b aa ab aaa
    which what zcrself's version does.
    Code:
    Dev-c++ 4.9.9.2
    
    windows xp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL list.sort() question
    By dudeomanodude in forum C++ Programming
    Replies: 14
    Last Post: 01-23-2008, 02:48 PM
  2. Simple Sort Question
    By krack_kills in forum C Programming
    Replies: 6
    Last Post: 02-25-2005, 02:19 AM
  3. cin Help...Sort of a noob Question?
    By Krak in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2003, 01:23 PM
  4. Radix Sort question
    By ripper079 in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2003, 06:58 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM