Like Tree4Likes
  • 2 Post By laserlight
  • 2 Post By laserlight

sort question

This is a discussion on sort question within the C++ Programming forums, part of the General Programming Boards category; 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"); ...

  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
    1,283
    What does that code output now?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    You should write a custom comparator that takes the length of the strings being compared into consideration.
    iMalc and MutantJohn like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,304
    Have you tried my suggestion?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    1,283
    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
    Posts
    23,025
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    More succinctly put, this seems to work:

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

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    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.
    iMalc and whiteflags like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,342
    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 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21