I just deleted an earlier post with a similar question because my example was not very clear, So I am trying again.
I have created a simple class called SportsSchedules.cpp The class stores 4 items; sportType, TeamName, city and number of wins. I have created a “sports vector” of SportsSchedules objects. I want to run the sportsVector through a loop and for each sport type I want to create a new vector. Each created sub vector should contain only the unique sportType.
Ideally, this sportsVector would run in a loop and would pop each object into its repsective subVector until it was empty(I guess)
Here is the code from my main:
#include <iostream>
#include "SportsSchedules.h"
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <functional>
bool sportType( std::string type);
int main (int argc, const char * argv[])
{
SportsSchedules *theSport;
theSport = new SportsSchedules("Football", "Chicago", "Bears", 7);
std::vector<SportsSchedules*> *sportsVector = new std::vector<SportsSchedules*>();
sportsVector->push_back(theSport);
theSport = NULL;
theSport = new SportsSchedules("Football", "Chicago", "Bears", 7);
sportsVector->push_back(theSport);
theSport = NULL;
theSport = new SportsSchedules("Baseball", "Boston", "RedSox", 62);
sportsVector->push_back(theSport);
theSport = NULL;
theSport = new SportsSchedules("Football", "GreenBay", "Packers", 15);
sportsVector->push_back(theSport);
theSport = NULL;
theSport = new SportsSchedules("Basketball", "Portland", "Trailblazers", 60);
sportsVector->push_back(theSport);
theSport = NULL;
theSport = new SportsSchedules("Football", "Seattle", "Seahawks", 7);
sportsVector->push_back(theSport);
theSport = NULL;
theSport = new SportsSchedules("Baseball", "Oakland", "A's", 67);
sportsVector->push_back(theSport);
std::cout<<"Test the SportsSchedules Vector "<<std::endl;
std::vector<SportsSchedules*>::iterator itr;
for(itr = sportsVector->begin(); itr != sportsVector->end(); ++itr ){
std::cout<<(*itr)->getSportType()<<" "<<(*itr)->getCity()<<" "<<(*itr)->getTeamName()<<" "
<<(*itr)->getNumWins()<<std::endl;
}
return 0;
}
bool trackType( std::string type){
SportsSchedules * sptPtr;
if(sptPtr->getSportType()== type)
return true;
else
return false;
}
The bool function was from an earlier attempt to try remove_copy_if. I kept getting a compiler error about no int pointer or function pointer. Not sure if that what I need as it creates a blue print of all the vector indexes. I want something that would push – pop if possible Someone also suggested using map or multi map but I didn’t quite understand it
Thanks
Someone suggested you a map because they are associative containers. Ie. instead of looking for a certain value using a positional index (0, 1, 2, …) you look up using an arbitrary value, which can be a string, for example.
So, how can it be useful for you? See this example:
uniques[(*itr)->getSportType()]retrieves fromuniquesastd::vector<SportsSchedules*>indexed by the value of(*itr)->getSportType(). If the key doesn’t exist in the map (first time you see the sport in thesportsVector), it will create a new one before doing it – otherwise, you get the previously created vector.To retrieve the info once it’s there, you can either look it up by name:
or iterating over it to get the (key, value) pairs. Have a look to map’s API for more info.