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

C++ c++;11使用lambda排序向量跟踪索引,c++,c++11,vector,lambda,stl,C++,C++11,Vector,Lambda,Stl,我尝试使用c++11应用此解决方案(我使用的是gcc-4.8.2) //排序算法示例 #include//std::cout #include//std::sort #include//std::vector 使用名称空间std; 向量排序索引(const vector&v){ 向量idx(v.size()); 对于(size_t i=0;i!=idx.size();++i)idx[i]=i; 排序(idx.begin(),idx.end(), [&v](size_t i1,size_t i2)

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

//排序算法示例
#include//std::cout
#include//std::sort
#include//std::vector
使用名称空间std;
向量排序索引(const vector&v){
向量idx(v.size());
对于(size_t i=0;i!=idx.size();++i)idx[i]=i;
排序(idx.begin(),idx.end(),
[&v](size_t i1,size_t i2){返回v[i1]std::cout我认为如果在编译选项中没有显式请求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
启用了C++11支持?顺便说一句,第一个循环已在
std::iota
中预先生成。编译器生成符号
\uu lambda0
似乎意味着启用了C++11支持。@DrewDormann不,不是。我可以使用
-std=gnu>复制此错误++03
-std=c++03
非常感谢Mike Seymour和stj,我确实需要添加标志-std=c++11非常感谢Mike Seymour和stj,我确实需要添加标志-std=c++11
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’
$ 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)’
g++ -Wall -std=c++11 test.cpp