我有两个字符串向量:
std::vector<std::string> savestring{"1", "3", "2", "4"}; // some numbers
std::vector<std::string> save2{"a", "b", "c", "d"}; // some names
我希望根据前者对后者进行重新排序,以便它最终成为{"a", "c", "b", "d"}
。我试过这个:
for (int i=0; i<savestring.size(); i++)
{
savestring[i] = save2[savestring[i]];
}
但我收到错误:
“binary'[':找不到运算符,它采用'std :: basic_string&lt; _Elem,_Traits,_Alloc&gt;'类型的右手操作数(或者没有可接受的转换)“
这意味着什么,我的代码有什么问题?
答案 0 :(得分:2)
问题是...
gulp.task "sass", ->
sourcePath = "styles/template/**/*.sass"
sass(sourcePath,{sourcemap:true})
.pipe(plumber())
.on('error',sass.logError)
.pipe(prefixer())
.pipe(minfiyCss())
.pipe(gulp.dest(path.join targetPath,"assets","css"))
...
gulp.task "angular", ->
sourcePath = [
"angular/config/*.coffee"
"angular/services/**/*.coffee"
"angular/directives/**/*.coffee"
"angular/controllers/**/*.coffee"
]
gulp.src(sourcePath)
.pipe(plumber())
.pipe(coffee {bare:true})
.pipe(ngAnnotate {single_quotes:true})
.pipe(sourcemap.init {loadMaps: true})
.pipe(concat 'angular.min.js')
.pipe(uglify())
.pipe(sourcemap.write())
.pipe(gulp.dest path.join targetPath,"assets","js")
...
是savestring[i]
,而std::string
中的方括号内应该有一个整数。因此,解决方案是首先通过编写自定义函数将save2[]
转换为整数。
所以,将其更改为:
std::string
不要忘记在顶部写// Converts a std::string to an int
int ToInt( const std::string& obj )
{
std::stringstream ss;
ss << obj;
int ret;
ss >> ret;
return ret;
}
for(int i=0;i<savestring.size();i++)
{
savestring[i]=save2[ToInt(savestring[i])];
}
来添加sstream
标题。
答案 1 :(得分:0)
您正在保存数字,表示为字符串。在用作数组索引之前,需要将它们转换为数字。