3 Replies - 5131 Views - Last Post: 21 June 2009 - 04:47 PM Rate Topic: -----

#1 tshauck  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 70
  • Joined: 04-January 09

Passing an iterator into a function

Posted 20 June 2009 - 11:19 PM

I'm trying to to utilize more functions in this program so it will be easier to reuse parts later on. Basically what I'm trying to do is change my show_all function
void show_all(vector<stock> stocks)
{
	vector<stock>::iterator AccessStocks;

	for(AccessStocks = stocks.begin(); AccessStocks != stocks.end(); AccessStocks++)
	{
		cout<<endl<<"Ticker: "<<AccessStocks->GetTicker()<<endl;
		cout<<"Date: "<<AccessStocks->GetDate()<<endl;
		cout<<"Open: "<<AccessStocks->GetOpen()<<endl;
		cout<<"High: "<<AccessStocks->GetHigh()<<endl;
		cout<<"Low: "<<AccessStocks->GetLow()<<endl;
		cout<<"Close: "<<AccessStocks->GetClose()<<endl;
		cout<<"Volume: "<<AccessStocks->GetVolume()<<endl;
	}
}


To a function that will upon a switch statement will allow for searches by different criteria like ticker, date, etc.

Essentially I have the function for search by ticker:
void find(vector<stock> stocks)
{
	vector<stock>::iterator AccessStocks;

	string search;

	cout<<"Enter the stock you'd like to search for";
	cin>>search;

	for(AccessStocks = stocks.begin(); AccessStocks != stocks.end(); AccessStocks++)
	{
		if(AccessStocks->GetTicker() == search)
		{
			cout<<endl<<"Ticker: "<<AccessStocks->GetTicker()<<endl;
			cout<<"Date: "<<AccessStocks->GetDate()<<endl;
			cout<<"Open: "<<AccessStocks->GetOpen()<<endl;
			cout<<"High: "<<AccessStocks->GetHigh()<<endl;
			cout<<"Low: "<<AccessStocks->GetLow()<<endl;
			cout<<"Close: "<<AccessStocks->GetClose()<<endl;
			cout<<"Volume: "<<AccessStocks->GetVolume()<<endl;
		}
	}
}



But I know that all of these will have [code]AccessStocks->...[code] so I want to pass the pointer to AccessStocks into a function however I don't really know how to do this.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing an iterator into a function

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Passing an iterator into a function

Posted 21 June 2009 - 03:06 PM

When I first tried something like this I made a function that took an AccessStock* and displayed all its values. But that completely violates acessbility specification and everything has to be public. Make a public member function of AcessesStock that displays all its contents. :D
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: Passing an iterator into a function

Posted 21 June 2009 - 03:24 PM

Any chance the functions can be part of a class? Then you could have the iterator as a class member and all of your problems are solved.
Was This Post Helpful? 0
  • +
  • -

#4 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Passing an iterator into a function

Posted 21 June 2009 - 04:47 PM

Use the visitor pattern.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1