0

I am a newbie in programming. Recently I try to use the sorting function from c++ sort keeping track of indices

template <typename T>
std::vector<size_t> ordered(std::vector<T> const& values) {
std::vector<size_t> indices(values.size());
std::iota(begin(indices), end(indices), static_cast<size_t>(0));

std::sort(
    begin(indices), end(indices),
    [&](size_t a, size_t b) { return values[a] < values[b]; }
);
return indices;
}

In Xcode, it is successfully compiled without any warnings. While in g++, it shows the following error message:

error: expected expression
          [&](size_t a, size_t b) { return values[a] < values[b];}
          ^

What does it imply? Thanks!

Community
  • 1
  • 1
dinex
  • 367
  • 5
  • 15

1 Answers1

2

begin and end reside in the std namespace. You need to qualify them:

std::sort(
    std::begin(indices), std::end(indices),
    [&](size_t a, size_t b) { return values[a] < values[b]; }
);

Also lambdas are a C++11 feature so you need to compile with -std=c++11 to use them.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402