std :: vector和c-style数组 [英] std::vector and c-style arrays

查看:222
本文介绍了std :: vector和c-style数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 OpenCL 来提高我们软件的速度。我们使用地图很多,为了简化,将地图表示为std :: vector< std :: vector>。 OpenCL API使用原始c风格指针作为参数,例如上面情况下的int *。



我的问题:




  • 在stl中是否有实现保证,向量在内存中是连续的?

  • 我可以安全地将std :: vector转换为int *并期望工作?

  • 在向量的矢量的情况下,我仍然可以认为这是真的吗?我会期望矢量持有其他状态数据,或对齐问题,或者可能是别的东西...

  • 最好的方法是什么?编写一个自定义2d数据结构,它保存一个内部连续的内存缓冲区并使用它?我必须向/从向量复制很多...



谢谢。


在stl中是否有实现保证,向量在内部是连续的?




从C ++ 03开始,是的,向量被保证使用连续的存储。
(在C ++ 98中,有一个意外的漏洞,所以一个实现可以假设使用非连续存储,但是它在2003年的标准修订版本中是固定的,没有实际使用 >非连续存储,因为这是一个可怕的想法)


我可以安全地将std :: vector转换为int *


通常的方式是& v [0] 。 (& * v.begin()也可能工作,但我似乎记得在标准中有一些蓬松的措辞,使这不是100%可靠)



否。你为什么会期望工作?向量是一个类。它不是指针。


在向量的向量的情况下,我仍然可以假设这个保持真正?我希望向量可以保存其他状态数据或对齐问题,或者可能是其他的东西...


向量的行为相同无论你存储在它。如果你做一个向量向量,你最终得到一个对象,它包含一个指向堆分配的数组的指针,其中每个元素都是一个对象,它包含一个指向堆分配的数组的指针。



至于你应该如何应对,这取决于很多因素。您的总数据集有多大?您可能希望连续分配整个表。使用向量向量,每行都是单独的分配。


I am experimenting with OpenCL to increase the speed of our software. We work with maps a lot and, to simplify, represent a map as a std::vector< std::vector >. The OpenCL API takes raw c-style pointers as arguments, for example int* in the case above.

My questions:

  • Are there implementation guarantees in the stl that vector is, internally, consecutive in memory?
  • Can I safely cast a std::vector to int* and expect that to work?
  • In the case of a vector of vectors, can I still assume this holds true? I would expect the vector to hold other state data, or alignment issues, or maybe something else...
  • What is the best way to approach this? Write a custom 2d data structure that holds an internal, contiguous-in-memory buffer and work with that? I'd have to copy to/from vectors a lot...

Thanks.

解决方案

Are there implementation guarantees in the stl that vector is, internally, consecutive in memory?

As of C++03, yes, a vector is guaranteed to use contiguous storage. (In C++98, there was an accidental loophole so an implementation could hypothetically use non-contiguous storage, but it was fixed in the 2003 revision of the standard - and no implementation actually used non-contiguous storage because it'd be a terrible idea)

Can I safely cast a std::vector to int* and expect that to work?

The usual way is &v[0]. (&*v.begin() would probably work too, but I seem to recall there's some fluffy wording in the standard that makes this not 100% reliable)

No. Why would you expect that to work? A vector is a class. It is not a pointer. It just contains a pointer.

In the case of a vector of vectors, can I still assume this holds true? I would expect the vector to hold other state data, or alignment issues, or maybe something else...

The vector behaves the same whatever you store in it. If you make a vector of vectors, you end up with an object which contains a pointer to a heap-allocated array, where each element is an object which contains a pointer to a heap-allocated array.

As for how you should approach this, it depends on a lot of factors. How big is your total dataset? You might want to have the entire table allocated contiguously. With a vector of vectors, each row is a separate allocation.

这篇关于std :: vector和c-style数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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