规范方式复制数组 [英] Canonical way to copy an array
问题描述
用C ++复制数组的规范方法是什么?
如果我们要复制POD,我们可以使用memcpy(但可能会有更多<如果我们知道块已经适当对齐,那么
有效的替代方案。
无论数组是否包含POD,我们都可以使用循环
as:
#include< cassert>
模板< class T>
void CopyArray(T * pdest,T const * psource,T const * const psover)
{
assert(pdest);
断言(psource);
断言(psover); / *逆转在这里! * /
断言(psover psource);
do
{
* pdest ++ = * psource ++;
} while(psource!= psover);
}
#define ARRLEN(arr)(sizeof(arr) )/ sizeof *(arr))
#define POVER(arr)((arr)+ ARRLEN((arr)))
int main()
{
int array1 [5] = {1,2,3,4,5};
int array2 [5];
CopyArray(array2,array1,POVER(array1));
}
但是,我认为这有一个标准库功能吗?
什么是最有效的方法?
另外,如何你去复制一个数组吗?在我看来,
你必须使用aligned_storage,然后做一个使用
" placement new"的循环。我想知道为什么以下是不允许的:
int main()
{
int arr1 [] = {1,2 ,3,4,5};
int arr2 [] = arr1;
}
-
Frederick Gotham
Frederick Gotham写道:
用C ++复制数组的规范方法是什么?
std :: copy。
Frederick Gotham schrieb:
在C ++中复制数组的规范方法是什么?
[复制元素的循环]
但是,我认为有一个标准库函数这样做?
std :: copy
什么是最有效的方法?
不是由标准定义的。
您可以使用std :: copy并且在测量时,某种方式更快对于
特定类型,您可以使用更快的
方法重载/专门化std :: copy。
-
Thomas
" Frederick Gotham" < fg ******* @ SPAM.com写了留言
新闻:26 ******************* @ news.indigo .ie ...
用C ++复制数组的规范方法是什么?
如果我们'在复制POD时,我们可以使用memcpy(但如果我们知道这些块是适当对齐的话,那么可能会有更多的有效替代方案)。
我对此表示怀疑。 memcpy()通常知道关于
内存地址属性的各种肮脏细节;如果地址适当对齐,
memcpy()通常会使用效率更高的实现。
Philip
What''s the canonical way to copy an array in C++?
If we''re copying a POD, we can use memcpy (but there could be a more
efficient alternative if we know that the blocks are suitably aligned).
Regardless of whether the array consists of POD''s, we could use a loop such
as:
#include <cassert>
template<class T>
void CopyArray(T *pdest, T const *psource, T const *const psover)
{
assert(pdest);
assert(psource);
assert(psover); /* Contraversey here! */
assert(psover psource);
do
{
*pdest++ = *psource++;
} while(psource != psover);
}
#define ARRLEN(arr) (sizeof(arr)/sizeof*(arr))
#define POVER(arr) ((arr) + ARRLEN((arr)))
int main()
{
int array1[5] = {1,2,3,4,5};
int array2[5];
CopyArray(array2,array1,POVER(array1));
}
However, I presume that there''s a Standard Library function for doing this?
What''s the most efficient method?
Also, how do you go about copy-constructing an array? Seems to me that
you''d have to use "aligned_storage", and then do a loop which uses
"placement new". I wonder why the following is disallowed:
int main()
{
int arr1[] = {1,2,3,4,5};
int arr2[] = arr1;
}
--
Frederick Gotham
Frederick Gotham wrote:
What''s the canonical way to copy an array in C++?
std::copy.
Frederick Gotham schrieb:What''s the canonical way to copy an array in C++?[loop that copies elements]
However, I presume that there''s a Standard Library function for doing this?std::copy
What''s the most efficient method?Not defined by the standard.
You could use std::copy and when measured, that some way is faster for a
specific type, you could overload/specialize std::copy using the "faster"
method.
--
Thomas
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:26*******************@news.indigo.ie...What''s the canonical way to copy an array in C++?
If we''re copying a POD, we can use memcpy (but there could be a more
efficient alternative if we know that the blocks are suitably aligned).I doubt it. memcpy() normally knows all sorts of grubby details about
properties of memory addresses; if the addresses are suitably aligned,
memcpy() will normally use the more efficient implementation anyway.
Philip
这篇关于规范方式复制数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!