Thread: lists (arrays, vectors, etc.) of different types

  1. #1
    Registered User
    Join Date
    Aug 2006

    lists (arrays, vectors, etc.) of different types

    How would you implement a list composed of different types in C++, similar to Python's lists?

    For example:
    a = [3.14, "banana", 12, ["another list", 42]]

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    Boost supports some stuff similar to that. Are you just asking for the sake of posterity or do you have a legitimate reason for such a thing?

  3. #3
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    If all entry's will have the same types then I'd create a struct containing all the types then use std::list or another std class template. the template you need to use really depends on what your going to do with your list.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  4. #4
    Registered User
    Join Date
    Jan 2005
    I believe boost::variant is for when you have a specific set of types you'll be using, and boost::any is for when the set of types is open-ended.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Without knowing any already made library, I would use a I list of void* and a list of their respective types. And a functions/marcos for the dereferencing, all pact up in a nice class. But whatever you do you won't be able to manipulate them as nicely as in the built-in lists in Python. So it would be kind of worthless

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and linked lists in place of arrays
    By kordric in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2008, 10:59 AM
  2. From Python's Linked Lists to Dynamic Arrays
    By hexbox in forum C Programming
    Replies: 3
    Last Post: 01-26-2005, 03:14 PM
  3. Sorting 2.. 1-D arrays of different types
    By GaGi in forum C Programming
    Replies: 3
    Last Post: 04-04-2002, 05:29 PM
  4. Vectors vs. Linked Lists
    By ginoitalo in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2002, 03:12 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM