Niels
Niels

Reputation: 597

How to point to n'th char from string in vector C++

When I iterate over a vector using an int (say i), I can easily say:

vector<string> V;
V[i][g]

in which (int) g is the g'th character of the string in V[i]

When I am in a loop and want to keep iterating although I will delete items (from V) on the run, I want to make use of:

vector<string>::iterator it;

and then, I thought, the g'th character of V[i] would - in the loop - be:

for (it = V.begin() ; it != V.end() ; it++)
    *it[g]

or, more logically in my eyes:

    it[g]

bit neither work... Can anyone tell me how to get the g'th char of V[i] in this iterator-using variant?

Upvotes: 0

Views: 188

Answers (1)

Tetix
Tetix

Reputation: 331

What you want to do is

for (std::vector<std::string>::iterator it = V.begin(); it!=V.end(); it++) {
        std::cout << *it << std::endl;       // whole string
        std::cout << (*it)[2] << std::endl;  // third letter only
    }

Upvotes: 2

Related Questions