473,402 Members | 2,072 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,402 software developers and data experts.

HOw to pass std::map as parameter?

I have declared the following

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

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

void function(std::map<std::string, std::string>);

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

Thanks in advance

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

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

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

void function(std::map<std::string, std::string>);

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

Yes.
void function(const std::map<std::string, 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.a u...
I have declared the following

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

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

void function(std::map<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::map<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::string, std::string> &);

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

Then use MyMap everywhere you would have used std::map<std::string,
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.a u...
I have declared the following

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

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

void function(std::map<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
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)...
5
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...
1
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...
5
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)...
3
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...
13
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
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...
7
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...
6
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
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
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...
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,...
0
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...

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.