In what cases should I use memcpy over standard operators in C++?

前端 未结 7 1652
梦毁少年i
梦毁少年i 2020-12-13 06:36

When can I get better performance using memcpy or how do I benefit from using it? For example:

float a[3]; float b[3];

is cod

7条回答
  •  萌比男神i
    2020-12-13 06:58

    You can use memcpy only if the objects you're copying have no explicit constructors, so as their members (so-called POD, "Plain Old Data"). So it is OK to call memcpy for float, but it is wrong for, e.g., std::string.

    But part of the work has already been done for you: std::copy from is specialized for built-in types (and possibly for every other POD-type - depends on STL implementation). So writing std::copy(a, a + 3, b) is as fast (after compiler optimization) as memcpy, but is less error-prone.

提交回复
热议问题