user1993285
user1993285

Reputation: 1

keep indexes before sorting of array c

Hi I use this code for my store indexes before the sorting, but when i COmpile in this row : :

    [&](size_t a, size_t b){ return values[a] < values[b]; }

, says to me error:

        Multiple markers at this line
- expected primary-expression before 
 '[' token
- expected primary-expression before 
 ']' token
- expected primary-expression before 
 'a'
- expected primary-expression before 
 'b'

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

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

);
return indices;
 }

And second question how can i this call , when I have the classic input 1d array

Upvotes: 0

Views: 111

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

This [...](...) { ... } syntax is a lambda expression, a feature of C++11. You need to make sure you're using a compiler that supports them. Some compilers provide a switch to enable C++ features (-std=c++0x for GCC).

If you want to use std::sort on a normal array, use:

std::sort(array, array + array_size, comp);

Alternatively, you can make your code agnostic as to whether you're using a container or an array. Where ac is an array or container:

std::sort(std::begin(ac), std::end(ac), comp);

Upvotes: 2

Related Questions