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

Printable View

• October 30th, 2012, 08:18 AM
ejohns85
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!
• October 30th, 2012, 08:44 AM
Paul McKenzie
Re: stl::sort on a two-dimensional stl::vector
Quote:

Originally Posted by ejohns85
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(); }```
Quote:

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
• October 30th, 2012, 09:09 AM
Paul McKenzie
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