Skip to main content
gamedev.net
🔒 Locked

Creating python-like lists in C++

Started by Zotoaster 1 reply 1.2k views
Original Post
Zotoaster
Zotoaster
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 ( List.at( n ).first == 1 )
            std::cout << List.at( n ).second << std::endl;   // This worked fine

        if ( List.at( n ).first == 2 )
            ShowList( List.at( 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
Antheus
Antheus
Quote:
Original post by Zotoaster

Can anyone help me figure this one out? Is there a container out there that can be used?


boost::any or cdiggins::any. Use vector of those.
Zotoaster
Zotoaster
Thanks Antheus, that cdiggins is just awesome. Cheers.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.