Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is it valid to return an stl data type from a function?

Status
Not open for further replies.

jennyy

Programmer
Jan 9, 2003
8
US
Is it possible to use an stl data type as the return type of a function? (see code below for an example). Also, can stl data types and iterators be passed by reference?

set<Action> Init::AllPossibleActions()
{
class Action f(0);
class Action t(1);

set<Action> all;
pair<set<Action>::iterator, bool> inserted;

inserted = all.insert(f);

if (!inserted.second)//insertion was unsuccessful
cerr << &quot;AllPossibleActions insertion into set unsuccessful\n&quot;;

inserted = all.insert(t);

if (!inserted.second)//insertion was unsuccessful
cerr << &quot;AllPossibleActions insertion into set unsuccessful\n&quot;;

#ifdef DEBUG
set<Action>::iterator pos;
cout << &quot;All possible Actions\n&quot;;
for(pos = all.begin(); pos != all.end(); ++pos)
{
cout << *pos << &quot; &quot;;
}
cout << &quot;\n&quot;;
#endif

return all;
}
 
Yes.

Half-ass code, but check it:

//demonstration instantiations, assignments, and printout
vector<int> y, *pY = &y;
*pY = retVec();
cout << y.at(0) << ' ' << pY->at(1) << endl;

//function example
vector<int> retVec()
{
vector<int> v;
v.push_back( 3 );
v.push_back( 5 );
return v;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top