Thread: initialising an associative container

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    initialising an associative container

    I was trying to initialise an associative container (map) in a function, but couldn't. Seems like whatever is initialised in the function stays in the function. That's natural for a function. So, how are we going to initialise the container in a function and use it in the main function. The initialising statement looks something like this:

    map<char, int>phone_book;

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Hmm.. your problem is very unclear. Are you trying to do something like this?

    Code:
    void someFunction() {
    	map<char,int> phone_book;
    	// do some stuff with phone_book
    }
    
    int main() {
    	someFunction();
    	// now try to use phone_book
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    You could make the container global, but that is generally bad idea. Other option is to let the function return a pointer to the map it created.

    Code:
    map<char, int>* CreateMap(void)
    {
      map<char, int>* pMap = NULL;
      pMap = new map<char, int>;
    
      // do something here, like check pMap != NULL
    
      return pMap;
    }
    
    int main(void)
    {
      map<char, int>* MapPtr;
    
      MapPtr = CreateMap();
    
      // remember to destroy the allocated map
      delete MapPtr;
    }
    Making error is human, but for messing things thoroughly it takes a computer

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    By initializing pointer?
    typedef std::map<char, char> CharMap;
    CharMap* theMap;
    void Init()
    {
    theMap = new CharMap;
    }

    or

    CharMap* CreateMap()
    {
    return new CharMap;
    }

    Is this your problem?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    4
    sorry for the confusing question.

    The below program works:

    #include <stdio.h>
    #include <iostream>
    #include <map>
    using namespace std;

    int main() {
    int i;
    char ch;

    map<char, int>table; //look-up table
    map<char, int>::iterator p;

    for(i=0; i<26; i++) {
    table.insert(pair<char, int>('A'+i, 65+i));
    }

    cout << "Enter key: ";
    cin >> ch;

    p = table.find(ch);
    if(p != table.end())
    cout << "Its ASCII value is " << p->second;
    else
    cout << "Key not in map.\n";

    return 0;
    }

    But when I build a map in a function like this:

    #include <stdio.h>
    #include <iostream>
    #include <map>
    using namespace std;

    void BuildMap(void); // map building function

    int main() {
    int i;
    char ch;

    BuildMap(); // create table here

    for(i=0; i<26; i++) {
    table.insert(pair<char, int>('A'+i, 65+i));
    }

    cout << "Enter key: ";
    cin >> ch;

    p = table.find(ch);
    if(p != table.end())
    cout << "Its ASCII value is " << p->second;
    else
    cout << "Key not in map.\n";

    return 0;
    }

    void BuildMap(void) {

    map<char, int>table; //look-up table
    map<char, int>::iterator p;

    }

    The main program does not reconise 'table' and 'p'. I'm using the STL(Standard Template Library) in C++.

    hope this clarify the problem.
    thanks for the previous reply.

  6. #6
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    The main program does not reconise 'table' and 'p'
    That's obvious because map and its iterator are local variables in the function BuildMap(). You have few reasonable ways. The one I prefer is to return the pointer to newly created map for the calling function from BuildMap() and initialize the iterator locally when you need it. So you don't need any global variables.

    There is a code example for you in my previous reply. I hope this helps.
    Making error is human, but for messing things thoroughly it takes a computer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return *any* container
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-18-2008, 02:39 AM
  2. sorting container
    By l2u in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2007, 01:12 PM
  3. stl container to store more than 1 type
    By sujeet1 in forum C++ Programming
    Replies: 7
    Last Post: 05-09-2007, 04:10 AM
  4. Replies: 1
    Last Post: 01-23-2006, 07:12 PM
  5. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM