Thread: A question about STL Lists.

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    77

    A question about STL Lists.

    I am gonna to write an ADT that represents the pile of cards used in a solitaire game. At first, i create a header file with class Pile.


    Code:
    // Pile.h
    #ifndef PILE_H
    #define PILE_H
    
    
    class Pile
    {
    public:
    	void addCard(const Card&card); //Adds a card to the Pile
    
    	void dump() const;
    
    
    private:
    
    	list<Card> cards; // Adds a card to the Pile
    
    
    
    
    };
    #endif

    My question is, inside the private, can i use
    Code:
    list<Card> cards; // Adds a card to the Pile
    to feed this requitement -> The Pile must have one attribute - a STL List of Cards.


    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I don't see why not.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Thanks, just wanna make sure it.

    However, How can i implement the method addCard, How can i code it to make it can add card to the Pile?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    push_front or push_back, perhaps?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Also, for the #include header.

    i think i have to use
    Code:
    #include <cstdlib>
    and anymore?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    #include <list>
    for the actual STL list
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't forget to indicate the proper namespace. For example, use 'std::list' instead of just 'list' in the code.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Daved, i don't really understand about it, would you please give me an example inside my code?
    thanks

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    // Pile.h
    #ifndef PILE_H
    #define PILE_H
     
    #include <list>
     
    class Pile
    {
    public:
    	void addCard(const Card&card); //Adds a card to the Pile
    	void dump() const;
    private:
    	std::list<Card> cards; // Adds a card to the Pile
    };
    #endif
    Or you could do this (not recommended, but "easier"):
    Code:
    // Pile.h
    #ifndef PILE_H
    #define PILE_H
     
    #include <list>
    using std::list;
     
    class Pile
    {
    public:
    	void addCard(const Card&card); //Adds a card to the Pile
    	void dump() const;
    private:
    	list<Card> cards; // Adds a card to the Pile
    };
    #endif
    Or this (not recommended, but "easiest"):
    Code:
    // Pile.h
    #ifndef PILE_H
    #define PILE_H
     
    #include <list>
    using namespace std;
     
    class Pile
    {
    public:
    	void addCard(const Card&card); //Adds a card to the Pile
    	void dump() const;
    private:
    	list<Card> cards; // Adds a card to the Pile
    };
    #endif

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Thanks for advise!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL Vector question
    By Beamu in forum C++ Programming
    Replies: 8
    Last Post: 01-19-2009, 12:51 PM
  2. Linked Lists Question
    By panfilero in forum C Programming
    Replies: 4
    Last Post: 11-22-2005, 01:33 AM
  3. Linked Lists Question
    By SlyMaelstrom in forum C++ Programming
    Replies: 12
    Last Post: 11-12-2005, 12:03 PM
  4. Question about using erase() with lists (STL)
    By apacz in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2005, 11:51 AM
  5. STL container question
    By PJYelton in forum C++ Programming
    Replies: 8
    Last Post: 12-16-2002, 08:40 PM