C + + array

C++ supports array data structures, which can store a fixed size sequential collection of elements of the same type. An array is used to store a series of data, but it is often thought of as a series of variables of the same type.

Array declarations do not declare individual variables, such as number0, number1,… Instead, declare an array variable, such as numbers, and use numbers[0], numbers[1]… And numbers[99] to represent individual variables. Specific elements in an array can be accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address corresponds to the last element.

Declare an array

To declare an array in C++, we specify the type and number of elements, as follows:

type arrayName [ arraySize ];

This is called a one-dimensional array. ArraySize must be an integer constant greater than zero, and type can be any valid C++ data type. For example, to declare balance, an array of 10 elements of type double, we declare it as follows:

double balance[10];

Balance is now an available array that can hold up to 10 numbers of type double.

Initializing an array

In C++, you can initialize arrays one by one, or you can use an initialization statement like this:

Double balance [5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between curly braces {} cannot be greater than the number of elements we specified in square brackets [] when we declared the array.

If you omit the size of the array, the size of the array is the number of elements at initialization. Therefore, if:

Double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

You will create an array that is exactly the same as the one created in the previous example. Here is an example of assigning an element in an array:

The balance [4] = 50.0;

The above statement assigns 50.0 to the fifth element of the array. All arrays have 0 as their first element index, also known as the base index, and the last index of an array is the total size of the array minus 1. Here is a graphical representation of the array discussed above:

Accessing an array element

Array elements can be accessed by the array name indexed. The index of the element is placed in square brackets, following the array name. Such as:

double salary = balance[9];

The above statement will assign the value of the 10th element in the array to the salary variable. The following example uses the above three concepts, namely, declaring an array, assigning an array, and accessing an array:

#include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ]; // initialize array elements for (int I = 0; i < 10; i++ ) { n[ i ] = i + 100; // set Element I to I + 100} cout << "Element" << setw(13) << "Value" << endl; For (int j = 0; j < 10; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0; }Copy the code

The above program uses the setw() function to format the output. When the above code is compiled and executed, it produces the following results:

Element        Value
      0          100
      1          101
      2          102
      3          103
      4          104
      5          105
      6          106
      7          107
      8          108
      9          109
Copy the code

C++ array details

Arrays are very important in C++, and we need to learn more about them. Here are some important array-related concepts that C++ programmers should be aware of:

In learning, we often encounter some problems, especially the knowledge don’t understand, my advice is to write down, jump in the past, because the connection between the knowledge of programming is very tight, but also is loose, a question that the road is blocked, we can change train of thought to solve, there is something that the present is not clear, waiting for you to fix up the back of the knowledge, It’s very common to look back and think “that’s the way it is”, so don’t settle for one place.

Finally, when you encounter problems, it is also very important to ask big gods. It is suggested to chat with this group and discuss with seniors. You will also get a lot of help. Can also exchange learning experience, technical problems, you can get PDF books source code, tutorials and so on for free use.