Sign in to follow this  

Creating python-like lists in C++

Recommended Posts

I've been having a lot of trouble creating python-like lists in C++. I'm working on my own scripting language, and I figured lists like this would be a useful feature. I basically want it to be something recursive, like an std::vector of type std::pair, containing 1) A piece of data indicating either the data-type of the list item, or indicating that the item is a list, and 2) either the list data, or a new list itself. I almost had something working, using a datatype like this: std::vector< std::pair< int, void* > > where the int would be 1 to mean it's an integer, or 2 to mean it's a list, and the void* is either an int or a new list of the same type as mentioned above. The type casting at this bit was difficult. This was almost what I had to display one of the lists:
void ShowList( std::vector< std::pair< int, void* > > List )
    for ( size_t n = 0; n < List.size(); n++ )
        if ( n ).first == 1 )
            std::cout << n ).second << std::endl;   // This worked fine

        if ( n ).first == 2 )
            ShowList( n ).second )     // The recursive bit. Couldn't typecast the void*
Can anyone help me figure this one out? Is there a container out there that can be used? Cheers

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this