using pointers with vector iterators

I followed a tutorial online on how to use an iterator with a vector and it worked perfectly, but I wanted to try something myself, which is iterate through a pointer of type vector, I cant seem to figure it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> strVect;

    vector<string>* pVect = &strVect;

    strVect.push_back("One");
    strVect.push_back("Two");
    strVect.push_back("Three");

    cout << (*pVect)[0] << endl;

    for(vector<string>::iterator it = pVect.begin(); it != pVect.end(); it++)
    {
        cout << *it << endl;
    }


    return 0;
}
Line 19:
1
2
// for(vector<string>::iterator it = pVect.begin(); it != pVect.end(); it++)
    for(vector<string>::iterator it = pVect->begin(); it != pVect->end(); it++)
I figured it out, the compiler suggested using the member selection arrow and that worked. However, I am wondering why I use the member selection arrow and not the dot operator? I BELIEVE that the Arrow operator can only be used with pointers and the dot operator, well idk. (it's been a while since i learned about those.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> strVect;

    vector<string>* pVect = &strVect;

    strVect.push_back("One");
    strVect.push_back("Two");
    strVect.push_back("Three");

    cout << (*pVect)[0] << endl;

    for(vector<string>::iterator it = pVect->begin(); it != pVect->end(); it++)
    {
        cout << *it << endl;
    }


    return 0;
}
> I BELIEVE that the Arrow operator can only be used with pointers

pVect is a pointer

1
2
3
4
5
6
7
8
9
10
11
// pVect is a pointer to an object of type vector; begin and end are members of vector
for(vector<string>::iterator it = pVect->begin(); it != pVect->end(); it++)
{
    cout << *it << endl;
}

// (*pVect) is an object of type vector; begin and end are members of vector
for(vector<string>::iterator it = (*pVect).begin(); it != (*pVect).end(); it++)
{
    cout << *it << endl;
}

http://coliru.stacked-crooked.com/a/a5c3dddf1aeab160
Topic archived. No new replies allowed.