【发布时间】:2018-06-14 21:23:23
【问题描述】:
我假设可以将operator[] 用于任何vector,而不管它包含的数据类型如何。我编写了这个算法来从字符串中删除空格,其中每个字符串都使用operator[]从字符串向量中索引。
std::vector<string> substrs;
//after reading strings into the vector
//by means of user input
for(auto i : substrs){
string s = substrs[i];
s.erase(remove(s.begin(), s.end(), ' '), s.end());
}
由于以下错误,上述代码段无法编译:
错误:对于“向量”类型(又名 '向量,分配器> >') 字符串 s = substrs[i];
谁能解释一下?
【问题讨论】:
-
您正在使用基于范围的 for 循环。您在 i 中拥有的是向量中的字符串。
标签: c++ string vector operator-keyword