473,886 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass map as default parameter to a function

44 New Member
hi,
Can any buddy help me to pass map as default orgunment to a function.
I have a map as below,

std::map<std::s tring,std::stri ng> 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 6772
Motoma
3,237 Recognized Expert Specialist
hi,
Can any buddy help me to pass map as default orgunment to a function.
I have a map as below,

std::map<std::s tring,std::stri ng> 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 Recognized Expert Top Contributor
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
2855
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 reference but set no default value, then the parser has no problems. I've resovled this for now by taking out the "false" default values. But how shall I handle those situations (there are many) where I wish to pass an object by reference but I'm not...
5
2564
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, &$intInsertId = NULL) { I am getting this error: Parse error: parse error, expecting `')'' in c:\program
110
10008
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 must be an object instead of
2
2446
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 parameter): SELECT * FROM MyTableFunction(1)
9
11383
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 example, let's say the map key is char* strings, and I want the comparison based on the nth character in the string. When the map is instantiated, I need to somehow specify n. So I might have something like this: class MyClass
6
2722
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 pass by reference in C since you are just passing an address (or a pointer value address I guess?). So I was wondering is this correct?
7
6928
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 static member. Any idea ?
12
11129
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
1110
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
9956
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10770
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10872
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9592
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7139
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5808
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4627
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.