Need help with sort() - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 38

Thread: Need help with sort()

  1. #16
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by ayumi
    another thing is ..... do i need to delete the vector after used??
    Yes and no...depending where you created the vector...
    Code:
    // Stack
    int main()
    {
      std::vector<int> IntVector;
    
      // No need to explicitely delete the vector, will be done implicitely
    
      return 0;
    }
    
    
    // Heap
    int main()
    {
      std::vector<int> *PointerToIntVector = new std::vector<int>;
    
      // Here we need to explicitely delete the vector, otherwise we will have a memory leak...
      delete PointerToIntVector;
    
      return 0;
    }
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  2. #17
    Join Date
    Apr 1999
    Posts
    27,446
    Originally posted by ayumi
    wow... thanks a lot paul and andreas and those who helped me!!

    another thing is ..... do i need to delete the vector after used??
    The vector will always clean up the memory it has allocated for itself when destroyed. If you place objects in a vector, you don't call delete. You make a vector of your objects, and you don't worry about memory management. Makes the application easier to maintain, and easier to debug.

    However, if you have place pointers in a vector that point to dynamically allocated objects (i.e. you used "new" to create the object), then of course, you are responsible for deleting the objects. The vector<> does not know that the object placed in the vector were created with "new", therefore it can't delete them automatically.

    The rule of thumb is that if your application uses "new" or "new[]", you must use "delete" or "delete[]".

    Regards,

    Paul McKenzie

  3. #18
    Join Date
    Aug 2003
    Posts
    162
    !!
    the sort is not quite right....

    i hav stored for example 2 files in the vector

    abc_1_xyz.txt
    abc_11_xyz.txt

    the sort shows the second file first and then the first 1...
    by right i wanted opposite.

    How do i enhance the sort?

  4. #19
    Join Date
    Apr 1999
    Posts
    27,446
    Originally posted by ayumi
    !!
    the sort is not quite right....
    The sort is correct. The underscore comes after the '1' in the ASCII collating sequence. So I don't know what you are really asking us to do...

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 28th, 2003 at 11:24 PM.

  5. #20
    Join Date
    Aug 2003
    Posts
    162
    sorry for not stating clearly....

    i mean vector [0] stored abc_11_xyz.txt
    vector [1] stored abc_1_xyz.txt

    after using sort.. i wanted [0] to store abc_1_xyz.txt and [1] to store abc_11_xyz.txt.

    How do i do it?

    Thanks

  6. #21
    Join Date
    Apr 1999
    Posts
    27,446
    You are still not making yourself clear. In your first post, you stated that you want a sort, so you have a sort. The sort is correct, a '_' comes after '1' in the ASCII collating sequence. Do you agree or disagree with that statement?

    OK, are you saying you want to sort in descending order and not ascending order? If you store in descending order, the '_' will come before '1', but 'B' will come before 'A', '9' will come before '8', etc. So for example, here is what you'll get

    FOX.TXT
    FIRE_1.TXT
    FIRE11.TXT
    DOG.TXT
    CAT.TXT
    ANIMAL.TXT

    Here the FIRE_1.TXT comes before FIRE11.TXT, but also DOG.TXT comes before CAT.TXT. Is this what you want? If this is not what you want, then what exactly do you want? You can't change the ASCII collating sequence. If you need a specialized sort that doesn't use the ASCII collating sequence, you should explain exactly what your rules are of sorting.

    Regards,

    Paul McKenzie

  7. #22
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646
    This problem is quite common. Usually, the files are created
    in another program. The best solution is to have that program
    create filenames that are compatible with sort routines (whether
    it be sort(), qsort(), or any other routine). That is, instead of
    abc_1_xyz.txt , the code would name it abc_01_xyz.txt

    This assumes that abc_99_xyz is the "largest" possble value. I
    usually put a large number of leading zeroes : abc_00001_xyz.txt


    Given a specific file naming sequence, you can get the sorting routine
    to sort your files like you want - but then, if you run the code
    with files that don not fit the naming convention, it will not work.

    In your case, the sort could use the characters in between the underscore
    characters to decide the order. Here is sample code. Note,
    I don't check that the filename is in the correct format -
    you should probably throw an exception if a problem occurs.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    struct sort_between_underscores
    {
        std::string BetweenUnderscores(const std::string& fname)
        {
            std::string::size_type pos1 = fname.find("_");
            std::string::size_type pos2 = fname.find("_",pos1+1);
            return fname.substr(pos1+1,pos2-pos1-1);
        }
    
        bool operator () (const std::string& lhs, const std::string& rhs)
        {
            return BetweenUnderscores(lhs) < BetweenUnderscores(rhs);
        }
    };
    
    int main()
    {
        std::vector<std::string> fnames;
    
        fnames.push_back("abc_11_xyz.txt");
        fnames.push_back("abc_1_xyz.txt");
        fnames.push_back("abc_111_xyz.txt");
        fnames.push_back("abc_22_xyz.txt");
    
        std::sort(fnames.begin(),fnames.end(),sort_between_underscores());
    
        std::vector<std::string>::iterator it = fnames.begin();
        for (; it!=fnames.end(); ++it)
        {
            std::cout << *it << std::endl;
        }
    
        return 0;
    }

  8. #23
    Join Date
    Aug 2003
    Posts
    162
    sorry .... but that's what i need ...

    abc_1_xyz.txt
    abc_2_xyz.txt
    abc_11_xyz.txt

    I need to get this no matter using what kind of sort ....
    And my filename is not always this format.....

    if array[0] = 11.txt
    array[1] = 1.txt

    i need sort 1.txt to be first

    What i need is to follow the sequence from Windows...

    The filename is obtain from CFileDialog multi select and i stored in array. But CFileDialog did not follow the sequence and i do not know what has happen that's why i'm forced to sort it manually.

    In window the sequence is for example
    abc_1_xyz.txt
    abc_2_xyz.txt
    abc_3_xyz.txt
    abc_11_xyz.txt

    Anyway to make the GetNextPathName(pos) to follow that sequence and store in my array accordingly?

    Thanks.
    Last edited by ayumi; August 29th, 2003 at 08:51 AM.

  9. #24
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646
    Change my previosu code to :

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <sstream>
    
    struct sort_between_underscores
    {
        int ToInt(const std::string& str)
        {
            int x;
            std::stringstream ss(str);
            ss >> x;
            return x;
        }
    
        int BetweenUnderscores(const std::string& fname)
        {
            std::string::size_type pos1 = fname.find("_");
            std::string::size_type pos2 = fname.find("_",pos1+1);
            return ToInt(fname.substr(pos1+1,pos2-pos1-1));
        }
    
        bool operator () (const std::string& lhs, const std::string& rhs)
        {
            return BetweenUnderscores(lhs) < BetweenUnderscores(rhs);
        }
    };
    
    int main()
    {
        std::vector<std::string> fnames;
    
        fnames.push_back("abc_11_xyz.txt");
        fnames.push_back("abc_1_xyz.txt");
        fnames.push_back("abc_111_xyz.txt");
        fnames.push_back("abc_2_xyz.txt");
    
        std::sort(fnames.begin(),fnames.end(),sort_between_underscores());
    
        std::vector<std::string>::iterator it = fnames.begin();
        for (; it!=fnames.end(); ++it)
        {
            std::cout << *it << std::endl;
        }
    
        return 0;
    }

  10. #25
    Join Date
    Aug 2003
    Posts
    162
    the sort works for any type of format?

    like 1.txt 11.txt

    abc1.txt abc11.txt

    any format ?

    thanks for that code ~

  11. #26
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646
    No, not any sequence ... just those that have 2 underscores
    as per your example. By the way, for these files :

    abc_1_xyz.txt
    abc_2_xyz.txt
    abc_3_xyz.txt
    abc_11_xyz.txt

    Windows Explorer on W2K gives this order :

    abc_1_xyz.txt
    abc_11_xyz.txt
    abc_2_xyz.txt
    abc_3_xyz.txt

    Not the order you said ...

  12. #27
    Join Date
    Aug 2003
    Posts
    162
    i'm using win xp...

    but your code i change to other format it seems to works lol!
    how to modify your code to works for any format?

  13. #28
    Join Date
    Apr 1999
    Posts
    27,446
    What i need is to follow the sequence from Windows...
    So the original question is not how to call a sort routine, the question is really what determines the order of items. This is basically what I was trying to ask you -- how do you determine what item comes before another item.

    By default, items are sorted by the operator < unless you override it. Since you are sorting strings, the standard ASCII collating sequence is used to determine what goes where. ASCII knows nothing about how Windows Explorer lays out items -- that is a "feature" of Windows Explorer.

    If you want to sort any other way besides the default, you have to provide the rules of the sort. This is what Philip did -- he didn't change how to call the sort, except that he provided "rules for sorting" in the third parameter to the std::sort() function.

    Regards,

    Paul McKenzie

  14. #29
    Join Date
    Aug 2003
    Posts
    162
    i wanted to sort that's because when i retrieve the filename from the directory i chose from file dialog and store in array, it is not the same order as in window

    window sequence can be anything .....i just wanted to extract the filename out in order of how it is in window directory but it seems that the getpathname function didn't get it out in the same sequence.. hence my array is messed up.

    anyways to extract in sequence of the window? so that i do not need so much trouble to sort. ;(

    Thanks~

  15. #30
    Join Date
    Apr 1999
    Posts
    27,446
    Then what you want is not a "sort", since you don't have any rules laid out as to how to sort. You want to gather names in some order.

    Maybe you should start a brand new thread, and not mention "sort" but just state you want to retrieve the file names in the order that "application x" retrieves them. If it then turns out that there is a way to "sort" them, then it will reveal itself. But to start the thread saying you want to sort has been misleading.

    Regards,

    Paul McKenzie

Page 2 of 3 FirstFirst 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center