在c++11中组合一个向量和int
combining a vector and int in c++11?
假设我已经定义并填充了一个称为test
的vector<string>
和一个名为a
的int
。如果我想将这两个函数组合成一个名为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。
相关文章:
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- EASTL矢量<向量<int>>连续的
- 如何写向量的无序向量集,即unordered_set<向量<向量<int>>集合?
- 为什么我不能返回向量<向量<int>>(进程返回 -1073741819 (0xC0000005))
- 如何在没有循环的C++中总结向量 int 的向量
- C++:向量<向量<int>>迭代器?
- C++:在向量<int> vn{MyAllocator<int>(a)} 中调用哪些构造函数?
- 初始化一个类型向量的巨大向量<int>
- adjacent_difference算法,back_inserter向量<向量<int>>
- 如何在函数中将向量<int>v[n] 作为变速器传递?
- 将向量<int>转换为整数
- 如何正确地将向量<int>转换为空隙*并返回向量<int>?
- 有没有一种优雅的方法来读取二进制文件并将其内容复制到向量<int>
- 从<string>向量<int>中定义的索引处的向量中删除字符串
- 如何使用另一个"Eigen::VectorXd"向量中的值初始化"Eigen::VectorXd",基于索引的"向量<int>"
- 有没有办法将向量<double>*指向向量<int>?
- 如何在不复制的情况下更新 std::unordered_map<std::string, std::vector> 中的向量<int>?
- 向量的向量 int 向量
- 在向量中仅制作外部向量<向量<int>>固定
- 如何在 c++ 中将质因数向量<int >减少到映射<int,int>?