stl::sort on a two-dimensional stl::vector
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: stl::sort on a two-dimensional stl::vector

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    98

    stl::sort on a two-dimensional stl::vector

    Hello,

    Suppose I have a two-dimensional stl vector (a vector of vectors), for example:

    Code:
    vector<vector<int> > x;
    And then I want to sort the outer vectors in order of the size of their inner vectors. How do I do this?

    Usually, with a one-dimensional vector, I can just create a comparison function and use the sort function. For example, if my vector is defined as:

    Code:
    vector<int> y;
    And I want to sort if in terms of the int values of each vector element, then I can sort by using the stl::sort function, with the comparison function defined as:

    Code:
    static bool SortFunction(int val1, int val2)
    {
         if (val1 > val2)
         {
              return true;
         }
         else
         {
              return false;
         }
    };
    However, with my two-dimensional vector, I use the following comparison function:

    Code:
    static bool SortFunction(vector<int> vec1, vector<int> vec2)
    {
         if (vector1.size() > vector2.size())
         {
              return true;
         }
         else
         {
              return false;
         }
    };
    I get a runtime error:

    "Debug Assertion Failed!
    File: c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm
    Line 3686
    Expression: invalid operator<"

    Why is this?

    Thanks!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,434

    Re: stl::sort on a two-dimensional stl::vector

    Quote Originally Posted by ejohns85 View Post
    However, with my two-dimensional vector, I use the following comparison function:
    Code:
    static bool SortFunction(vector<int> vec1, vector<int> vec2)
    {
         if (vector1.size() > vector2.size())
         {
              return true;
         }
         else
         {
              return false;
         }
    };
    Why are you passing the vectors by value?

    You should pass vectors by reference or const reference, not by value. Every time that sort function is called, it has to make temporary copies of the vector, all because you're passing them by value instead of by const reference. In general you shouldn't pass objects by value unless you know you must pass them by value (which is rare -- the objects that usually do get passed by value are function objects).

    Second, why are you ending your function block with a semicolon?

    Third, the code is a one-liner.
    Code:
    static bool SortFunction(const std::vector<int>& vec1, const std::vector<int>& vec2)
    {
        return vector1.size() > vector2.size();
    }
    I get a runtime error:

    "Debug Assertion Failed!
    File: c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm
    Line 3686
    Expression: invalid operator<"

    Why is this?
    A function can't just live by itself -- it needs code to call it. Show us exactly how you're using this function, better yet, show us the entire program that duplicates the error.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 1999
    Posts
    27,434

    Re: stl::sort on a two-dimensional stl::vector

    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    typedef std::vector<int> IntVector;
    typedef std::vector<IntVector> IntVector2D;
    
    bool SortFunction(const IntVector& v1, const IntVector& v2)
    {  
        return v1.size() > v2.size();
    }
    
    int main()
    {
        IntVector2D my2D;
    
        // push on vectors of different sizes
        my2D.push_back(IntVector(2));
        my2D.push_back(IntVector(1));
        my2D.push_back(IntVector(20));
        my2D.push_back(IntVector(15));
    
        // sort them by size
        std::sort(my2D.begin(), my2D.end(), SortFunction);
    
        // output results
        for (size_t i = 0; i < my2D.size(); ++i )
             std::cout << "The number of items in IntVector(" << i << ") = " << my2D[i].size() << "\n";
    }
    Code:
    Output:
    The number of items in IntVector(0) = 20
    The number of items in IntVector(1) = 15
    The number of items in IntVector(2) = 2
    The number of items in IntVector(3) = 1
    This is an example of a full program. As you can see, it works with no issues.

    More than likely, the real program that you have that attempts to sort is sorting invalid entries, or you've totally messed up your containers in some way and you're giving the std::sort garbage data.

    The std::sort works every time it's tried, but only if you give it valid data. That's why it's important to show your full program and not just the sort function.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 30th, 2012 at 09:17 AM.

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