Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > HOw to pass std::map as parameter?

Reply
Thread Tools

HOw to pass std::map as parameter?

 
 
Woodster
Guest
Posts: n/a
 
      11-14-2003
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
 
Reply With Quote
 
 
 
 
Marco Wahl
Guest
Posts: n/a
 
      11-14-2003
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

 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      11-14-2003
"Woodster" <mirror@127.0.0.1> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      11-14-2003

"Woodster" <mirror@127.0.0.1> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
difference between pass by address and pass by reference!! blufox C Programming 2 04-03-2006 02:53 PM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM
pass by ref vs pass by pointer Radde C++ 38 08-23-2005 06:29 PM
Pass-by-reference instead of pass-by-pointer = a bad idea? Mr A C++ 111 07-14-2005 03:04 AM



Advertisments