• Advertisement
Sign in to follow this  

Creating python-like lists in C++

This topic is 3389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 ( 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

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement