What is a Map/Table? How is it Implemented?

This is a discussion on What is a Map/Table? How is it Implemented? within the C++ Programming forums, part of the General Programming Boards category; Hello, I would like to know What a Map/Table is? Im sure its just a way to organize data to ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    49

    What is a Map/Table? How is it Implemented?

    Hello, I would like to know What a Map/Table is? Im sure its just a way to organize data to look up. Like A Regular table and map would be. But I dont know how its implemented in code. Can Someone explain to me what it is in better terms, and Show me an example of a simple one implemented in Code? Thanks

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    The standard map is usually implemented as a red-black tree. You can find an implementation and description here.
    Kampai!

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Example use of a map:
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main(void)
    {
        typedef map<string,float> StrFltMap;
        StrFltMap FruitPrices;
        StrFltMap::iterator it;
        string search;
    
        FruitPrices["Pear"] = 0.66f;
        FruitPrices["Apple"] = 0.89f;
        FruitPrices["Kiwi"] = 1.19f;
        FruitPrices["Banana"] = 0.59f;
    
        cout << "Welcome to Bob's Produce Market" << endl;
        cout << "Available fruits and their prices:" << endl;
        for( it = FruitPrices.begin(); it != FruitPrices.end(); ++it )
            cout << it->first << " costs " << it->second << "/lb." << endl;
    
        cout << "\nEnter in a fruit to search for: ";
        getline(cin,search);
        while( search.length() )
        {
            if( (it = FruitPrices.find(search)) != FruitPrices.end() )
                cout << "That fruit costs " << it->second << "/lb." << endl;
            else
                cout << "Sorry, we do not have that fruit in stock." << endl;
    
            cout << "\nEnter in a fruit to search for: ";
            getline(cin,search);
        }
    
        cout << "Goodbye!" << endl;
        cin.get();
    }
    Notice in the output below that the map container automatically sorts its entries according to the key value regardless of the order in which they were inserted into the map. I.e. Apple comes before Banana which comes before Kiwi which comes before Pear:

    Code:
    Welcome to Bob's Produce Market
    Available fruits and their prices:
    Apple costs 0.89/lb.
    Banana costs 0.59/lb.
    Kiwi costs 1.19/lb.
    Pear costs 0.66/lb.
    
    Enter in a fruit to search for: Banana
    That fruit costs 0.59/lb.
    
    Enter in a fruit to search for: Pear
    That fruit costs 0.66/lb.
    
    Enter in a fruit to search for: Apple
    That fruit costs 0.89/lb.
    
    Enter in a fruit to search for: blah
    Sorry, we do not have that fruit in stock.
    
    Enter in a fruit to search for: 
    Goodbye!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Sake is more in the direction I wanted. The Standard library Map isnt what I was looking for. I was meaning to create my own map, not use someone elses. But thank you Hk. Sake the Information you gave me is going to take me a while to get and read. Its a little complex from what I seen. A Binary Tree is a map then? Also I have another Question, How are the values in the Hex base Number system maped to the Values they should be? Like A is 10 and B is 11. But in ASCI ITs not 10 or 11. How Does THe compilers and stuff know It is 10 or 11. I sthere a map it uses to check it or something? What other data structures are used in making maps? Thanks for the replies

  5. #5
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>A Binary Tree is a map then?
    A map is an abstract concept of a collection of items that are accessed by a key. It just happens that a binary search tree is a very close match to that concept without any tweaking. If you wanted you could implement a map as a hash table, or a simple array.

    >>How are the values in the Hex base Number system maped to the Values they should be?
    It's all binary.

    >>I sthere a map it uses to check it or something?
    That's totally up to the system, but it could be implemented that way.
    Kampai!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SpaceX nebulas implemented
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 01-31-2009, 10:32 PM
  2. Implemented vertex arrays, prob. rendering
    By BobMcGee123 in forum Game Programming
    Replies: 8
    Last Post: 07-22-2006, 09:00 PM
  3. Binary search tree implemented by use of an array
    By supa_scoopa in forum C Programming
    Replies: 4
    Last Post: 02-09-2006, 08:19 AM
  4. How events are implemented in any OS?
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-11-2004, 03:02 AM
  5. how are copy protections implemented?
    By mickey in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 10:29 PM

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