大家,
我是一名 Python/C# 人员,我正在尝试学习 C++。
在 Python 中我曾经做过这样的事情:
myRoutes = {0:[1,2,3], 1:[[1,2],[3,4]], 2:[[1,2,3],[[1,2,3] ],[1,2,3]],[4]]}
基本上,当您有可变长度的数组并且您不想为它们浪费 2D 矩阵时,请将数组嵌套到字典中以进行跟踪其中是一个不错的选择。
在 C++ 中,我尝试了 std::map; > >
它有效,但我觉得必须有更好的方法来做到这一点。
我更喜欢坚持使用标准库,但像 boost 这样的流行库对我来说也是可以接受的。
感谢您的帮助,
阿里
Everybody,
I am a Python/C# guy and I am trying to learn C++.
In Python I used to do things like:
myRoutes = {0:[1,2,3], 1:[[1,2],[3,4]], 2:[[1,2,3],[[1,2,3],[1,2,3]],[4]]}
Basically when you have arrays of variable length and you don't want to wast a 2D matrix for them, nesting arrays into a dictionary to keep track of them is a good option.
In C++ I tried std::map<int, std:map<int, std:map<int, int> > >
and it works but I feel there got to bo a better way to do this.
I prefer to stick to the standard libraries but popular libraries like boost are also acceptable for me.
I appreciate your help,
Ali
发布评论
评论(2)
看起来问题的一部分是:“如何在容器中存储异构数据?”有几种不同的方法:
1) 使用现成的数组类型类来抽象出确切的细节(即维数)。示例: Boost 基本线性代数
< strong>2) 使用
Boost.variant
构建
variant
类型的访问者有点复杂(它涉及编写boost::static_visitor 的子类)
对于变体中的每种类型都有一个operator()
重载)。从好的方面来说,访问者会进行静态类型检查,以确保他们实现了正确的处理程序集。3) 使用包装类型,例如
Boost.Any
来包装不同类型的兴趣。总的来说,我认为第一个选项(使用专门的数组类)可能是最可靠的。我在最近的代码中也大量使用了
variants
,虽然编译错误很长,但一旦习惯了这些,进行编译时检查就很好了,与Python的“运行和运行”相比,我更喜欢这一点。后来发现你错了”范例。It looks like part of the question is: 'How do I store heterogeneous data in a container?' There are several different approaches:
1) Use a ready-made class of array types that abstracts away the exact details (i.e., dimensionality). Example: Boost Basic Linear Algebra
2) Make an explicit list of element types, using
Boost.variant
Building a visitor for
variant
type is a bit involved (it involves writing a subclass ofboost::static_visitor<desired_return_type>
with a singleoperator()
overload for each type in your variant). On the plus side, visitors are statically type checked to ens ure that they implement the exact right set of handlers.3) Use a wrapper type, like
Boost.Any
to wrap the different types of interest.Overall, I think the first option (using a specialized array class) is probably the most reliable. I have also used
variants
heavily in recent code, and although the compile errors are long, once one gets used to those, it is great to have compile time checking, which I prefer strongly over Python's "run and find out you were wrong later" paradigm.我们中的许多人都和您一样正在经历痛苦,但有解决方案。这些解决方案之一是 Boost 库(它就像 C++ 的第二个标准库)。有相当多的集合库。在你的情况下,我会使用 Boost::Multi-Dimentional数组。
看起来像这样:
创建了一个 2x2x2 数组。模板参数中的第一个类型“double”指定数组将保存的类型,第二个“3”指定数组的维数。然后,您可以使用“范围”来传递每个维度的实际大小。非常易于使用,并且其意图在语法上很清晰。
现在,如果您正在处理 Python 中的某些内容,例如
foo = {0:[1,2,3], 1:[3,4,5]}
您真正需要的是多重地图。这是标准库的一部分,本质上是一个红黑树,按键索引,但有一个值列表。Many of us share the pain you're experiencing right now but there are solutions to them. One of those solutions is the Boost library (it's like the 2nd standard library of C++.) There's quite a few collection libraries. In your case I'd use Boost::Multi-Dimentional Arrays.
Looks like this:
Which creates a 2x2x2 array. The first type in the template parameters "double" specifies what type the array will hold and the second "3" the dimension count of the array. You then use the "extents" to impart the actual size of each of the dimensions. Quite easy to use and syntatically clear in its intent.
Now, if you're dealing with something in Python like
foo = {0:[1,2,3], 1:[3,4,5]}
what you're really looking for is a multimap. This is part of the standard library and is essentially a Red-Black tree, indexed by key but with a List for the value.