Thread: is it possible to pass an std map as a parameter to a user defined function?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    is it possible to pass an std map as a parameter to a user defined function?

    i have a std map that stores information obtained within main() and is then needed within a user defined function, and i was wondering if it was possible. i cant just pass the value of the map into the function, as the key is determined within the function itself.
    if it is possible, how do i prototype the function, i.e. what do i set the parameter type as?

    thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Post the code you're working with (because what you're describing doesn't make a whole lot of sense).

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not sure why you think a map would work differently from any other type, as long as you know what it's a map of.
    Code:
    int my_function(std::map<std::string, int> hey_look_a_map)

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, except that the OP stated that the key is defined within the function, which is confusing.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I had assumed OP meant that the key used to access the map was determined inside the function (which would have to be true anyway), not that the type of the key was indeterminate. We'll have to see.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Quote Originally Posted by Sebastiani View Post
    Post the code you're working with (because what you're describing doesn't make a whole lot of sense).




    my project is to build an assembler for a mips program i wrote

    i have this map in my main function:

    Code:
    string printCode(string, string&)
    
    int main()
        {
            map <string, int> labList;
    
    
    .
    .
    .
    
    	while(!file_read.eof())
            {
    			
    		string s,t,u;
    		getline(file_read, line,'\n');     
    		addec=makeAddress(lineNumber);       
    		file_write<<setfill('0')<<setw(8)<<hex<<addec;
    		file_write<<": ";
    		t=printCode (line,u);				
    		if (t=="label")
    		{
    			file_write<<"<"<<u.erase((u.length()-1),1)<<">"<<endl;
    			labList[u]=addec;
    		}
    that as it loops through this while code, it saves the address of labels in the map as they [the labels] come up.
    i have an idea that involves passing in the map labList into the printCode function (which finds the binary of each line in mips). to get printCode to properly make branch functions, it needs a list of labels and their addresses, hence the map.

    i tried prototyping a fuction like

    Code:
    void function (string, map);
    but it throws an error with map as a parameter.


    thus my question is: Can i pass std maps as parameters to functions, kinda like passing a pointer to an array

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    I'm not sure why you think a map would work differently from any other type, as long as you know what it's a map of.
    Code:
    int my_function(std::map<std::string, int> hey_look_a_map)
    oh! that's my problem: i wasnt putting the map name after the declaration; i was just putting
    Code:
    int function(map<string, int>)
    as opposed to
    Code:
    int function(map<string, int>map_name)

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Nyarlathotep_ View Post
    oh! that's my problem: i wasnt putting the map name after the declaration; i was just putting
    Code:
    int function(map<string, int>)
    as opposed to
    Code:
    int function(map<string, int>map_name)
    That doesn't really match what you said in your previous post, but at any rate, how else could you process a variable within a function, if it has no 'name' to reference it with? Seems like you might want to sit down and spend a bit more time studying the language.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Unless you plan to modify the map inside the function, you should pass a const reference to it to avoid the extra overhead of copying the map:
    Code:
    int function( const map<string, int>& map_name )
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I would recommend using a typedef for the map.

    Code:
    ...
    typedef std::map<std::string,int> IntMap;
    ...
    int function(const IntMap & mapName)
    ...
    On a side note, if your map is going to store large objects I would recommend storing pointers to the objects in the map rather than storing the objects themselves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM