1

I am trying to sort a vector of indexes, sorting it the same exact way as my regular vector is already sorted. This statement works correctly and compiles correctly in visual studio, but I need it to compile in Linux, so I need to somehow rewrite the statement to work with an older version of the compiler. The index vector is declared and initialized by this, which still works fine on the old compiler:

vector<float> indexes(toBeSorted.size());
//initialize original index locations
for (float i = 0; i != indexes.size(); ++i) indexes[i] = i;

The question is this line, which breaks in Linux:

sort(indexes.begin(), indexes.end(), [toBeSorted](float i1, float i2) {return toBeSorted[i1] > toBeSorted[i2]; });

Please help!

14
  • 1
    That line doesn't magically break in Linux unless it's doing something like invoking undefined behaviour. If you mean it doesn't compile, that's not Linux's problem. Commented Dec 3, 2013 at 14:21
  • 1
    which compiler are we talking about on Linux? Commented Dec 3, 2013 at 14:22
  • 1
    What compiler are you using? If GCC, try the -std=c++0x option to enable C++11 features. Commented Dec 3, 2013 at 14:22
  • 1
    You're using a lambda expression, which is C++11. If your compiler is g++ 4.8 or newer, you can add the compiler flag -std=c++11 to compile it directly. Commented Dec 3, 2013 at 14:22
  • @JoeZ, If you mean 4.8 for the flag, it works as of 4.7.1 if I'm not mistaken. Lambdas were supported long before that. Commented Dec 3, 2013 at 14:23

1 Answer 1

2

Use a functor:

struct Sorter
{
  bool operator()(...)
  { /* logic * / }
};

This should still work in an older version of the compiler...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.