C ++中的多维容器 [英] Multidimensional Containers in C++

查看:278
本文介绍了C ++中的多维容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何库提供多维容器来使用像向量<>?



我想看到像:

  TwoD< object_class_name> D2; 
ThreeD< object_class_name> D3;

且object_class_name可以是任何对象而不是内置类型。



,所以我可以使用

  D2 [i] [j] 
D3或者类似的

$

D3 [i,j,k] p>

感谢。

解决方案

如果是c ++ 11, 使用 ,允许将 template

  template< typename T& 
using TwoD = std :: vector< std :: vector< T>>

template< typename T>
using ThreeD = std :: vector< std :: vector< std :: vector< T>>>

用法:

 code> TwoD< int> t2ints; 
TwoD< std :: string> t2strings;

ThreeD< int> t3ints;
ThreeD< std :: string> t3strings;


Is there any library provide a multidimensional container to use like vector<>?

I would like to see something like:

TwoD<object_class_name> D2;
ThreeD<object_class_name> D3;

and the object_class_name could be any object instead of only the builtin types.

so I can use the object like

D2[i][j]
D3[i,j,k] or D3(i,j,k)

or similar

Thanks.

解决方案

If c++11, a possible solution is using which allows aliasing of a template:

template <typename T>
using TwoD = std::vector<std::vector<T>>;

template <typename T>
using ThreeD = std::vector<std::vector<std::vector<T>>>;

usage:

TwoD<int> t2ints;
TwoD<std::string> t2strings;

ThreeD<int> t3ints;
ThreeD<std::string> t3strings;

这篇关于C ++中的多维容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆