| | |
Default values for STL container function arguments?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Quick question:
Normally when I have a function, I can make one or more of the 'last' arguments as defaults, for example:
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?
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?
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)
string whatever( int a, string b="NONE" ) { //blah, blah, blah string temp= "WHATEVER"; return temp; }
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)
string whateverTwo( int a, vector<string> defaultV = ????) { //whatever string temp= "WHATEVER"; return temp; }
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
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)
string whateverTwo( int a, const vector<string> *defaultV = 0) { vector<string> stringVector; if (defaultV != 0) stringVector = *defaultV; // ... }
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
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.
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.
![]() |
Similar Threads
- Send data on a serial port (C++)
- help with classes?! (C++)
- Function Arguments (Java)
- Help splitting up code into .h and .cpp (C++)
- This Should be Easy for You Guys! (Linux Servers and Apache)
- Pointer to function as an argument (C++)
- Need Help Revising Array program: Visual C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: Reading/Writing to Executable
- Next Thread: compile error
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






