小编Bud*_*son的帖子

如何为std :: fill()使用C++ 0x lambdas局部变量?

所以我试图测试lambda访问它所使用的范围内的局部变量,大致基于Bjarne在C++ 0x FAQS页面上的一个简单例子:http: //www2.research.att.com/ 〜BS/C++ 0xFAQ.html#拉姆达

当我尝试这个简单的测试代码时:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//Test std::fill() with C++0x lambda and local var
void f (int v) { 
    vector<int> indices(v);
    int count = 0;
    fill(indices.begin(), indices.end(), [&count]() {
        return ++count;
    });

    //output test indices
    for (auto x : indices) {
        cout << x << endl;
    }
}

int main() {
    f(50);
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

required from 'void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >, _Tp …
Run Code Online (Sandbox Code Playgroud)

c++ lambda fill iota c++11

4
推荐指数
1
解决办法
1321
查看次数

标签 统计

c++ ×1

c++11 ×1

fill ×1

iota ×1

lambda ×1