为什么这段代码给我一个错误?
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string k;
vector<string> f;
int main()
{
string k;
while (cin >> k) {
char l = '3';
cin >> k;
for (auto &v : k) {
if (v == 'e')
v = l;
if (v == 'i')
v = '1';
if ( v == 'z')
v = '2';
}
f.push_back(k);
}
auto r = f.begin();
auto s = f.end();
while (r != s) {
string j = f[r];
cout << j;
++r;
}
return 0;
}
我尝试多次编译和编辑它,但我总是得到这个错误。 所以我尝试对迭代器使用不同的类型,但是这不能做得更好。 有没有人有解决这个问题的建议? 这将是一个 leet 文本生成器
最佳答案
您正在尝试混合使用两种不同的方式来访问 std::vector
中的元素,使用迭代器和使用下标运算符。
如果你想使用下标运算符,你必须使用 vector 中的有效索引来调用它,例如:
std::string s = v[1]; // get the second element of the vector
如果你想使用迭代器,你会这样做:
auto it = v.begin();
std::string s = *it; // get the element referred by the iterator it
关于c++ - 运算符 [] 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35710515/