Search code examples
c++arraystypesmutable

C++ mutable multitype array


I am quite new to C++ but am familiar with a few other languages. I would like to use a data type similar to a Java ArrayList, an Objective-c NSMutableArray or a Python array, but in C++. The characteristics I am looking for are the possibility to initialize the array without a capacity (thus to be able to add items gradually), and the capability to store multiple datatypes in one array.

To give you details of what I want it for is to read data from different tables of a mysql db, without knowing the number of fields in the table, and being able to move this data around. My ideal data type would allow me to store something like this:

idealArray = [Bool error,[string array],[string array]];

where the string arrays may have different sizes, from 1 to 20 in size (relatively small).

I don't know if this is possible in C++, any help appreciated, or links towards good ressources.

Thanks


Solution

  • You may use structure or class to store (named) multiple data types together, such as:

    class Record
    {
       bool _error;
       vector<string> _v1;
       vector<string> _v2;
    };
    vector<Record> vec;
    

    or std::tuple to store (unnamed) multiple data types, e.g.

    vector<tuple<bool, vector<string>, vector<string> > > vec;