473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to pass map as default parameter to a function

44
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
Jan 19 '07 #1
2 6648
Motoma
3,237 Expert 2GB
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.
Jan 19 '07 #2
horace1
1,510 Expert 1GB
no problem so long as you have a map to use as a default, e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5. map<const char*, int> defaultClass;    // default map
  6.  
  7. // print contents of map (or the default)
  8. void printMap(map<const char*, int>  s = defaultClass)
  9.  cout << "george is " << s["george"] << " years old" << endl;
  10.  map<const char*, int>::iterator it;
  11.   for( map<const char*, int>::iterator iter = s.begin(); iter != s.end(); iter++ ) {
  12.     cout << (*iter).first << " is " << (*iter).second << " years old" << endl;
  13. }
  14. }
  15.  
  16. int main()
  17. {
  18.   // setup default class names and ages  and print it
  19.   defaultClass["henry"] = 5;
  20.   defaultClass["george"] = 6;
  21.   defaultClass["susan"] = 8;
  22.   defaultClass["jenny"] = 12;
  23.   cout << "default class " << endl;
  24.   printMap();
  25.   // setup class names and ages and print it
  26.   map<const char*, int> class1;
  27.   class1["sam"] = 3;
  28.   class1["george"] = 4;
  29.   class1["mary"] = 89;
  30.   class1["sally"] = 11;
  31.   cout << "\nclass " << endl;
  32.   printMap(class1);
  33.   cin.get();
  34. }
  35.  
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
Jan 19 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
5
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,...
110
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...
2
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...
9
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...
6
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...
7
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...
12
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....
0
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.
0
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...
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.