返回向量在C ++中的标准 [英] Returning Vectors standard in C++

查看:142
本文介绍了返回向量在C ++中的标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我知道这是一个常见的问题,但我还没有真正找到一个正确的答案。这真的是一个关于标准的问题。我正在开展一个涉及遗传算法的项目。但是我遇到了一个瓶颈,当它涉及到返回一个向量。有一个正确的方式做到这一点。通常我使用动态分配的数组,并返回一个指向新创建的数组的指针。

Now, I know this is a common question, but I haven't been able to really find a straight answer on this. This is really a question about standards. I am working on a project involving the genetic algorithm. But I'm running into a bottleneck when it comes to returning a vector. Is there a "proper" way to do this. Normally I use dynamically allocated arrays, and return a pointer to a newly created array.

obj* func(obj* foo);

这样,一切都是高效的,没有数据的复制。有一个等价的做一个矢量吗?这个向量中有对象,所以通过值返回它会慢我想象。是通过引用传递结果向量的唯一解决方案?

That way, everything is efficient and there is no copying of data. Is there an equivalent to doing this with a vector? This vector has objects in it, so returning it by value would be slow I imagine. Is the only solution to pass a "resultant" vector by reference?

void func(vector<obj> &input, vector<obj> &result);

另外,为了将来参考,是使用向量或其他STL的标准做法容器在动态分配的数组?动态分配的数组只用作设计容器的低级工具吗?

And, on a side note for future reference, is it standard practice to use a vector or other STL container over a dynamically allocated array? Are dynamically allocated arrays only used as a low level tool for designing containers? Are they just a relic of the past?

推荐答案

vector<obj> func(const vector<obj>& input);

所有编译器默认实现RVO,或者打开优化时,如果不优化对你不关心性能,所以这不会是一个问题。

All compilers implement RVO either by default or when you turn optimizations on, if you don't turn optimizations on you don't care about performance, so that would not be an issue anyway.

如果你的编译器是C ++ 11符合的,那么在最坏的情况下,而不是复制1个指针,当移动语义踢入时,你将支付3个指针,

If your compiler is C++11 conforming, then in the worst case instead of copying 1 pointer you will pay for 3 pointers when move semantics kick in, which compared with the cost of allocating the memory is really no cost.

创建一个简单的有意义的接口,然后测量性能和瓶颈,并从那里进行优化。

Build a simple meaningfull interface, then measure the performance and bottlenecks and optimize from there.

根据您的使用模式,如果您可以重复使用输出容器,您可能需要将上述内容转换为:

Depending on your use pattern, if you can reuse the output container, you might want to transform the above into:

void func(vector<obj>& output, const vector<obj>& input);

在创建一个新向量时,这将具有相同的性能,但是如果你在循环中调用这个函数,您可以避免少量分配:

This will have the same performance when creating a new vector, but if you call this function in a loop, you may be able to avoid a few allocations:

std::vector<obj> output;
for ( ... ) {
   output.clear();
   func(output, input);

// compare with
for ( ... ) {
   std::vector<obj> output = func(input);

在第一个代码块中,如果所有向量的大小相同, c $ c> clear()将删除元素,但保留缓冲区,以便下次调用 func 不需要分配。在第二种情况下,它需要在 func 中分配,并在范围结束时释放。

In the first block of code, if all of the vectors are of the same size, the clear() will remove the elements but leave the buffer intact, so that the next call to func need not allocate. In the second case it needs to allocate inside func and will deallocate at the end of the scope.

一个稍微更丑的界面要使用,所以我会选择第一个(语义更干净),并使用第二只,如果第一个证明有多个分配的问题。

This is a slightly uglier interface to use, so I would opt for the first one (which semantically is cleaner), and use the second only if the first proves to have an issue with multiple allocations.

这篇关于返回向量在C ++中的标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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