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

前端 未结 5 1620
执笔经年
执笔经年 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 10:45

    Yes, using st standard library algorithm copy:

    #include 
    
    int main()
    {
      int array[5] = { 1,2,3,4,5 }; // note: no int[] array
      int *b = new int[3];
      std::copy(array+1, array+4, b); 
      // copies elements 1 (inclusive) to 4 (exclusive), ie. values 2,3,4
    }
    

提交回复
热议问题