在 C++11 中复制常量大小数组的最简洁方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12328301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:09:52  来源:igfitidea点击:

Cleanest way to copy a constant size array in c++11

c++arraysoptimizationc++11copy

提问by bobbaluba

I often find myself wanting to copy the contents of arrays that have a constant size, I usually just write something along the lines of:

我经常发现自己想要复制具有恒定大小的数组的内容,我通常只是按照以下方式编写一些内容:

float a[4] = {0,1,2,3};
float b[4];

for(int i=0; i<4; i++){
    b[i]=a[i];
}

As of lately, I am writing a linear calculus library for educational purposes, and I was wondering if there was a better way to do it.

最近,我正在编写一个用于教育目的的线性微积分库,我想知道是否有更好的方法来做到这一点。

The first thing that came to my mind, was using memcpy:

我想到的第一件事是使用 memcpy:

memcpy(b, a, sizeof(float) * 4);

But this seems very c-like and error prone to me. I like having my errors at compile time, and this can get ugly for data types with non-trivial copy constructors, or if I forget to multiply with sizeof(datatype).

但这对我来说似乎非常像 C 语言并且容易出错。我喜欢在编译时出现错误,对于具有非平凡复制构造函数的数据类型,或者如果我忘记与 sizeof(datatype) 相乘,这可能会变得丑陋。

Since I am writing a math library that I am going to use intensively, performance is very important to me. Are the compilers today smart enough to understand that the first example is just copying a chunk of memory and optimize it to be as efficient as the second solution?

由于我正在编写一个我将大量使用的数学库,因此性能对我来说非常重要。今天的编译器是否足够聪明,可以理解第一个示例只是复制一块内存并将其优化为与第二个解决方案一样有效?

Perhaps there is a function in the standard library that can help me? Something new in c++11? Or should I just create a macro or a template function?

也许标准库中有一个函数可以帮助我?C++11 中的新东西?或者我应该只创建一个宏或模板函数?

回答by Benjamin Lindley

If you use std::arrayinstead of a built-in array (which you should), it becomes very simple. Copying an array is then the same as copying any other object.

如果您使用std::array而不是内置数组(您应该使用),它会变得非常简单。复制数组与复制任何其他对象相同。

std::array<float,4> a = {0,1,2,3};
std::array<float,4> b = a;

回答by Mysticial

The C++03 way would be to use std::copy():

C++03 的方式是使用std::copy()

float a[4] = {0,1,2,3};
float b[4];

std::copy(a,a + 4, b);

That's about as clean as it gets. On C++11 prefer

这几乎是干净的。在 C++11 上更喜欢

std::copy(std::begin(a), std::end(a), std::begin(b));

Or better yet, use std::array and get assignment for free:

或者更好的是,使用 std::array 并免费获得分配:

std::array<float,4> a = {0,1,2,3};
auto b = a;

回答by PiotrNycz

For interested in C++03 (and also C) solution - always use struct containing an array instead of solely array:

对于对 C++03(以及 C)解决方案感兴趣的人 - 始终使用包含数组的结构而不是单独的数组:

struct s { float arr[5]; };

Structs are copyable by default.

默认情况下,结构是可复制的。

Its equivalent in C++11 is,already mentioned, std::array<float,5>;

它在 C++11 中的等价物是,已经提到, std::array<float,5>;

回答by Shital Shah

Below method works for usual arrays as well std:array.

下面的方法适用于通常的数组以及 std:array。

float a[4] = {0,1,2,3};
float b[4];

std::copy(std::begin(a), std::end(a), b);

回答by hjbabs

#include <algorithm>
std::copy_n(a, 4, b)