相关疑难解决方法(0)

在性能方面使用std :: memcpy()或std :: copy()会更好吗?

memcpy如下所示使用它是否更好,或者std::copy()在性能方面更好用?为什么?

char *bits = NULL;
...

bits = new (std::nothrow) char[((int *) copyMe->bits)[0]];
if (bits == NULL)
{
    cout << "ERROR Not enough memory.\n";
    exit(1);
}

memcpy (bits, copyMe->bits, ((int *) copyMe->bits)[0]);
Run Code Online (Sandbox Code Playgroud)

c++ optimization performance

154
推荐指数
5
解决办法
11万
查看次数

为什么我不能将C风格的数组复制到std :: array?

我有这个代码:

 std::array<int,16> copyarray(int input[16])
{
    std::array<int, 16> result;
    std::copy(std::begin(input), std::end(input), std::begin(result));
    return result;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译此代码时,我收到此错误:

'std::begin': no matching overloaded function found 
Run Code Online (Sandbox Code Playgroud)

和类似的错误std::end.

有什么问题以及如何解决?

c++ arrays stl c++11 stdarray

20
推荐指数
2
解决办法
2489
查看次数

如何将 int[4] 转换为 std::array&lt;int,4&gt;?

我有一个语言内置数组,我需要将它的元素复制到容器库数组中进行一些处理。我尝试了几件事,但似乎不起作用。有没有办法将一种类型转换为另一种类型?

语言内置数组声明为:

int arr[] = {1,12,343,54,99};
Run Code Online (Sandbox Code Playgroud)

虽然库容器数组被声明为:

std::array<int,4> myarray = {4, 26, 80, 14} ;
Run Code Online (Sandbox Code Playgroud)

std::array 在 header 下声明<array>

c++ arrays stl

1
推荐指数
1
解决办法
588
查看次数

标签 统计

c++ ×3

arrays ×2

stl ×2

c++11 ×1

optimization ×1

performance ×1

stdarray ×1