小编Jin*_* Li的帖子

如果返回一个向量项,那么lambda表达式的返回类型是什么?

请考虑以下代码段:

#include <iostream>
#include <vector>
#include <functional>

int main() 
{
    std::vector<int>v = {0,1,2,3,4,5,6};
    std::function<const int&(int)> f = [&v](int i) { return v[i];}; 
    std::function<const int&(int)> g = [&v](int i) -> const int& { return v[i];};

    std::cout << f(3) << ' ' << g(3) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期待相同的结果:in f,v由const引用传递,所以v[i]应该有const int&类型.

但是,我得到了结果

 0 3
Run Code Online (Sandbox Code Playgroud)

如果我不使用std :: function,一切都很好:

#include <iostream>
#include <vector>
#include <functional>

int main() 
{
    std::vector<int>v = {0,1,2,3,4,5,6};
    auto f = [&v](int i) …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11 std-function return-type-deduction

27
推荐指数
3
解决办法
3320
查看次数

运行yarn install时如何避免node-gyp重建?

当我运行时yarn install,我的本机插件总是被重建。可以看出,即使没有更新任何内容,也node-gyp rebuild运行命令而不是node-gyp build。

caros@ubuntu:~/Developer/xviz-converter$ yarn install
yarn install v1.16.0
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
$ node-gyp rebuild
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@10.16.0 | linux | x64
gyp info spawn /usr/bin/python2
....
Run Code Online (Sandbox Code Playgroud)

如何让纱线运行node-gyp build而不是node-gyp rebuild?完全重建非常耗时。

rebuild node.js node-gyp yarnpkg node-addon-api

6
推荐指数
1
解决办法
609
查看次数

用stringstream更新后C++ stringstream无法正常工作:str()

我知道stringstream可以更新stringstream::str(),但是如果我在stringstream之后输入其他内容,则无法按预期工作.以下代码段演示了这种现象:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;
int main()
{
    stringstream ss; //ostringstream gives the same output
    ss << "Original string ";
    ss.str("Updated string ");
    ss << "sth else";
    cout << ss.str() << endl;
}
Run Code Online (Sandbox Code Playgroud)

我希望得到输出

Updated string sth else
Run Code Online (Sandbox Code Playgroud)

但它实际上是输出

sth elsestring
Run Code Online (Sandbox Code Playgroud)

似乎相反,将新输入的字符串附加到当前字符串的末尾(在我的情况下Updated string),它会尝试从头开始覆盖它.我的代码出了什么问题?

这是一个现场演示

c++ string stringstream

5
推荐指数
1
解决办法
1147
查看次数