Std:: boost::数组列表

std::list of boost::arrays

本文关键字:列表 数组 boost Std      更新时间:2023-10-16

我在使用数组列表时遇到了一些麻烦。因此,为了澄清问题,我知道std::list不能包含数组。我正在使用boost::array来做到这一点。

目前,我对我的寻路算法所需的所有数据类型进行原型化,并测试它们的速度和缓存一致性。

typedef boost::array<int,2> VertexB2i;
typedef std::list<VertexB2i> VertexList;

这些类型不用于寻路,它们比所有真正的c++数组和寻路器标志更容易使用,所以它们只用于生成导航网格。(我也知道在这种情况下我可以使用stl::pair来代替boost::数组,但我想保持生成的数据尽可能与寻路器数据相似,所以我不必一直处理两个完全不同的接口)

VertexList* vl = new VertexList();
vl->push_back({{8,28}}); // this does not work, why?

因此,在为这两种数据类型设置一些测试数据时,我注意到注释行不起作用,尽管这确实起作用:

VertexList* vl = new VertexList();
VertexB2i ver1 = {{8,28}};
vl->push_back({ver1}); // works

总结一下:在不首先宣布"VertexB2i"为独立的情况下,是否有办法推迟"VertexB2i"?一般的建议?

std::array(或boost::array)是聚合类型。在c++ 98/03中,它只能在声明器中初始化:

std::array<int, 2> arr = { 1, 2 };
thelist.push_back(arr); // makes a copy

在c++ 11中,您也可以使用统一初始化来创建临时:

std::list<std::array<int,2>> l;
l.push_back(std::array<int,2>{{2,3}});     // maybe copy
l.emplace_back(std::array<int,2>{{2,3}});  // maybe copy, probably optimized out