C++ basic introduction array/one-dimensional array/two-dimensional array/definition form/function of array name

Introduction to C++ Basics Array/One-dimensional array/Two-dimensional array/Definition form/The role of array name

Table of contents

1. Brief introduction

Two, one-dimensional array

1. One-dimensional array definition square form

 2. One-dimensional array array name

Three, two-dimensional array

1. Two-dimensional array definition method

 2. Two-dimensional array array name

3. Two-dimensional array application case


1. Brief introduction

Organize some knowledge developed in C++, so that you can consult and use it in time when you encounter similar problems later.

This section introduces that an array, the so-called array, is a collection that stores data elements of the same type, including the functions and precautions of array/one-dimensional array/two-dimensional array/definition form/array name. If there are deficiencies, welcome to point out, or if you have a better method, please leave a message.

Array features:

  • Each data element in the array is of the same data type
  • Arrays are made up of contiguous memory locations

Two, one-dimensional array

1. One-dimensional array definition square form

Three definition forms of dimensional array definition:

  •  data type array name[array length];
  • data type array name[array length] = {value1,value2...};
  • Data type array name[ ] = {value1,value2...};

illustrate:

  • The naming convention of the array name is consistent with the naming convention of the variable name, do not have the same name as the variable
  • The subscript in the array is indexed from 0

code:

#include <iostream>
using namespace std;

int main() {

	//定义方式1
	//数据类型 数组名[元素个数];
	int score[10];

	//利用下标赋值
	score[0] = 100;
	score[1] = 99;
	score[2] = 85;

	//利用下标输出
	cout << score[0] << endl;
	cout << score[1] << endl;
	cout << score[2] << endl;


	//第二种定义方式
	//数据类型 数组名[元素个数] =  {值1,值2 ,值3 ...};
	//如果{}内不足10个数据,剩余数据用0补全
	int score2[10] = { 100, 90,80,70,60,50,40,30,20,10 };

	//逐个输出
	//cout << score2[0] << endl;
	//cout << score2[1] << endl;

	//一个一个输出太麻烦,因此可以利用循环进行输出
	for (int i = 0; i < 10; i++)
	{
		cout << score2[i] << endl;
	}

	//定义方式3
	//数据类型 数组名[] =  {值1,值2 ,值3 ...};
	int score3[] = { 100,90,80,70,60,50,40,30,20,10 };

	for (int i = 0; i < 10; i++)
	{
		cout << score3[i] << endl;
	}

	system("pause");

	return 0;
}

 2. One-dimensional array array name

Use of 1D array names:

  • Can count the length of the entire array in memory
  • You can get the first address of the array in memory

illustrate:

  • Note that the array name is a constant and cannot be assigned
  • Print the array name directly, you can view the first address of the memory occupied by the array
  • Perform sizeof on the array name to get the size of the memory space occupied by the entire array

code:

#include <iostream>
using namespace std;

int main() {

	//数组名用途
	//1、可以获取整个数组占用内存空间大小
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };

	cout << "整个数组所占内存空间为: " << sizeof(arr) << endl;
	cout << "每个元素所占内存空间为: " << sizeof(arr[0]) << endl;
	cout << "数组的元素个数为: " << sizeof(arr) / sizeof(arr[0]) << endl;

	//2、可以通过数组名获取到数组首地址
	cout << "数组首地址为: " << (int)arr << endl;
	cout << "数组中第一个元素地址为: " << (int)&arr[0] << endl;
	cout << "数组中第二个元素地址为: " << (int)&arr[1] << endl;

	//arr = 100; 错误,数组名是常量,因此不可以赋值


	system("pause");

	return 0;
}

 

Three, two-dimensional array

Two-dimensional array: It is to add one more dimension to the one-dimensional array.

1. Two-dimensional array definition method

Four ways to define a two-dimensional array:

  1. `Data type array name[row number][column number];`
  2. `Data type array name[row number][column number] = {{data1, data2}, {data3, data4}};`
  3. `Data type array name[row number][column number] = {data 1, data 2, data 3, data 4};`
  4. `Data type array name[ ][number of columns] = {data 1, data 2, data 3, data 4};`

illustrate:

  • The above 4 definition methods, using the second one is more intuitive and improves the readability of the code
  • When defining a two-dimensional array, if the data is initialized, the number of rows can be omitted

code:

#include <iostream>
using namespace std;

int main() {

	//方式1  
	//数组类型 数组名 [行数][列数]
	int arr[2][3];
	arr[0][0] = 1;
	arr[0][1] = 2;
	arr[0][2] = 3;
	arr[1][0] = 4;
	arr[1][1] = 5;
	arr[1][2] = 6;

	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << " ";
		}
		cout << endl;
	}

	//方式2 
	//数据类型 数组名[行数][列数] = { {数据1,数据2 } ,{数据3,数据4 } };
	int arr2[2][3] =
	{
		{1,2,3},
		{4,5,6}
	};

	//方式3
	//数据类型 数组名[行数][列数] = { 数据1,数据2 ,数据3,数据4  };
	int arr3[2][3] = { 1,2,3,4,5,6 };

	//方式4 
	//数据类型 数组名[][列数] = { 数据1,数据2 ,数据3,数据4  };
	int arr4[][3] = { 1,2,3,4,5,6 };

	system("pause");

	return 0;
}

 2. Two-dimensional array array name

Use of 1D array names:

  • View the memory space occupied by a two-dimensional array
  • Get the first address of the two-dimensional array

code:

#include <iostream>
using namespace std;

int main() {

	//二维数组数组名
	int arr[2][3] =
	{
		{1,2,3},
		{4,5,6}
	};

	cout << "二维数组大小: " << sizeof(arr) << endl;
	cout << "二维数组一行大小: " << sizeof(arr[0]) << endl;
	cout << "二维数组元素大小: " << sizeof(arr[0][0]) << endl;

	cout << "二维数组行数: " << sizeof(arr) / sizeof(arr[0]) << endl;
	cout << "二维数组列数: " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;

	//地址
	cout << "二维数组首地址:" << arr << endl;
	cout << "二维数组第一行地址:" << arr[0] << endl;
	cout << "二维数组第二行地址:" << arr[1] << endl;

	cout << "二维数组第一个元素地址:" << &arr[0][0] << endl;
	cout << "二维数组第二个元素地址:" << &arr[0][1] << endl;

	system("pause");

	return 0;
}

3. Two-dimensional array application case

Description: There are three students (Zhang San, Li Si, Wang Wu), the scores in one test are as follows, please output the total scores of the three students respectively

language math English
Zhang San 100 100 100
Li Si 90 50 100
Wang Wu 60 70 80

Code:

#include <iostream>
using namespace std;

int main() {

	int scores[3][3] =
	{
		{100,100,100},
		{90,50,100},
		{60,70,80},
	};

	string names[3] = { "张三","李四","王五" };

	for (int i = 0; i < 3; i++)
	{
		int sum = 0;
		for (int j = 0; j < 3; j++)
		{
			sum += scores[i][j];
		}
		cout << names[i] << "同学总成绩为: " << sum << endl;
	}

	system("pause");

	return 0;
}

 

Guess you like

Origin blog.csdn.net/u014361280/article/details/127602245