[C/C++] Assign an array to another array

Loop traversal assignment: Use a loop structure (such as a for loop) to assign and copy elements one by one. Here is the sample code:

#include <iostream>

const int SIZE = 5;  // 数组的大小

int main() {
    
    
    int arr1[SIZE] = {
    
    1, 2, 3, 4, 5};  // 原始数组
    int arr2[SIZE];  // 目标数组

    // 将arr1的值赋给arr2
    for (int i = 0; i < SIZE; ++i) {
    
    
        arr2[i] = arr1[i];
    }

    // 打印arr2的值
    for (int i = 0; i < SIZE; ++i) {
    
    
        std::cout << arr2[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

In the above code, two arrays arr1and are defined arr2, and arr1the values ​​of are assigned to one by one using the loop structure arr2. Then, we use another loop to print arr2the value of .

Running the above code, the output will be:

1 2 3 4 5

This indicates arr2that successfully received the same value arr1from .

Use the functions std::copy or std::memcpy from the standard library

In C++, =the address of one array can be assigned to another array by directly using the assignment operator. However, the contents of the array are not copied. This will cause both arrays to share the same memory space, and modifications to one array will also affect the other.

If you want to realize the overall assignment of the contents of the array, you can use the function in the standard library std::copyor std::memcpy, at this time, there are two arrays (that is, the addresses are different). Here is the sample code:

#include <iostream>
#include <algorithm>
#include <cstring>

const int SIZE = 5;  // 数组的大小

int main() {
    
    
    int arr1[SIZE] = {
    
    1, 2, 3, 4, 5};  // 原始数组
    int arr2[SIZE];  // 目标数组

    // 使用 std::copy 进行整体赋值
    std::copy(arr1, arr1 + SIZE, arr2);

    // 打印arr2的值
    for (int i = 0; i < SIZE; ++i) {
    
    
        std::cout << arr2[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

In the code above, the contents of std::copyare copied into the using the function . In this way, and will have independent memory spaces, and modifications to one array will not affect the other.arr1arr2arr1arr2

Running the above code, the output will be:

1 2 3 4 5

This shows that arr2successfully received the same value arr1from and that the two arrays are independent.

Use standard library containers: If you use standard library containers (such as std::vector, std::array, etc.) in C++, you can directly use the assignment operator or copy constructor of the container to copy between arrays.

#include <vector>
std::vector<int> source = {
    
    1, 2, 3, 4, 5};
std::vector<int> destination = source;

This approach works well when using standard library containers, which provide convenient copy operations.

Whichever method you choose, make sure the size of the destination array is large enough to hold the elements of the source array, and take care to avoid array out-of-bounds accesses.

Guess you like

Origin blog.csdn.net/XiugongHao/article/details/130734130