C ++ member function called begin () question

Assuming that:

vector<string> mvec; 

has some elements on it

Partial Code:

for(vector<string>::iterator it1 = mvec.begin(); it1 != mvec.end(); ++it1) {  
for(string::iterator it2 = it1->begin(); it2 != it1->end(); ++it2)

So:

it1-> begin () to separate the object, and then call the member function begin () of this object, which object is it1 pointing to?

Line

+3
source share
5 answers

Since it it1is iteratorfor vector<string>, when you eliminate it, you will receive string.

+5
source

it1 points to an instance of a string class stored in a vector.

+5
source

:

#include <vector>
#include <string>
#include <iostream>
using namespace std;

int main() {
    vector <string> mvec;
    mvec.push_back("foo");
    mvec.push_back("bar");
    for(vector<string>::iterator it1 = mvec.begin(); it1 != mvec.end(); ++it1) {  
        for(string::iterator it2 = it1->begin(); it2 != it1->end(); ++it2) {
            cout << * it2 << endl;
        }
    }
}

f
o
o
b
a
r

, - .

+2

, string, , it2 .

+1

Like others, dereferencing it1will give you string. This may seem strange, given that it1it is not a pointer, but the class is iteratoroverloaded operator->, in which case all bets are disabled in relation to what happens.

+1
source

All Articles