How can I copy a part of an array to another array in C++?

前端 未结 5 1702
执笔经年
执笔经年 2021-02-05 10:07

This is the same question asked in C# but i need for C++

How can I copy a part of an array to another array?

Consider I\'m having

    int[] a = {         


        
5条回答
  •  我寻月下人不归
    2021-02-05 11:02

    There is the C memcpy command, which you can use like this:

    memcpy(destinationArray, sourceArray, sizeof(*destinationArray) * elementsToCopy);
    

    There is also std::copy, which is a more C++ way of doing it:

    std::copy(source, source + elementsInSource, destination);
    

    Note that neither of these functions check to make sure that enough memory has been allocated, so use at your own risk!

提交回复
热议问题