如何在一个变量中插入每个数据类型的多个项目?

问题描述 投票:0回答:1

我可以在一个变量中存储每个数据类型的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>;

UPDATE :

/ NOW THE BELOW LINE IS WORKING FINE, THANKS TO @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;
}
c++ c++11 vector stl variant
1个回答
1
投票

编译器似乎无法推断括号中的初始化列表应该是什么类型。然而,你可以使用内部的直接列表初始化器。std::vector

vector<mixed_data_types> example2 = { { floats { 1.0f, 2.0f } } };
© www.soinside.com 2019 - 2024. All rights reserved.