Default values for STL container function arguments?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Default values for STL container function arguments?

 
0
  #1
Nov 7th, 2005
Quick question:

Normally when I have a function, I can make one or more of the 'last' arguments as defaults, for example:
  1. string whatever( int a, string b="NONE" )
  2. {
  3. //blah, blah, blah
  4. string temp= "WHATEVER";
  5. return temp;
  6. }

The benefit is that I can have one function and I can do logic in there based on whether my parameter has the default or has something else.

I would like to do the same thing, but with a parameter that is a container. How do I do it?
  1. string whateverTwo( int a, vector<string> defaultV = ????)
  2. {
  3.  
  4. //whatever
  5. string temp= "WHATEVER";
  6. return temp;
  7. }
I want this so that I can either pass in the vector or not. I know that I of course can make 2 different functions, but wanted to see if I could do a default instead. Any thoughts?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Default values for STL container function arguments?

 
0
  #2
Nov 7th, 2005
Why not use a constant pointer to the vector? If the vector is not zero, then make a copy of it inside the function.

  1. string whateverTwo( int a, const vector<string> *defaultV = 0) {
  2. vector<string> stringVector;
  3. if (defaultV != 0)
  4. stringVector = *defaultV;
  5. // ...
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Default values for STL container function arguments?

 
0
  #3
Nov 7th, 2005
STL vector objects have their own default capacity, which is implementation dependent. You should be able to determine this value rather simply. Something like this should do.

vector<int> v;
cout << "default capacity of vectors in my STL implementation is " << v.capacity <<

Of course you can specify a capacity, if you desire.

vector<int> v(5);
cout << "the capacity of v is " << v.capacity();
cout << "the size of v is ";
if(v.empty())
cout << "zero" << endl;
else
cout << v.size();

You can also specify default values.

vector<int> v(5,0);
for(int i = 0; i < 5; ++i)
cout << "v[" << i << "] = " << v[i] << ' ' ;

Data structures can be very big, meaning passing them back and forth by value can be costly. It's often wiser to pass them by reference, making them const references if you don't want the function to change any of the values in the structure being passed. So I'd declare the structure using my own defualt capacity and default all values to the desired default value and then pass that structure to a function by reference in it's current state.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC