944,054 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5738
  • C++ RSS
Nov 7th, 2005
0

Default values for STL container function arguments?

Expand Post »
Quick question:

Normally when I have a function, I can make one or more of the 'last' arguments as defaults, for example:
C++ Syntax (Toggle Plain Text)
  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?
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Nov 7th, 2005
0

Re: Default values for STL container function arguments?

Why not use a constant pointer to the vector? If the vector is not zero, then make a copy of it inside the function.

C++ Syntax (Toggle Plain Text)
  1. string whateverTwo( int a, const vector<string> *defaultV = 0) {
  2. vector<string> stringVector;
  3. if (defaultV != 0)
  4. stringVector = *defaultV;
  5. // ...
  6. }
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Nov 7th, 2005
0

Re: Default values for STL container function arguments?

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading/Writing to Executable
Next Thread in C++ Forum Timeline: compile error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC