在c++11中组合一个向量和int

combining a vector and int in c++11?

本文关键字:向量 int 合一 c++11 组合      更新时间:2023-10-16

假设我已经定义并填充了一个称为testvector<string>和一个名为aint。如果我想将这两个函数组合成一个名为combined的对象,在这个对象中,我可以用combined[0] = test;初始化/检索向量对象,用combined[1] = a;初始化/检索int对象,那么最好的函数是什么?我该怎么做?我曾试图做vector<vector<string>, int>,但这给了我一个错误。注意:如果这很重要的话,我正在使用-std=c++11进行编译。

使用std::tuple<std::vector<std::string>,int>

#include <tuple>
#include <vector>
#include <string>

int main() {
    std::vector<std::string> test;
    int a{};
    std::tuple<std::vector<std::string>,int> combined;
    //To access elements, use `std::get`:
    std::get<0>(combined) = test;
    std::get<1>(combined) = a;
}

回答cellsheet的评论:该函数已经存在,它被称为std::make_tuple()(另请参阅fjardon关于如何存储该函数的评论)。

顺便说一句,为什么你需要用int来扩展std::vector<std::string>

如果我正确理解您的要求,我认为您可以使用std::pair:

std::pair<std::vector<std::string>, int> combined;
combined.first = test; // assign vector
combined.second = a; // assign int

或简称

auto combined = std::make_pair(test,a);

它需要(丑陋的)类型省略:

#include <iostream>
#include <stdexcept>
#include <type_traits>
#include <vector>
class X {
    public:
    typedef std::vector<std::string> vector_type;
    typedef int integer_type;
    private:
    enum Type {
        TypeVector,
        TypeInteger
    };
    template <bool Constant>
    class Proxy
    {
        private:
        typedef typename std::conditional<
                Constant, const void, void>::type void_t;
        public:
        typedef typename std::conditional<
            Constant, const vector_type, vector_type>::type vector_t;
        typedef typename std::conditional<
            Constant, const integer_type, integer_type>::type integer_t;
        Proxy(vector_t& v)
        :   m_type(TypeVector), m_data(&v)
        {}
        Proxy(integer_t& i)
        :   m_type(TypeInteger), m_data(&i)
        {}
        operator vector_t& () const {
            if(m_type != TypeVector) throw std::runtime_error("Invalid Type");
            return *static_cast<vector_t*>(m_data);
        }
        operator integer_t& () const {
            if(m_type != TypeInteger) throw std::runtime_error("Invalid Type");
            return *static_cast<integer_t*>(m_data);
        }
        private:
        template <typename T, typename U, bool> struct Assignment
        {
            static void apply(void_t*, const U&) {}
        };
        template <typename T, typename U>
        struct Assignment<T, U, true>
        {
            static void apply(void_t* p, const U& value) {
                *static_cast<T*>(p) = value;
            }
        };
        template <typename T, typename U>
        // Attention: Use a reference - std::is_assignable<int, int>::value> is false;
        struct Assign : Assignment<T, U, std::is_assignable<T&, U>::value>
        {};

        public:
        template <typename U>
        Proxy&
        operator = (const U& value) {
            static_assert( ! Constant, "Assignment to Constant");
            switch(m_type) {
                case TypeVector:
                Assign<vector_t, U>::apply(m_data, value);
                break;
                case TypeInteger:
                Assign<integer_t, U>::apply(m_data, value);
                break;
                default: throw std::out_of_range("Invalid Type");
            }
            return *this;
        }
        private:
        Type m_type;
        void_t* m_data;
    };
    public:
    X() : m_v{"Hello"}, m_i(1) {}
    Proxy<true> operator [] (std::size_t i) const {
        switch(i) {
            case 0: return Proxy<true>(m_v);
            case 1: return Proxy<true>(m_i);
            default: throw std::out_of_range("Invalid Index");
        }
    }
    Proxy<false> operator [] (std::size_t i) {
        switch(i) {
            case 0: return Proxy<false>(m_v);
            case 1: return Proxy<false>(m_i);
            default: throw std::out_of_range("Invalid Index");
        }
    }

    private:
    vector_type m_v;
    integer_type m_i;
};
int main() {
    // Note: The Proxy has no operator []
    // const
    {
        const X x;
        const X::vector_type& v = x[0];
        std::cout << v[0] << " " << x[1] << std::endl;
    }
    // non const
    {
        X x;
        X::vector_type& v = x[0];
        v[0] = "World";
        x[1] = 2;
        std::cout << v[0] << " " << x[1] << std::endl;
    }
}

你可以考虑boost::any。