我不确定如何在不创建额外变量的情况下解决此问题。这是无法编译的代码:
std::string & printVec(std::vector<double> const &ds, std::string &dum) {
std::vector<double>::iterator it;
// std::vector<double> dsi = ds; // Created just to get the code to work, using dsi.begin() etc.
dum = " ";
for (it = ds.begin(); it != ds.end(); it++) { // Compiler error. No suitable "="
dum = dum + std::to_string(*it) + " ";
}
return dum;
}
Run Code Online (Sandbox Code Playgroud)
如果我删除输入上的 const,它会编译:
std::string & printVec(std::vector<double> &ds, std::string &dum) {
std::vector<double>::iterator it;
dum = " ";
for (it = ds.begin(); it != …Run Code Online (Sandbox Code Playgroud)