473,756 Members | 6,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HOw to pass std::map as parameter?

I have declared the following

std::map<std::s tring, std::string> myMap;

to pass myMap to functions should I be declaring functions as:

void function(std::m ap<std::string, std::string>);

or is there a preferred/better method of doing this?

Thanks in advance

Sean Hannan
Jul 19 '05 #1
3 30082
Hi Sean!
I have declared the following

std::map<std::s tring, std::string> myMap;

to pass myMap to functions should I be declaring functions as:

void function(std::m ap<std::string, std::string>);

or is there a preferred/better method of doing this?

Yes.
void function(const std::map<std::s tring, std::string>&);
This avoids the copy of the map which could be heavy work.

Ciao, Marco

Jul 19 '05 #2
"Woodster" <mi****@127.0.0 .1> wrote in message
news:MP******** *************** *@news.westnet. com.au...
I have declared the following

std::map<std::s tring, std::string> myMap;

to pass myMap to functions should I be declaring functions as:

void function(std::m ap<std::string, std::string>);

or is there a preferred/better method of doing this?


It depends on what the function does.

Unless you need to copy the whole map when you pass it, a reference to it
would be better:
void function(std::m ap<std::string, std::string> &);

And if you aren't changing the map in the function, use a const reference:
void function(const std::map<std::s tring, std::string> &);

You can also use a typedef if you want:
typedef std::map<std::s tring, std::string> MyMap;

Then use MyMap everywhere you would have used std::map<std::s tring,
std::string>, e.g.,
void function(const MyMap &);

DW

Jul 19 '05 #3

"Woodster" <mi****@127.0.0 .1> wrote in message
news:MP******** *************** *@news.westnet. com.au...
I have declared the following

std::map<std::s tring, std::string> myMap;

to pass myMap to functions should I be declaring functions as:

void function(std::m ap<std::string, std::string>);

or is there a preferred/better method of doing this?


In addition to the "pass by reference" suggestion, I usually make a typedef
for something like that - something that makes it a more meaningful type
name, which also has the advantage of making the function list easier to
read. It does add a slight layer of abstraction though - when you use the
function, you have to go look up that typedef first to see what it really
means.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3419
by: cppaddict | last post by:
Hi, I'm confused about what the comparison operator in a map template is: In particular, I want to know why something like the following doesn't work: bool pointCompare(POINT p1, POINT p2) { if (p1.y > p2.y || (p1.y == p2.y && p1.x > p2.x) ) return true;
5
426
by: EnTn | last post by:
Hi Everyone... I've been trying to use a std::map to do some storage. Basically, i'm storing double values using a Key Object. The Key object is quite simple and is just a pair of int's (conceptually these are 2d coords). My problem is: I have setup a simple test probgram that populates a map with 9 <key,value> pairs. When I try to query the map using
1
3553
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my program uses two MFC classes: CRect and CPoint which represents Rectangle and Point concepts (as usual) and a user
5
8748
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k) throw(std::runtime_error)
3
3714
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I can't think of a good fix, or even why it broke. The problem In one of my CFormView derived classes I have a member variable of the type...
13
9674
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
3
2061
by: digz | last post by:
This is a very simplified version of something I am trying to understand. The State object holds the strings and maps , and I pass a reference to State to the process Function which manipulates it based on the tag passed in. I cannot pass the strings/maps in question as references coz .. a) thats redundant, state has everything, it increases number of parameters unnecessarily b) I have to do the case/switch outside the function to...
7
10265
by: guido | last post by:
Hi, I'm looking for a container class that can map whole ranges of keys to objects - something like std::map, but not only for individual values for the key, but for whole ranges. Example: I want to be able to tell the container to return object a for every given key between 0 and 10, object c for every key between 11 and 500000 and object c for every key between 500001 and 599999, without having to
6
5437
by: Juha Nieminen | last post by:
joseph cook wrote: Not always. By default, yes, but you can specify other comparators, eg: std::map<int, int, std::greaterreversedMap;
0
9868
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
9836
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
9707
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6533
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
5139
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...
0
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
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
2
3352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2664
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.