【问题标题】:c++11 using lambda sort vector keeping track of indicesc++11使用lambda排序向量跟踪索引
【发布时间】:2015-06-24 16:12:41
【问题描述】:

我尝试使用 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’

我会很感激任何帮助:-)

【问题讨论】:

  • Works for me。这是唯一的错误信息吗?您是否使用 -std=c++11 启用了 C++11 支持?
  • 顺便说一句,第一个循环已经在std::iota 中预制。
  • 编译器生成符号__lambda0似乎暗示启用了C++11支持。
  • @DrewDormann 不,不是。我可以使用-std=gnu++03-std=c++03 重现此错误。
  • 非常感谢 Mike Seymour 和 stj,确实我需要添加标志 -std=c++11

标签: c++ c++11 vector lambda stl


【解决方案1】:

我认为如果您没有在编译选项中明确请求 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

【讨论】:

  • 非常感谢 Mike Seymour 和 stj,确实我需要添加标志 -std=c++11
猜你喜欢
  • 2010-12-07
  • 2012-05-21
  • 2013-04-27
  • 1970-01-01
  • 2014-04-12
相关资源
最近更新 更多