繁体   English   中英

c ++ 11使用lambda排序向量跟踪索引

[英]c++11 using lambda sort vector keeping track of indices

我尝试使用c ++ 11应用此解决方案(我使用的是gcc-4.8.2)

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

using namespace std;

vector<size_t> sort_indexes(const vector<float> &v) {

  vector<size_t> idx(v.size());
  for (size_t i = 0; i != idx.size(); ++i) idx[i] = i;

  sort(idx.begin(), idx.end(),
       [&v](size_t i1, size_t i2) {return v[i1] < v[i2];});

  return idx;
}

int main () {

  std::vector<float> w(4, 0.2f);
  w.push_back(0.3f);

  std::vector<size_t> idx = sort_indexes(w);

  // print out content:
  std::cout << "ordering:";
  for (std::vector<size_t>::iterator it=idx.begin(); it!=idx.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

但是我收到一个编译错误,说:

error: no matching function for call to ‘sort(std::vector<long unsigned int>::iterator, std::vector<long unsigned int>::iterator, sort_indexes(const std::vector<float>&)::__lambda0)’
    [&v](size_t i1, size_t i2) {return v[i1] < v[i2];});

error: template argument for ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)’ uses local type ‘sort_indexes(const std::vector<float>&)::__lambda0’

我将不胜感激任何帮助 :-)

我认为如果您在编译选项中未明确请求C ++ 11支持,则会发生该错误。

使用以下命令编译测试程序时,出现了一些错误,包括您报告的错误。 我得到的错误略有不同,因为我有g ++ 4.9.1而不是4.8.2:

$ g++ -Wall test.cpp

test.cpp: In function ‘std::vector<long unsigned int> sort_indexes(const std::vector<float>&)’:
test.cpp:14:57: warning: lambda expressions only available with -std=c++11 or -std=gnu++11
        [&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
                                                         ^
test.cpp:14:58: error: no matching function for call to ‘sort(std::vector<long unsigned int>::iterator, std::vector<long unsigned int>::iterator, sort_indexes(const std::vector<float>&)::<lambda(size_t, size_t)>)’
        [&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
                                                          ^
test.cpp:14:58: note: candidates are:
In file included from /usr/include/c++/4.9/algorithm:62:0,
                 from test.cpp:3:
/usr/include/c++/4.9/bits/stl_algo.h:4676:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter)
     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
     ^
/usr/include/c++/4.9/bits/stl_algo.h:4676:5: note:   template argument deduction/substitution failed:
test.cpp:14:58: note:   candidate expects 2 arguments, 3 provided
        [&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
                                                          ^
In file included from /usr/include/c++/4.9/algorithm:62:0,
                 from test.cpp:3:
/usr/include/c++/4.9/bits/stl_algo.h:4705:5: note: template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)
     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
     ^
/usr/include/c++/4.9/bits/stl_algo.h:4705:5: note:   template argument deduction/substitution failed:
test.cpp: In substitution of ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int> >; _Compare = sort_indexes(const std::vector<float>&)::<lambda(size_t, size_t)>]’:
test.cpp:14:58:   required from here
test.cpp:14:58: error: template argument for ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)’ uses local type ‘sort_indexes(const std::vector<float>&)::<lambda(size_t, size_t)>’
        [&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
                                                          ^
test.cpp:14:58: error:   trying to instantiate ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)’

通过将-std=c++11添加到编译器调用命令中,您应该能够摆脱这些错误:

g++ -Wall -std=c++11 test.cpp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM