Как я могу вставить несколько элементов каждого типа данных в варианте? - PullRequest
0 голосов
/ 01 мая 2020

Я могу хранить 1 элемент каждого типа данных в варианте, например:

using var_t = variant<int, float, long, double, string>;

, а затем примерно так:

vector<var_t> example1 = {1, 1.2f, 345l, 67.89, "S"};

Но как можно Я вставляю несколько элементов каждого типа данных в один из вариантов? что-то вроде этого, это кажется правильным.

using ints = vector<int>;
using floats = vector<float>;
using longs = vector<long>;
using doubles = vector<double>;
using strings = vector<string>;

// вариант векторов нескольких типов данных:

using mixed_data_types = variant<ints,floats,longs,doubles,strings>;

ОБНОВЛЕНИЕ:

// ТЕПЕРЬ РАБОТАЕТ НИЖЕ ЛИНИЯ, СПАСИБО @CaptainRuhrpott (StackOverflow)

//vector<mixed_data_types> example2 = {{1,1}, {2.1f,2.2f}, {3.1l,3.2l}, {4.4,4.5}, {"Hello", "Hi"}};

// Changed to
// vector<mixed_data_types> example2 = {{ ints{1,1,2,3,4,5,6,7,8,9,10}}, { floats{2.1f,2.2f}}, {longs{330l,337l}}, {doubles{4.4,4.5}}, {strings{"Hello", "Hi"}}};

// Итератор для примера2

for(auto& v: example2) {
        //1. void visitor, only called for side-effects (here, for I/O)
        visit([](auto&& arg){
            //cout << typeid(arg).name() <<"\n";
                for(auto& r: arg){

                    // cout << typeid(r).name() << " ";
                }
            cout << "\n";
        }, v);
}

// ГЛАВНЫЙ КОД НАЧИНАЕТСЯ ЗДЕСЬ

#include <iostream>
#include <sstream>
#include <typeinfo>
#include <string>
//#include <cstring>
#include <iterator>
#include <vector>
#include <variant>
//#include <algorithm>

using namespace std;

using ints = vector<int>;
using floats = vector<float>;
using longs = vector<long>;
using doubles = vector<double>;
using strings = vector<string>;

// variant of vectors of multiple data types :
using mixed_data_types = variant<ints,floats,longs,doubles,strings>;

// this is working fine, variant of multiple data types
using var_t = variant<int, float, long, double, string>;

using another_mixed = variant<ints,float,long,double,string>;

static unsigned int intCount = 0;
static unsigned int floatCount = 0;
static unsigned int longCount = 0;
static unsigned int doubleCount = 0;
static unsigned int stringCount = 0;

int main (){

    // using var_t = variant<int, float, long, double, string>;
    string progress (100, 254);
    vector<var_t> example1 = {1, 1.2f, 345l, 67.89, "S"}; // vector of variant of multiple data_types
    string ex1 = "vector<var_t> example1 = {1, 1.2f, 345l, 67.89, \"S\"};";
    // THIS ALSO SEEMS TO BE VALID 
    // using mixed_data_types = variant<ints,floats,longs,doubles,strings>;

    // NOW THE BELOW LINE IS WORKING FINE, THANKS TO @CaptainRuhrpott (StackOverflow)
    vector<mixed_data_types> example2 = {{ ints{1,1,2,3,4,5,6,7,8,9,10}}, { floats{2.1f,2.2f}}, {longs{330l,337l}}, {doubles{4.4,4.5}}, {strings{"Hello", "Hi"}}};
    string ex2 = "vector<mixed_data_types> example2 = {{ ints{1,1,2,3,4,5,6,7,8,9,10}}, { floats{2.1f,2.2f}}, {longs{330l,337l}}, {doubles{4.4,4.5}}, {strings{\"Hello\", \"Hi\"}}};";
    // progress bar
    cout << progress <<"\n";
    cout << "Example 1: " << ex1 <<"\n";
    // working fine for example 1
    for(auto& v: example1) {
        //1. void visitor, only called for side-effects (here, for I/O)
        visit([](auto&& arg){
            string data_type = "";
            string keepArgs = typeid(arg).name();
            if (keepArgs == "i") { data_type = "int"; intCount++;}
            if (keepArgs == "f") { data_type = "float"; floatCount++;}
            if (keepArgs == "l") { data_type = "long"; longCount++;}
            if (keepArgs == "d") { data_type = "double"; doubleCount++;}

            // This may not work in some compilers
            if ((keepArgs == "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE") || (keepArgs == "St7variantIJifldSsEE")) { data_type = "std::string"; stringCount++;}
            cout << "Value is: " <<arg << " and it's data-type is: " << data_type << "\n";
            //cout << typeid(arg).name() << "\n";
        }, v);
    }

    cout << progress <<"\n";
    cout << "Example 2: " << ex2 <<"\n";
    cout << "Please identify the data types by the following short names.." << "\n";
    cout << "i=int, f=long, l=long, d=double, s=string" << "\n";
    cout << "e.g, 1-->i, means 1 is int type" << "\n";
    cout << "e.g, 2.1-->f, means 2.1 is float type" << "\n";
    cout << progress <<"\n";

    for(auto& v: example2) {
        //1. void visitor, only called for side-effects (here, for I/O)
        visit([](auto&& arg){

            string data_type2 = "";
            string keepArgs2 = typeid(arg).name();
            if (keepArgs2 == "St6vectorIiSaIiEE") { data_type2 = "vector<int>";}
            if (keepArgs2 == "St6vectorIfSaIfEE") { data_type2 = "vector<float>";}
            if (keepArgs2 == "St6vectorIlSaIlEE") { data_type2 = "vector<long>";}
            if (keepArgs2 == "St6vectorIdSaIdEE") { data_type2 = "vector<double>";}
            if (keepArgs2 == "St6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE") { data_type2 = "vector<string>";}

            cout << data_type2 <<"\n";
            //cout << arg << "\n";
            for(auto& r: arg){
            string data_type3 = "";
            string keepArgs3 = typeid(r).name();
            if (keepArgs3 == "i") { data_type3 = "i"; intCount++;}
            if (keepArgs3 == "f") { data_type3 = "f"; floatCount++;}
            if (keepArgs3 == "l") { data_type3 = "l"; longCount++;}
            if (keepArgs3 == "d") { data_type3 = "d"; doubleCount++;}

            // This may not work in some compilers
            if ((keepArgs3 == "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE") || (keepArgs2 == "St7variantIJifldSsEE")) { data_type3 = "s"; stringCount++;}
            cout << r << "-->" << data_type3 << " ";
            }
            cout << "\n";
        }, v);
    }

    cout << progress <<"\n";

    cout << "Total Counts: " << "\n" <<
    "integer Types = " << intCount << "\n" <<
    "float Types = " << floatCount << "\n" <<
    "long Types = " << longCount << "\n" <<
    "double Types = " << doubleCount << "\n" <<
    "string Types = " << stringCount << "\n";


    return 0;
}

1 Ответ

1 голос
/ 01 мая 2020

Компилятор, похоже, не может определить, какой тип должен иметь список в скобках. Однако вы можете использовать прямой инициализатор списка внутреннего std::vector

vector<mixed_data_types> example2 = { { floats { 1.0f, 2.0f } } };
...