Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to pick one object out of all objects with a specific set of attributes?

Posted on 2008-10-23
9
195 Views
Last Modified: 2012-05-05
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!

Dan
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
		findThisGroup.SetCTorCountyID(CTorCountyID);
		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()) 
			{
				++count;
				/*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);
	//v.GetPeerGroupID();
	return 0;
}

Open in new window

0
Comment
Question by:ltdanp22
  • 6
  • 3
9 Comments
 

Author Comment

by:ltdanp22
ID: 22783984
Second paragraph edited:
My thoughts on how to do this: push each matching object onto a second vector using the value of "result". Then pick one of those objects at random using existing function DiscreteUniformVariate(0, vector.siz()) and get the PeerGroup attribute out of the object by calling GetPeerGroupID(). I just don't know how to do this.
Also, please ignore the second to last line "v.GetPeerGroupID();" as v is not a peergroup object.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22784057
Hi ,

you could do it somehoe like this:

> std::vector<CPeerGroup> v_result;
> 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())
>  {
>   ++count;
>   /*cout  << "\t" << ++count << ": " 
>   << (*result).GetPeerGroupID() << ", " 
>   << (*result).GetCTorCountyID()    << ", " 
>   << (*result).GetAgeGroup()    << endl;*/
>// add the found item to 'v_result'
>    v_result.push_back( *result );
>// I put this line here since there's no need to do another 'if'
>    result++; // If found one, prepare to look for the next
>   }
>  }
>  ...
> // now get one random item
> int n = rand() % v_result.size();
> CPeerGroup randomItem = v_result[ n ];


Hope that helps,

ZOPPO
0
 

Author Comment

by:ltdanp22
ID: 22790374
Zoppo,
Looks perfect. However, when I run the code I get this error message:
error C2065: 'v_PeerGroup : undeclared identifier
The revised function with your changes incorporated is in the attached snippet.
The v_PeerGroup vector is declared in the function. Should it be declared somewhere else? The header file or the main function?
Daniel
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ltdanp22
ID: 22790379
Whoops. Forgot the snippet...
int SelectPeerGroup(int AgeGroup, int CTorCountyID)//DP2
{
	bool foundMatch = false;
 
	for(int numberOfTries = 1; !foundMatch; numberOfTries++)
	{
		CPeerGroup findThisGroup(-1);
		findThisGroup.SetAgeGroup(AgeGroup);
		findThisGroup.SetCTorCountyID(CTorCountyID);
		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(); // Assign to a reference type and use.
		vector<CPeerGroup>::iterator result = v.begin();
		vector<CPeerGroup> v_PeerGroup;
		int count = 0; // count matches found
		while (result != v.end())
		{
			result = std::find(result, v.end(), findThisGroup);
			if (result != v.end()) 
			{
				++count;
				cout	<< "\t" << ++count << ": " 
						<< (*result).GetPeerGroupID() << ", " 
						<< (*result).GetCTorCountyID()    << ", " 
						<< (*result).GetAgeGroup()    << endl;
			v_PeerGroup.push_back(*result);
			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, v_PeerGroup.size());
	CPeerGroup SelectedPeerGroup = v_PeerGroup[Index];
	int PeerGroupID = SelectedPeerGroup.GetPeerGroupID();	//v.GetPeerGroupID();
	return PeerGroupID;
}

Open in new window

0
 

Author Comment

by:ltdanp22
ID: 22791352
One more question: is there any difference between vector.push() and vector.push_back()?
0
 

Author Comment

by:ltdanp22
ID: 22791415
Scratch that last question. I just answered it for myself.
 
 
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 22794488
Hi ltdanp22,

the problem with 'v_PeerGroup' is that you use it outside the scope where it's declared.

Just move this line
> vector<CPeerGroup> v_PeerGroup;
before this line
> for(int numberOfTries = 1; !foundMatch; numberOfTries++)

Zoppo
0
 

Author Comment

by:ltdanp22
ID: 22805457
Hi Zoppo,
Thanks! Seems to have worked. Can you explain to me why this is necessary so I know what not to do in the future?
Is it because v_PeerGroup is deleted for every iteration of the for loop?
Dan
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22810911
Hi,

this was needed since every variable instantiated on the stack is only available within it's execution scope. Such a scope is simply the code between '{' and '}'. So, anything you declare within a '{', '}' block can't be accessed outside of this block.

ZOPPO
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 150
Using Diagnostic tools in VS2015: Unresoved allocations 19 102
Least Squares Curve Fitting 4 96
C++ :Change value from  DisableCMD registry 4 65
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question