推向量的向量

发布于 2024-07-16 11:17:32 字数 218 浏览 3 评论 0原文

推回向量的向量有什么问题吗? 就像

typedef vector<Point> Polygon;
vector<Polygon> polys;
polys.push_back(some_poly);

some_poly 中的所有元素都会被复制,对吗?

我的代码中有一个错误,我似乎无法弄清楚它出了什么问题。

Is there anything wrong with pushing back a vector of vectors? like

typedef vector<Point> Polygon;
vector<Polygon> polys;
polys.push_back(some_poly);

All the elements in some_poly will be copied right?

I have a bug in my code and I can't seem to figure out what's wrong with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤蝉 2024-07-23 11:17:32

是的,向量按照预期被复制。 有一个名为 geordi 的好软件,它可以显示以下内容:

{ 
    using namespace tracked; 
    typedef vector<B> poly_t; 
    poly_t poly(3); // contains 3 B's
    vector<poly_t> v; 
    v.push_back(poly); 
}

它跟踪创建/ tracked::B 的副本。 这是输出:

B0* B1*(B0) B2*(B0) B3*(B0) B0~ B4*(B1) B5*(B2) B6*(B3) B4~ B5~ B6~ B1~ B2~ B3~

这是我们仅跟踪 v.push_back 时的输出:

B4*(B1) B5*(B2) B6*(B3)

如您所见,第一个 B0 被创建为向量构造函数的默认参数。 然后该对象被复制到 3 个 B 中,之后当构造函数返回时,B0 再次被销毁。 然后创建poly。 然后,我们将其推回到多边形向量中。 参数 poly 被复制到一个新向量中,该向量在多边形向量内创建并由该向量管理。

如果它崩溃了,问题可能出在程序的其他部分。 检查复制构造函数/构造函数和析构函数是否正常工作,并且如果使用动态内存分配,它们不会两次删除内容。

Right, the vector is copied like expected. There is a good software called geordi which can show this:

{ 
    using namespace tracked; 
    typedef vector<B> poly_t; 
    poly_t poly(3); // contains 3 B's
    vector<poly_t> v; 
    v.push_back(poly); 
}

It tracks creation/copies of tracked::B. Here is the output:

B0* B1*(B0) B2*(B0) B3*(B0) B0~ B4*(B1) B5*(B2) B6*(B3) B4~ B5~ B6~ B1~ B2~ B3~

This is output when we only track v.push_back:

B4*(B1) B5*(B2) B6*(B3)

As you see, first B0 is created as the default argument of the vector constructor. Then that object is copied into 3 B's and after that B0 is destroyed again as the constructor comes back. poly then is created. Then, we push_back it into a vector of polygons. The argument, poly, is copied into a new vector which is created within the vector of polygons and managed by that.

If it crashes, the problem probably lies within another part of your program. Checks that the copy constructor/constructor and destructor work correctly and that they don't delete things twice if you use dynamic memory allocation.

做个少女永远怀春 2024-07-23 11:17:32

是的,只要您为 Point 类定义了复制构造函数和赋值运算符(并确保它们做正确的事情等),那应该可以正常工作。 std::vector 会很好地推动,因此错误一定在其他地方 - 显然我们需要更多详细信息来进一步提供帮助。

如果您要推送一个向量,则会对性能产生影响,但在它正常工作之前不要担心(只有当它成为问题时)。

Yes, that should work fine, as long as you have defined a copy constructor and assignment operator for your Point class (and ensured they're doing the right thing etc). std::vector will push just fine, so the bug must be elsewhere - obviously we'd need more details to help further.

There are performance implications if you're going to push a vector of things, but don't worry about that until it's working (and then only if it becomes a problem).

潇烟暮雨 2024-07-23 11:17:32

没有任何问题。 它不是最有效的插入/擦除结构,但它绝对应该有效。

如果您有兴趣使插入/删除更有效,您可能应该切换到:

typedef vector<Point> Polygon;
typedef vector<Polygon*> Polygons;
Polygons polys;
polys.push_back(new Polygon());

Nothing at all wrong with it. It's not the most efficient structure for inserts/erasures, but it should definitely work.

If you are interested in making inserts/erasures more efficient, you should probably switch to:

typedef vector<Point> Polygon;
typedef vector<Polygon*> Polygons;
Polygons polys;
polys.push_back(new Polygon());
怕倦 2024-07-23 11:17:32

虽然向量的向量没有任何问题,但您可能想看看 Boost.MultiArray。 它可以更有效率。

 typedef boost::multi_array<Point, 2> Polygons;

另一个想法是,您可能希望使 Polygon 成为一个真正的类(可能包含向量),以提供比 std::vector 默认提供的方法更适合的方法。

Although there is nothing wrong with a vector of vectors, you might want to have a look at Boost.MultiArray. It can be more efficient.

 typedef boost::multi_array<Point, 2> Polygons;

Another idea is that you may want to make Polygon a real class (probably containing a vector) to provide more adapted methods than the ones provided by default with std::vector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文