maps as reference?

This is a discussion on maps as reference? within the C++ Programming forums, part of the General Programming Boards category; hey, I've got a function that i need to return two maps from. As i can't do it with a ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    maps as reference?

    hey,

    I've got a function that i need to return two maps from. As i can't do it with a <code> return </code> is there a way to pass the two maps as reference parameters (like is possible with vectors or arrays) and thus make it possible to modify both in the function?

    cheers every1.

    Rob.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Since you know that references work for some types, doesn't it seem reasonable that references OUGHT to work for some other types?

    In other words, if you've already tried and failed, then post some of what you tried here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've got a function that i need to return two maps from.
    Passing them by reference to the function would work just fine. Or if you need to create them locally and return them, try returning a pair:
    Code:
    #include <utility>
    
    // Replace with a more suitable typedef
    typedef map<string, int> maptype;
    
    pair<maptype, maptype> foo()
    {
      maptype a;
      maptype b;
    
      // ...
    
      return make_pair ( a, b );
    }
    Then you have both maps in an easily accessed location that suggests a relationship. Of course, if they are completely unrelated, a pair may be confusing to readers.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21