-
October 30th, 2012, 08:18 AM
#1
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:
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|