CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38

Hybrid View

  1. #1
    Join Date
    Aug 2003
    Posts
    162

    Need help with sort()

    Can i know how to use the <algorithm> sort function in c++?

    Is it possible to use that to sort the order in my string array?

    Thanks

  2. #2
    Join Date
    Apr 2003
    Posts
    1,755

    Smile

    You can use qsort to sort your string array
    Code:
    int sortfunc(const void *elem1, const void *elem2) 
    {   
       return strcmp(*((LPSTR*)elem1), *((LPSTR*)elem2));
    }
    
    ...
    
    LPSTR strs[] = {"String4", "String1", "String3", "String2"};
    qsort(&strs[0], 4, sizeof(strs[0]), sortfunc);
    Hope it will help you

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by rxbagain
    You can use qsort to sort your string array
    Well...one side note....you should not use 'qsort()' with STL types like 'vector' for several reasons like for example:
    • 'qsort()' is not guaranteed to work with non-POD types (structures and classes)
    • 'qsort()' is not type-safe

  5. #5
    Join Date
    Aug 2003
    Posts
    162
    hmm how can i use the code to sort 2D array?
    i need a more simple code on the sort()...
    thanks

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well....how does your array look like?

  7. #7
    Join Date
    Aug 2003
    Posts
    162
    i have a storage[10][100] array....
    storing 10 string.

    i wanted to sort it by name.

    how can i use the code to modify to this?

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    #include <algorithm>
    
    #include <conio.h>
    
    
    int main()
    {
      std::vector<std::string> StringArray;
    
      StringArray.push_back("String One");
      StringArray.push_back("String Four");
      StringArray.push_back("String Two");
      StringArray.push_back("String Three");
    
      std::sort(StringArray.begin(), StringArray.end());
    
      // Print results
      for(std::vector<std::string>::iterator iter = StringArray.begin();
          iter != StringArray.end();
          ++iter)
        std::cout << *iter << std::endl;
    
      // Wait for keystroke
      _getch();
    
      return 0;
    }

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449
    a) You should use std::vector<std::string>. Much easier to handle instead of char[10][100].
    Code:
    #include <algorithm>
    #include <vector>
    #include <string>
    #include <iostream>
    
    typedef std::vector<std::string> StringArray;
    
    using namespace std;
    
    StringArray S(3, "");  
    
    int main()
    {
        S[0] = "Paul";
        S[1] = "John";
        S[2] = "Alan";
        cout << "Before sort:" << endl;
        cout << S[0] << " " << S[1] << " " << S[2] << endl;
    
        std::sort(S.begin(), S.end() );
        cout << "After sort:" << endl;
        cout << S[0] << " " << S[1] << " " << S[2] << endl;
    }
    The problem with sorting a char[10][100] is that the element that you are sorting (a char[100]) is not an l-value, and std::sort() requires that what you're sorting is an l-value. A wrapper for the array is required:
    Code:
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    struct Wrapper
    {
        Wrapper(const char *val="") { strcpy(Char100, val); }
        char Char100[100];
    };
    
    bool SortIt( const Wrapper& first,  const Wrapper& second)
    {
        int result = strcmp(first.Char100, second.Char100);
        return (result < 0)?true:false;
    }
    
    int main()
    {
        Wrapper GlobArray[] = {"Paul", "John", "Alan"};
    
        cout << "Before sort:" << endl;
        cout << GlobArray[0].Char100 << " " << GlobArray[1].Char100 << " " << GlobArray[2].Char100 << endl;
    
        cout << "After sort" << endl;
        std::sort(GlobArray, GlobArray + 3, SortIt);
        cout << GlobArray[0].Char100 << " " << GlobArray[1].Char100 << " " << GlobArray[2].Char100 << endl;
    }
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 28th, 2003 at 05:41 AM.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Andreas Masur
    Well...one side note....you should not use 'qsort()' with STL types like 'vector' for several reasons like for example:
    • 'qsort()' is not guaranteed to work with non-POD types (structures and classes)
    • 'qsort()' is not type-safe
    Not only that, the compiler can optimize std::sort and the functor more aggressively than qsort(). In truth, there is no reason whatsoever to use qsort() in a C++ program unless the compiler's library doesn't have STL (which is rare).

    Even if the compiler doesn't have STL, you still end up having to write your own sort, since as you pointed out, qsort() is not guaranteed to work for non-POD types.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Aug 2003
    Posts
    162
    StringArray S(3, "");

    this is equivilant to S[3][??] ??
    how many characters does it takes in?

    sorry quite new to STL stuff

    Paul: i get 4 warning with your code.....

    warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
    >::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information


    And 1 more serious problem... fstream f.open cannot open the array....

    error C2664: 'fopen' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'


    wad is that? anyways to solve?

    Thanks
    Last edited by ayumi; August 28th, 2003 at 06:34 AM.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by ayumi
    StringArray S(3, "");

    this is equivilant to S[3][??] ??
    how many characters does it takes in?
    A vector is a dynamic array. The vector type is std::string. The vector has 3 std::strings, each initialized to "". A std::string grows dynamically, so the number of characters that can be stored is theoretically unlimited.
    Paul: i get 4 warning with your code.....
    Those warnings only mean that the debugger will recognize only the first 255 characters. They are harmless and can be turned off with

    #pragma warning (disable : 4786)
    And 1 more serious problem... fstream f.open cannot open the array....

    error C2664: 'fopen' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
    std::string whatever;
    fopen( whatever.c_str(), "r");

    The c_str() member function returns a const char *.
    sorry quite new to STL stuff
    Get "Accelerated C++" by Koenig & Moo. You would have learned immediately not to use char[10][100] and instead do what I posted.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by ayumi
    warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
    >::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
    Take a look at this FAQ...

  14. #14
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Since you are new to the 'vector' class, besides the book Paul already mentioned...take a look at this introduction to the vector class...

  15. #15
    Join Date
    Aug 2003
    Posts
    162
    wow... thanks a lot paul and andreas and those who helped me!!

    another thing is ..... do i need to delete the vector after used??

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured