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

前端 未结 5 1699
执笔经年
执笔经年 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 10:49

    Assuming you want a dynamically-allocated array as in the C# example, the simplest way is:

    std::vector b(a.begin() + 1, a.begin() + 4);
    

    This also has the advantage that it will automatically release the allocated memory when it's destroyed; if you use new yourself, then you'll also need to use delete to avoid memory leaks.

提交回复
热议问题