C++ study notes fifteen-array

What is an array?
An array is a collection of the same type, used to store and process a large number of data structures of the same type.
How to use arrays?
Before using an array, we must first declare the type. We should first determine the following questions:
1: The name of the
array 2: The type of the
array 3: The size of the
array The declaration format of the array is a
data type identifier [constant expression 1][ Constant expression 2]
The data types in the array include integer, floating point, etc. You can even define data types for words, such as classes.
The constant expression is the boundary of the array. It should be noted that the array starts from the subscript 0 and ends at N-1, for example, int a[10], the range included is a[0]~a[9]. 10 elements, a[2][3] represents a two-dimensional array, which can be understood as an array with 2 rows and 3 columns.
We usually use arrays to use subscripts, such as the declaration

int a[10]

Then you can call the elements in a[1], a[3]..., it should be noted that the subscript can not be out of range.
The following is an example of the array, we customize the data type point, and then quote, the example is as follows

# include <iostream>
using namespace std;
//定义数据类型point
class point
{
    
    
private:
	int x;
	int y;

public:
	point(int xx,int yy);
	void show();//输出函数接口,输出私有数据成员的值
};
point::point(int xx=0, int yy=0) :x(xx), y(yy) 
{
    
    }
void point::show()
{
    
    
	cout << "x is" << x << "\n" << "y is" << y << endl;

}
int main()
{
    
    
	//定义point数组,一共10个元素
	point a[10] = {
    
     {
    
    1,2},{
    
    2,3} };
	a[0].show();//输出第一个元素
	a[1].show();//输出第二个元素
	a[2].show();//输出第三个元素
	
}

The output result is as follows.
Insert picture description here
How to initialize the array?
One-dimensional array initialization

int a[10]={
    
    1,2,3};

The default complement not written later is 0, a[0]=1,a[1]=2,a[2]=3,a[4]=0,a[5]=0,a[6]=0 ,...
For a two-dimensional array, use the form of two curly brackets, the specific form is as follows

int a[2][3]={
    
    {
    
    1,2},{
    
    2,3}}

In the same way, zero padding in other positions is not written.
In addition, the array is stored in order .

Arrays can be passed as function parameters. Arrays can be passed
as function parameters. When the name of the array is used as a function parameter, the address is passed.
Specific examples are as follows

# include <iostream>
using namespace std;

void show(int a[], int i)
{
    
      
	for (int j = 0; j < i; j++)
	{
    
    
		if (a[j] == ' ')
		{
    
    
			cout << "are you creazy?" << endl;
			break;
		}
		cout << a[j] << endl;
	}
}
int main()
{
    
    
	int a[5] = {
    
     1,2,3,4,5 };
	show(a, 4);

}

The results of the operation are as follows:
Insert picture description here
From the above results, we can see that we pass the name of an array as a parameter into the function, which is equivalent to passing the address of the array into it. Incoming is the address

Array needs to be mastered: the declaration and initialization of the array, the use of the array (using subscripts), the type of the array can be a custom type (for example, the example above is a custom point class ). And if the name of the array is passed as a parameter, the address is passed , and the array storage is continuous.

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/114144131