Monday, 15 September 2014

c++ - keep indexes before sorting of array c -



c++ - keep indexes before sorting of array c -

hi utilize code store indexes before sorting, when compile in row : :

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

, says me error:

multiple markers @ 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){ homecoming values[a] < values[b]; } ); homecoming indices; }

and sec question how can phone call , when have classic input 1d array

this [...](...) { ... } syntax lambda expression, feature of c++11. need create sure you're using compiler supports them. compilers provide switch enable c++ features (-std=c++0x gcc).

if want utilize std::sort on normal array, use:

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

alternatively, can create code agnostic whether you're using container or array. ac array or container:

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

c++ sorting search

No comments:

Post a Comment