简体   繁体   English

有没有可能传递std :: vector <int> 作为获取std :: vector的乐趣的参数 <std::array<int, 3> &gt;?

[英]Is it possible without copy to pass std::vector<int> as param to fun that get std::vector<std::array<int, 3>>?

I have such a method 我有这样的方法

void foo(std::vector<std::array<int, 3>> iVec){
....
}

I don't want to change this method, but I need to pass as param this vector std::vector<int> 我不想更改此方法,但需要将此向量std::vector<int>作为参数传递

As far as I understand this two vectors are taking the same memory... So, theoretical I can use method std::move to move this vector as is. 据我了解,这两个向量占用相同的内存...因此,理论上讲,我可以使用std::move方法移动该向量。 But this vectors has a different size.. so... I don't know... 但是这个向量的大小是不同的..所以...我不知道...

One guy told me that it could be possible if change the function that it will get two int pointers, and so it could be possible. 一个人告诉我,如果更改函数将有可能获得两个int指针,这是有可能的,因此这是可能的。

But I don't understand what does it mean without a example... If someone could provide an example I really appreciate. 但是我不明白没有一个例子意味着什么。如果有人可以提供一个例子,我真的很感激。

But anyway question still is: if is it possible to pass my vector as a function param that has another type. 但是无论如何,问题仍然是:是否有可能将我的向量作为具有另一种类型的函数参数传递。 If yes, so how? 如果是,那怎么办?

Fell free to ask 随意问

The C++ standard does not guarantee that sizeof(std::array<MyType, N>)==sizeof(MyType)*N . C ++标准不保证sizeof(std::array<MyType, N>)==sizeof(MyType)*N That is, even though the elements of an individual std::array are contiguous, a vector of array s may not have the elements of the arrays evenly spaced. 也就是说,即使单个std::array的元素是连续的, array s的vector也可能不会使数组的元素均匀间隔。 So there is no standard-compliant way of doing this, because the two vectors would not necessarily be "taking the same memory". 所以有这样做,因为这两个向量不一定会“采取同样的记忆”的不符合标准的方式。

You could maybe get things to work with reinterpret_cast and such on your compiler. 您也许可以在编译器上使用reinterpret_cast类的东西。 ( std::move from a reinterpreted vector type will definitely not work, not in theory and not in practice.) This will not be portable or reliable or a good idea. std::move从重新诠释矢量型肯定不行的,没有在理论上和在实践中没有)。这会不会是便携或可靠性或一个好主意。 You have three options: copy the data, change the source type, or change the destination type. 您有三个选项:复制数据,更改源类型或更改目标类型。

I don't want to change this method ... 我不想更改此方法...

But you should. 但是你应该。 If you make it work with iterators rather than the container, you will get more flexibility: 如果使其与迭代器而不是容器一起使用,您将获得更大的灵活性:

template <typename IT>
void foo(IT begin, IT end){
    ....
}

Now passing begin and end of whatever container is possible. 现在传递任何可能的容器的beginend However, if you want to go back to passing iterators to a std::vector<std::array<int, 3>> you would have to invest some extra effort to get an iterator type that lets you iterate from [0][0] till [size_x][size_y] , but as the arrays all have size 3, this shouldnt be too complicated. 但是,如果您想回到将迭代器传递给std::vector<std::array<int, 3>>方法,则必须付出额外的努力才能获得一个可以从[0][0]进行迭代的迭代器类型[0][0][size_x][size_y] [0][0]为止,但是由于数组的大小均为3,因此不要太复杂。 And anyhow you should consider to use flat vectors instead of a 2D structure. 无论如何,您都应该考虑使用平面向量而不是2D结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM