Ritwick Malhotra
Ritwick Malhotra

Reputation: 97

Iterating through data members of an object in a vector

I'm trying to achieve a sort of relation between 2 classes with an id attribute. teamId in SoccerTeams class must be same as the directorId of a director in managing_directors class so as to assign a managing director to a soccer team.

Both classes have a vector teams and man_dirs respectively.

class SoccerTeams {
    string teamName;
    int teamId;
    friend class managing_directors;
 public:

    //teams vector
    vector<SoccerTeams> teams;

    void addTeam(string name, int id) {
        SoccerTeams newTeam(name, id);
        teams.push_back(newTeam);
    };

    SoccerTeams(){};
    SoccerTeams(string teamName, int id){
        teamName = teamName;
        teamId = id;
    };
    ~SoccerTeams(){};
};


//managing directors of a soccer team
class managing_directors : public SoccerTeams, public people {
    int directorId;
 public:
    //man_dirs vector
    vector<managing_directors> man_dirs;

    managing_directors(int num_of_managers, int avg_age);
    managing_directors(){};
    ~managing_directors(){};
    void addDirector(string name, char sex, int age, int id) {
        managing_directors newDirector;
        newDirector.name = name;
        newDirector.sex = sex;
        newDirector.age = age;
        newDirector.directorId = id;                        
        man_dirs.push_back(newDirector);
    }; 

I tried making a function to check team id in managing_directors class as follows.

int idChecker(int id) {
    for (vector<SoccerTeams>::const_iterator i = teams.begin(); i != teams.end(); ++i) {
        if(i -> teamId == id)
            return 1;
    }
    return 0;
}

But the function always returned 0, which I believe is because it is not checking the same instance of teams vector created in the main function, since when I tried printing this teams vector inside managing_directors class, it was empty. Is it possible to make the teams vector global? Is this the correct approach to solve this problem?

Definition of people class inherited in managing_directors(just in case)

class people {
    string name;
    char sex;
    int age;
    bool under_auth;
    int auth_level;
    friend class managing_directors;
 public:
    people(){};
    ~people(){};    
    virtual int get_age(){ return this->age; }; 
};

UPDATE:

I fixed the loop as given in one of the answers, but I don't think that was the issue, since the function still returns 0. I tried using cout to try and print all the ids and I got nothing in the output, it was empty.

I did something like this

int idChecker(int id) {
    for (vector<SoccerTeams>::const_iterator i = teams.begin(); i != teams.end(); ++i) {
        cout << i - > teamId << endl; //test code
        if(i -> teamId == id)
            return 1;
    }
    return 0;
}

Upvotes: 1

Views: 54

Answers (1)

NathanOliver
NathanOliver

Reputation: 180595

Your loop is wrong. In

for (vector<SoccerTeams>::const_iterator i = teams.begin(); i != teams.end(); ++i) {
    if(i -> teamId == id)
        return 1;
    return 0;
}

If the first i does not match then you immediately return 0. What you need to do is move that return 0; out of the for loop so you only return 0 when the loop does not find a match. That gives you

int idChecker(int id) {
    for (vector<SoccerTeams>::const_iterator i = teams.begin(); i != teams.end(); ++i) {
        if(i -> teamId == id)
            return 1;
    }
    return 0;
}

You can also simplify this using ranged based for loop like

int idChecker(int id) {
    for (const auto& e : teams)
        if(e.teamId == id)
            return 1;
    return 0;
}

You could also use std::any_of and a lambda like

int idChecker(int id) {
    return std::any_of(teams.begin(), teams.end()
        [=](const auto& e) { return e.teamId == id; });
}

Upvotes: 4

Related Questions