How to iterate through a vector

I have a vector of a class type. This class has a protected member variable called characterType, which is a string, and a public member function that returns this value. How can I iterate through this vector once it has some contents, and print out the names of its members? Here's what I have currently:

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
27
28
29
30
31
32
33
34
for(int i = 0; i < 4; i++)
    {
    cout << "1. Goblin\n"
         << "2. Barbarian\n"
         << "3. Reptile man\n"
         << "4. Blue man\n\n";
    cin >> characterSelection;

    switch(characterSelection)	{
        case '1':
            v1.push_back(new Goblin);
            break;
        case '2':
            v1.push_back(new Barbarian);
            break;
        case '3':
            v1.push_back(new Reptile);
            break;
        case '4':
            v1.push_back(new BlueMan);
            break;
        default:
            v1.push_back(new Goblin);
            cout << "\n\nInvalid entry--combatant defaulted to Goblin.\n\n";
            continuePlay();
            break;
    }

    cout << "Container contents: ";

    vector<Character*>::iterator p1;
    for(p1 = v1.begin(); p1 != v1.end(); p1++)
        cout << v1->characterType << endl;
}


But my compiler does not like what I did on line 33: "Base operand of '->' has non-pointer type 'std::vector<Character*>". I've tried just using the dot operator, which also doesn't compile. What is the best way to accomplish want I'm trying to do?
closed account (jvqpDjzh)
As I understood an iterator is similar to a pointer, so to access to each class pointer you have first to dereference the pointer and then, since you have actually pointers in your vector, you have to use the -> operator to access to the class members:
(*p1)->characterType;
But I am not sure, 'cause I don't know much things about iterators
closed account (jvqpDjzh)
Well, for me this is working! (I am not completely sure about the efficiency and correctness of this program. Please, if you are an expert confirm or correct.)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Character
{
public:
    std::string type_c = "";//please, don't make your normal member variables public
//I have just made it, to make it fast
    Character(std::string type) : type_c (type) { }
    virtual ~Character(){}
};

class Barbarian : public Character
{
    public:
    Barbarian(std::string type) : Character(type){ }//calling the mother constructor
};

class GoodPerson : public Character
{
    public:
    GoodPerson(std::string type) : Character(type){ }//calling the mother constructor
};

int main()
{
    vector <Character*> cv;

    int i = 1;

    switch(i)
    {
    case 1:
        cv.push_back(new Barbarian("Barbarian"));
        break;
    case 2:
        cv.push_back(new GoodPerson("Good"));
        break;
    default:
        break;
    }

    for(vector<Character*>::iterator it = cv.begin(); it != cv.end(); ++it)
        std::cout << (*it)->type_c;
    for(vector<Character*>::iterator it = cv.begin(); it != cv.end(); ++it)
        delete (*it);//deleting objects in the vector
//actually, I don't know if we have to use delete or just pop_back the elements

    return 0;
}
Last edited on
Wow, thank you for putting so much work into answering this! I would have included more of my code to give a functioning example if I had known you'd go to such lengths. You were absolutely right, I had to dereference the pointer with parentheses. Thanks for making this forum so great :)
Registered users can post here. Sign in or register to post.