dinex
dinex

Reputation: 377

expected expression before '['

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!

Upvotes: 0

Views: 808

Answers (1)

NathanOliver
NathanOliver

Reputation: 180424

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.

Upvotes: 2

Related Questions