An expert here wrote the function below for me. Right now, I think the code only counts the number of objects that have all the attributes that the reference object "findThisGroup" has. Unfortunately, it also needs to pick one of these objects at random and return an attribute of the chosen object to the calling function. Getting the attribute out of the object can be done using an existing member function called "GetPeerGroupID()".
My thoughts on how to do this: create a vector that pushes each matching object onto a second vector using the value of result. then simply pick one of those objects at random and get the PeerGroup attribute out of the object by calling GetPeerGroupID(). I just don't know how to do this.
I've tried my best to comment each line so that it's easy to follow. If you have any questions, feel free to email me (see my profile). Thanks a bunch!
int SelectPeerGroup(int AgeGroup, int CTorCountyID)//calling fcn passes two attributes the PeerGroupID that is returned must have AgeGroup index and location index.
bool foundMatch = false;
for(int numberOfTries = 1; !foundMatch; numberOfTries++)
CPeerGroup findThisGroup(-1);//initialized PeerGroup object with dummy ID
findThisGroup.SetAgeGroup(AgeGroup);//gives Peergroup object AgeGroup and location attributes
cout << "Looking for... \n"
<< "County: \n" << findThisGroup.GetCTorCountyID() << ", "
<< "Age: \n" << findThisGroup.GetAgeGroup() << endl;
//std::find returns an iterator pointing to the located match
//std::find is in #include <algorithm>
std::vector<CPeerGroup>& v = get_vector(); // calls a function that returns a vector of all existing PeerGroup objects and Assigns to a reference type for use below.
vector<CPeerGroup>::iterator result = v.begin();
int count = 0; // count matches found
while (result != v.end())
result = std::find(result, v.end(), findThisGroup);//i believe the find function returns the index of the matching object
if (result != v.end())
/*cout << "\t" << ++count << ": "
<< (*result).GetPeerGroupID() << ", "
<< (*result).GetCTorCountyID() << ", "
<< (*result).GetAgeGroup() << endl;*/
if (result != v.end())
result++; // If found one, prepare to look for the next
if (count == 0) cout << "Didn't find any!" << endl << endl;
foundMatch = (count > 0);
//int index = DiscreteUniformDeviate(0, count - 1);