请考虑以下代码段:
#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) 当我运行时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?完全重建非常耗时。
我知道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++ ×2
c++11 ×1
lambda ×1
node-gyp ×1
node.js ×1
rebuild ×1
std-function ×1
string ×1
stringstream ×1
yarnpkg ×1