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

This is a discussion on lists (arrays, vectors, etc.) of different types within the C++ Programming forums, part of the General Programming Boards category; How would you implement a list composed of different types in C++, similar to Python's lists? For example: Code: a ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    43

    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:
    Code:
    a = [3.14, "banana", 12, ["another list", 42]]

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    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
    Posts
    7,328
    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
    Posts
    1,853
    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, 02:14 PM
  3. Sorting 2.. 1-D arrays of different types
    By GaGi in forum C Programming
    Replies: 3
    Last Post: 04-04-2002, 04:29 PM
  4. Vectors vs. Linked Lists
    By ginoitalo in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2002, 02:12 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

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