C++ Lambda - String params在以下函数调用中不起作用

RW2*_*W23 3 c++ lambda

我有一个问题,当我尝试访问一个lambda中的主要参数的字符串时,编译器在我尝试使用该字符串调用函数时不会识别它.

这是我的代码:

void removePunctuation(std::vector<std::string> &inTokens,
                   std::vector<std::string> &outTokens) {
std::for_each(inTokens.begin(), inTokens.end(), [outTokens](std::string s) {
    std::string newS = s;
    // newS.erase(std::remove_if(newS.begin(), newS.end(), ispunct));
    outTokens.push_back(newS);});
}
Run Code Online (Sandbox Code Playgroud)

并产生以下错误:

a2.cpp:114:19: error: no matching member function for call to 'push_back'
    outTokens.push_back(newS);});
Run Code Online (Sandbox Code Playgroud)

当我尝试调用一个在调用中使用lambda的字符串参数的函数时,我也在其他函数中遇到这种错误.

任何帮助深表感谢!

Ale*_*exD 5

默认情况下,lambda参数以只读方式传递,请尝试

[&outTokens](std::string s)
Run Code Online (Sandbox Code Playgroud)

(无论如何,如果outTokens希望修改参数,也许你想要的.)