SoFunction
Updated on 2025-04-08

Several common methods for initializing arrays in C++ (simple and easy to understand)

In C++, there are several ways to initialize an array, depending onType, size of the array and whether the initial value is given

Here are several common array initialization methods:

1. Initialize a one-dimensional array

1.1. Use list initialization (recommended method)

#include <iostream>
 
int main() {
    // Initialize the array and specify the element    int arr[5] = {1, 2, 3, 4, 5};
 
    // Output array content    for (int i = 0; i < 5; i++) {
        std::cout << arr[i] << " ";
    }
 
    return 0;
}

1.2. Initialize the partial list

#include <iostream>
 
int main() {
    // Only the first two elements are initialized, and the remaining elements are defaulted to 0    int arr[5] = {1, 2};
 
    // Output array content    for (int i = 0; i < 5; i++) {
        std::cout << arr[i] << " ";
    }
 
    return 0;
}

1.3. Initialize the list with std::fill

#include <iostream>
#include <algorithm> // Include fill function 
int main() {
    int arr[5];
    
    // Initialize the array with fill, fill each element to 7    std::fill(std::begin(arr), std::end(arr), 7);
    
    // Output array content    for (int i = 0; i &lt; 5; i++) {
        std::cout &lt;&lt; arr[i] &lt;&lt; " ";
    }
 
    return 0;
}
 
// std::fill{7, 7, 7, 7, 7}

1.4. Use loop to initialize the list

#include &lt;iostream&gt;
 
int main() {
    int arr[5];
 
    // Initialize array using loop    for (int i = 0; i &lt; 5; i++) {
        arr[i] = i * 2;  // Fill as even    }
 
    // Output array content    for (int i = 0; i &lt; 5; i++) {
        std::cout &lt;&lt; arr[i] &lt;&lt; " ";
    }
 
    return 0;
}

2. Initialize a two-dimensional array

2.1. Initialize array using list

#include &lt;iostream&gt;
 
int main() {
    // Initialize a two-dimensional array    int arr[3][2] = {{1, 2}, {3, 4}, {5, 6}};
    
    // Output the contents of the two-dimensional array    for (int i = 0; i &lt; 3; i++) {
        for (int j = 0; j &lt; 2; j++) {
            std::cout &lt;&lt; arr[i][j] &lt;&lt; " ";
        }
        std::cout &lt;&lt; std::endl;
    }
 
    return 0;
}

2.2. Initialize part of the two-dimensional array

#include &lt;iostream&gt;
 
int main() {
    // Only some elements will be initialized, and other elements will be filled with 0    int arr[3][3] = {{1, 2}, {3, 4}};
    
    // Output the contents of the two-dimensional array    for (int i = 0; i &lt; 3; i++) {
        for (int j = 0; j &lt; 3; j++) {
            std::cout &lt;&lt; arr[i][j] &lt;&lt; " ";
        }
        std::cout &lt;&lt; std::endl;
    }
 
    return 0;
}

2.3. Loop initialization of a two-dimensional array

#include &lt;iostream&gt;
 
int main() {
    int arr[3][3];
    
    // Initialize a 2D array using a loop    for (int i = 0; i &lt; 3; i++) {
        for (int j = 0; j &lt; 3; j++) {
            arr[i][j] = i * 3 + j;  // Use index to fill        }
    }
 
    // Output the contents of the two-dimensional array    for (int i = 0; i &lt; 3; i++) {
        for (int j = 0; j &lt; 3; j++) {
            std::cout &lt;&lt; arr[i][j] &lt;&lt; " ";
        }
        std::cout &lt;&lt; std::endl;
    }
 
    return 0;
}

3. Dynamically initialize array with new

#include &lt;iostream&gt;
 
int main() {
    int* arr = new int[5];  // Dynamically allocate arrays of size 5 
    // Initialize the array    for (int i = 0; i &lt; 5; i++) {
        arr[i] = i * 2;  // Fill even numbers    }
 
    // Output dynamic array content    for (int i = 0; i &lt; 5; i++) {
        std::cout &lt;&lt; arr[i] &lt;&lt; " ";
    }
 
    delete[] arr;  // Free dynamically allocated array memory    return 0;
}

4. Initialization method in C++11

More flexible array types are provided in C++11 and above.

4.1. Use std::array to statically initialize arrays

#include &lt;iostream&gt;
#include &lt;array&gt;
 
int main() {
    std::array&lt;int, 5&gt; arr = {1, 2, 3, 4, 5};  // Initialize with std::array 
    // Output std::array content    for (int i = 0; i &lt; 5; i++) {
        std::cout &lt;&lt; arr[i] &lt;&lt; " ";
    }
 
    return 0;
}

4.2. Dynamically initialize arrays using std::vector

#include &lt;iostream&gt;
#include &lt;vector&gt;
 
int main() {
    std::vector&lt;int&gt; vec = {1, 2, 3, 4, 5};  // Initialize with std::vector 
    // Output std::vector content    for (size_t i = 0; i &lt; (); i++) {
        std::cout &lt;&lt; vec[i] &lt;&lt; " ";
    }
 
    return 0;
}

The above are the detailed contents of several common methods (simple and easy to understand) of C++ arrays. For more information about C++ arrays, please pay attention to my other related articles!