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 < 5; i++) { std::cout << arr[i] << " "; } return 0; } // std::fill{7, 7, 7, 7, 7}
1.4. Use loop to initialize the list
#include <iostream> int main() { int arr[5]; // Initialize array using loop for (int i = 0; i < 5; i++) { arr[i] = i * 2; // Fill as even } // Output array content for (int i = 0; i < 5; i++) { std::cout << arr[i] << " "; } return 0; }
2. Initialize a two-dimensional array
2.1. Initialize array using list
#include <iostream> 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 < 3; i++) { for (int j = 0; j < 2; j++) { std::cout << arr[i][j] << " "; } std::cout << std::endl; } return 0; }
2.2. Initialize part of the two-dimensional array
#include <iostream> 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 < 3; i++) { for (int j = 0; j < 3; j++) { std::cout << arr[i][j] << " "; } std::cout << std::endl; } return 0; }
2.3. Loop initialization of a two-dimensional array
#include <iostream> int main() { int arr[3][3]; // Initialize a 2D array using a loop for (int i = 0; i < 3; i++) { for (int j = 0; j < 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 < 3; i++) { for (int j = 0; j < 3; j++) { std::cout << arr[i][j] << " "; } std::cout << std::endl; } return 0; }
3. Dynamically initialize array with new
#include <iostream> int main() { int* arr = new int[5]; // Dynamically allocate arrays of size 5 // Initialize the array for (int i = 0; i < 5; i++) { arr[i] = i * 2; // Fill even numbers } // Output dynamic array content for (int i = 0; i < 5; i++) { std::cout << arr[i] << " "; } 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 <iostream> #include <array> int main() { std::array<int, 5> arr = {1, 2, 3, 4, 5}; // Initialize with std::array // Output std::array content for (int i = 0; i < 5; i++) { std::cout << arr[i] << " "; } return 0; }
4.2. Dynamically initialize arrays using std::vector
#include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; // Initialize with std::vector // Output std::vector content for (size_t i = 0; i < (); i++) { std::cout << vec[i] << " "; } 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!