No match for operator []

why is this code giving me an error?

#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;
}

I tried to compile it and edit it many times, but I always get this error. So I tried to use different types for iterators but that could not be done eighter. Does anyone have a suggestion to solve this? It would be a leet text generator

Upvotes: 0

Views: 58

Answers (2)

Ionut
Ionut

Reputation: 6866

You're trying to mix two different ways to access an element in an std::vector, using an iterator and using the subscript operator.

If you want to use the subscript operator, you have to call it with a valid index into the vector, e.g.:

std::string s = v[1]; // get the second element of the vector

If you want to use iterators, you would do it like this instead:

auto it = v.begin();
std::string s = *it; // get the element referred by the iterator it

Upvotes: 2

Barmar
Barmar

Reputation: 780984

Since r is an iterator, you use it like a pointer, not like an array index. So f[r] should be *r.

string j = *r;

Upvotes: 1

Related Questions