0

假设我有一个vector<string>已经定义和填充的被调用test和一个int被调用a。如果我想将这两个组合成一个单独的对象combined,我可以在其中combined[0] = test;使用向量combined[1] = a;初始化/检索对象并使用 int 初始化/检索对象,这样做的最佳功能是什么,我该怎么做所以?我曾试图这样做vector<vector<string>, int>,但这给了我一个错误。注意:如果这很重要,我将使用 -std=c++11 进行编译。

4

3 回答 3

5

使用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 关于如何存储它的评论)。

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

于 2013-09-03T02:23:36.300 回答
4

如果我正确理解您的要求,我认为您可以使用以下命令执行此操作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);
于 2013-09-03T02:22:28.433 回答
0

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

#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。

于 2013-09-03T07:26:24.090 回答