C++ Linked List or Hash Table

Discussion in 'Web Design & Coding' started by Complete, Nov 15, 2007.

  1. Complete

    Complete OSNN Addict

    Messages:
    94
    In the C++ Standard Template Library (STL) is there a ready-to-use Hash Table? If not, is there a good linked list?

    What advantages are there to me writing one fron scratch? What about using one of the MFC classes?
     
  2. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    std::list is a linked list. Doubly linked.

    As for hash table, how are you planning on using this structure? Show us some usage scenario's and maybe we can suggest something that will work well.
     
  3. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    The STL doesn't include a hash table implementation. It does include a doubly linked list though - see list.

    Advantages over writing from scratch? It'll save you development time. :) The STL data structures are also well-tested and widely used, so you can be reasonably sure that there aren't any major bugs in their implementations.
     
  4. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
  5. Complete

    Complete OSNN Addict

    Messages:
    94
    I think I will keep it as simple as possible.
    But using STL is brand new to me.

    I want to use nodes with two strings.

    This is all I have thus far:

    Code:
     
    #include <list> // list class-template definition
    using namespace std; 
    class Node
    {
       CString string1;
       CString string2;
    }
     
    list<Node> MyList;
    
    Now, how would one add a new node and "push" it into the list?
     
  6. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    Yeah, I was aware of the SGI hash_map, but didn't want to suggest something that's not included by default. And yes, a map or multi_map would work well if you need to store key-value pairs.
     
  7. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    You would then create an instance of Node and pass it in as an argument to MyList.push_front() or MyList.push_back(), depending on whether you want the node to be added to the head or tail end of the list.