HOw to pass std::map as parameter?

Discussion in 'C++' started by Woodster, Nov 14, 2003.

  1. Woodster

    Woodster Guest

    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
     
    Woodster, Nov 14, 2003
    #1
    1. Advertising

  2. Woodster

    Marco Wahl Guest

    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
     
    Marco Wahl, Nov 14, 2003
    #2
    1. Advertising

  3. Woodster

    David White Guest

    "Woodster" <mirror@127.0.0.1> wrote in message
    news:...
    > 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
     
    David White, Nov 14, 2003
    #3
  4. Woodster

    jeffc Guest

    "Woodster" <mirror@127.0.0.1> wrote in message
    news:...
    > 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.
     
    jeffc, Nov 14, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Hildebrand
    Replies:
    5
    Views:
    7,978
    krogers
    Mar 20, 2012
  2. Peter Jansson
    Replies:
    5
    Views:
    6,314
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    1
    Views:
    428
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,025
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,005
    James Kanze
    Dec 21, 2008
Loading...

Share This Page