hi,
Can any buddy help me to pass map as default orgunment to a function.
I have a map as below, std::map<std::string,std::string> myStringMap
and i want to pass this map to a function as default orgunment.
(
as we allways pass an int or long as default orgunment to function like this
void fun( int iRollNumber=10 );
//here default value of iRollNumber is 10
)
So can we do the same in case of map . If yes then how ???
plz help me ,
thank's in advance.
Regards,
Abhinay
2 6648
hi,
Can any buddy help me to pass map as default orgunment to a function.
I have a map as below, std::map<std::string,std::string> myStringMap
and i want to pass this map to a function as default orgunment.
(
as we allways pass an int or long as default orgunment to function like this
void fun( int iRollNumber=10 );
//here default value of iRollNumber is 10
)
So can we do the same in case of map . If yes then how ???
plz help me ,
thank's in advance.
Regards,
Abhinay
I don't think you can use a variable as a default value. What you may be able to do instead is overload the function, where a call without arguments performs a call to the original function specifying the default value.
no problem so long as you have a map to use as a default, e.g. -
#include <iostream>
-
#include <map>
-
using namespace std;
-
-
map<const char*, int> defaultClass; // default map
-
-
// print contents of map (or the default)
-
void printMap(map<const char*, int> s = defaultClass)
-
{
-
cout << "george is " << s["george"] << " years old" << endl;
-
map<const char*, int>::iterator it;
-
for( map<const char*, int>::iterator iter = s.begin(); iter != s.end(); iter++ ) {
-
cout << (*iter).first << " is " << (*iter).second << " years old" << endl;
-
}
-
}
-
-
int main()
-
{
-
// setup default class names and ages and print it
-
defaultClass["henry"] = 5;
-
defaultClass["george"] = 6;
-
defaultClass["susan"] = 8;
-
defaultClass["jenny"] = 12;
-
cout << "default class " << endl;
-
printMap();
-
// setup class names and ages and print it
-
map<const char*, int> class1;
-
class1["sam"] = 3;
-
class1["george"] = 4;
-
class1["mary"] = 89;
-
class1["sally"] = 11;
-
cout << "\nclass " << endl;
-
printMap(class1);
-
cin.get();
-
}
-
this will display
default class
george is 6 years old
george is 6 years old
henry is 5 years old
susan is 8 years old
jenny is 12 years old
class
george is 4 years old
george is 4 years old
sam is 3 years old
mary is 89 years old
sally is 11 years old
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lawrence |
last post by:
The following class method is being rejected by the PHP parser. If I
change the method paramaters and allow the objects to be passed as
copies, then the parser has no problem. Or, if I pass by...
|
by: John T |
last post by:
I am trying to make a function that takes an optional parameter that gets
passed by reference.
Here is the first line of my function definition:
function funQueryDatabase($strQuery,...
|
by: Mr A |
last post by:
Hi!
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.
Exemple:
void func(Objec& object); //object...
|
by: Yarik |
last post by:
Hello there!
I am working with MS SQL Server 2000.
I have a table function that takes an integer parameter and returns a
table, and I can successfully use it like this (passing a literal
as a...
|
by: Martoon |
last post by:
I want to instantiate an STL map with my own compare function, and I
want to pass a parameter to the compare function that will be stored
and used for all comparisons in that map instance.
As an...
|
by: lisp9000 |
last post by:
I've read that C allows two ways to pass information between
functions:
o Pass by Value
o Pass by Reference
I was talking to some C programmers and they told me there is no such
thing as...
|
by: PhilTheGap |
last post by:
Hi,
I've tried this:
<asp:Button ID="Save" runat="server" Text="OK" OnClick="ServerSave"
OnClientClick="SaveParam (<% Util.MaxTags %>)" />
but if fails... Util is a C# class, MaxTags a...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms....
|
by: John O'Hagan |
last post by:
On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano
Thanks, both to you and Bruno for pointing this out, I'll certainly be using
it in future.
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |