Can <list> be applied to user-defined class in C++?

Discussion in 'C++' started by Kenneth, Oct 21, 2005.

  1. Kenneth

    Kenneth Guest

    <list> seems to be a powerful structure to store the related nodes in
    memory for fast operations, but the examples I found are all related to
    primitive type storage.
    I'm doing a project on C++ with my defined classes to be added to
    linked list structure so as to facilitate the operation of all
    instances of defined classes. Is that possible to apply such classes to
    <list> or <Vector> structure?

    Thanks!
    Kenneth, Oct 21, 2005
    #1
    1. Advertising

  2. "Kenneth" <> wrote in message
    news:...
    > <list> seems to be a powerful structure to store the related nodes in
    > memory for fast operations, but the examples I found are all related to
    > primitive type storage.
    > I'm doing a project on C++ with my defined classes to be added to
    > linked list structure so as to facilitate the operation of all
    > instances of defined classes. Is that possible to apply such classes to
    > <list> or <Vector> structure?
    >
    > Thanks!
    >


    yes!

    but there are certain requirements... i think you need public default and
    copy constructor (maybe assignment operator too, not sure, check the C++
    standard, or any STL bood)
    Martin Vorbrodt, Oct 21, 2005
    #2
    1. Advertising

  3. Kenneth

    Jim Langston Guest

    "Kenneth" <> wrote in message
    news:...
    > <list> seems to be a powerful structure to store the related nodes in
    > memory for fast operations, but the examples I found are all related to
    > primitive type storage.
    > I'm doing a project on C++ with my defined classes to be added to
    > linked list structure so as to facilitate the operation of all
    > instances of defined classes. Is that possible to apply such classes to
    > <list> or <Vector> structure?
    >
    > Thanks!
    >


    Yeah. I do it all the time. Pretty much the same way you do it with built
    in primitive types.

    If your class does any memory management (new, delete) itself make sure you
    have copy and assignment methods.

    vector<MyClass> MyVector;

    MyVector.push_back( new MyClass );

    MyClass TempInstance;
    MyVector.push_back( TempInstance );
    Jim Langston, Oct 21, 2005
    #3
  4. Kenneth

    Mike Wahler Guest

    "Kenneth" <> wrote in message
    news:...
    > <list> seems to be a powerful structure to store the related nodes in
    > memory for fast operations, but the examples I found are all related to
    > primitive type storage.
    > I'm doing a project on C++ with my defined classes to be added to
    > linked list structure so as to facilitate the operation of all
    > instances of defined classes. Is that possible to apply such classes to
    > <list> or <Vector> structure?


    Certainly.

    class C
    {
    int i;
    public:
    C(int arg) : i(arg)
    {
    }
    };

    std::list<C> my_list;

    The class can be as simple or as complex as
    you like (but of course still subject to
    'resource allocation' issues, e.g. the
    'rule of three').

    -Mike
    Mike Wahler, Oct 21, 2005
    #4
  5. Kenneth

    Peter_Julian Guest

    "Kenneth" <> wrote in message
    news:...
    | <list> seems to be a powerful structure to store the related nodes in
    | memory for fast operations, but the examples I found are all related
    to
    | primitive type storage.
    | I'm doing a project on C++ with my defined classes to be added to
    | linked list structure so as to facilitate the operation of all
    | instances of defined classes. Is that possible to apply such classes
    to
    | <list> or <Vector> structure?
    |

    Not only can a list store some particular node-type, but virtually any
    node type, including your own. I've left out one of the requirements,
    the assignment operator for the nodes. The std::list container itself
    can be encapsulated within a templated class with your own interface.

    #include <iterator> // for std::eek:stream_iterator
    #include <algorithm> // for std::copy
    #include <list>
    #include <ostream>
    #include <iostream>

    // my own list container class
    template< class T >
    class TList
    {
    std::list< T > m_list;
    public:
    TList() : m_list() { }
    ~TList() { }
    void push_back(const T& t)
    {
    m_list.push_back(t);
    }
    void pop_back()
    {
    m_list.pop_back();
    }
    void display() const
    {
    std::cout << "list contents:\n ";
    std::copy( m_list.begin(),
    m_list.end(),
    std::eek:stream_iterator< T >( std::cout, " ") );
    std::cout << std::endl;
    }
    };

    // a templated, complex Node type
    template< class N, class D, class L>
    class Node
    {
    N m_n;
    D m_d;
    L m_l;
    public:
    Node(N n, D d, L l) : m_n(n), m_d(d), m_l(l) { }
    Node(const Node& copy)
    {
    m_n = copy.m_n;
    m_d = copy.m_d;
    m_l = copy.m_l;
    }
    ~Node() { }
    /* friends */
    friend std::eek:stream&
    operator<<(std::eek:stream& os, const Node& a)
    {
    os << a.m_n << ", " << a.m_d << ", " << a.m_l;
    return os << std::endl;
    }
    };

    int main()
    {
    TList<int> nlist; // a list of integers
    typedef Node<int, double, long> t_Node;
    TList< t_Node > nodelist; // a list of complex nodes

    for ( int sz = 0; sz < 10; ++sz)
    {
    nlist.push_back(sz);
    nodelist.push_back( t_Node(sz, sz + 0.1, sz * 10) );
    }

    nlist.display();
    nodelist.display();

    return 0;
    }

    /* output:

    list contents:
    0 1 2 3 4 5 6 7 8 9
    list contents:
    0, 0.1, 0
    1, 1.1, 10
    2, 2.1, 20
    3, 3.1, 30
    4, 4.1, 40
    5, 5.1, 50
    6, 6.1, 60
    7, 7.1, 70
    8, 8.1, 80
    9, 9.1, 90

    */

    The same applies to any other standard container (std::vector,
    std::queue, std::deque, etc) except that associative containers (set,
    map, multiset, multimap, etc) have additional requirements other than
    copy ctor and operator=.

    Even the node type itself can be composed of other templated user-types.
    And that would not require any modification of either the Node or TList
    class (You can derive from TList to provide supplimentary features).
    Comparitively, the work required is minute when you consider the
    flexibility and reusability of the templated classes.
    Peter_Julian, Oct 21, 2005
    #5
  6. Kenneth

    Axter Guest

    Kenneth wrote:
    > <list> seems to be a powerful structure to store the related nodes in
    > memory for fast operations, but the examples I found are all related to
    > primitive type storage.
    > I'm doing a project on C++ with my defined classes to be added to
    > linked list structure so as to facilitate the operation of all
    > instances of defined classes. Is that possible to apply such classes to
    > <list> or <Vector> structure?
    >
    > Thanks!


    For non-abstract class, yes.
    However, if you're trying to store derived types using a base type,
    then you would have to use a pointer, or a smart pointer.

    vector<foo*> vMyFoo;
    //Or even better, use smart pointer
    vector<boost::shared_ptr<foo> > vMyFoo; //Boost shared pointer

    vector<copy_ptr<foo> > vMyFoo; //A clone pointer

    I recommend the clone pointer over boost shared pointer because it
    works like concrete types and when making comparisons it lets you use
    value semantics.

    You can download the copy_ptr from the following link:
    http://code.axter.com/copy_ptr.h

    You can also use a COW (Copy On Write) smart pointer:
    http://code.axter.com/cow_ptr.h
    Axter, Oct 21, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok
    Replies:
    2
    Views:
    359
    Mantorok
    Jan 12, 2006
  2. Petterson Mikael
    Replies:
    3
    Views:
    4,277
    Neal Gafter
    Oct 8, 2003
  3. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  4. Realove
    Replies:
    0
    Views:
    451
    Realove
    May 20, 2008
  5. Replies:
    1
    Views:
    493
    Sion Arrowsmith
    Jul 10, 2008
Loading...

Share This Page