ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++之一维&二维数组

2021-01-24 22:02:36  阅读:147  来源: 互联网

标签:arr main int C++ ++ 二维 数组 using include


一维&二维数组

数组中的每个数据元素都是相同的数据类型,数组是由连续的内存位置组成的

常见的数组定义:
第一种:
数据类型 数组名[数组长度];  //这里我们是要自己进行定义赋值的

#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[5];
	arr[0] = 1;
	arr[1] = 2;
	for (int i = 0; i < 5;i++) {
	
		cout << arr[i] << endl;
	}
	system("pause");
	return 0;

}

第二种:
在定义里就给赋值,如果给填充的数字小于数组的长度,没有填充的就自动填充为0
数据类型 数组名[数组长度]={1,2,3};
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[5] = {1,2,3,3};
	for (int i = 0; i < 5;i++) {
		cout << arr[i] << endl;
	}	
	system("pause");
	return 0;

}

第三种:
数据类型 数组名[]={1,2,3,4};
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[] = {1,2,3,3};
	for (int i = 0; i < 5;i++) {
		cout << arr[i] << endl;
	}	
	system("pause");
	return 0;

}

一维数组

通过数组名统计整个数组占用内存的大小和数量

int整形一个数组占用四个字节,效果如下:
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[6] = {1,2,3};
	
	cout << sizeof(arr) << endl;
	system("pause");
	return 0;

}

#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[6] = {1,2,3};
	
	cout << sizeof(arr)/sizeof(int) << endl;
	system("pause");
	return 0;

}

获取数组在内存中的首地址

地址本身其实是16进制的,但可以转换为int,发现int间为4个字字节差,同样也可以发现arr相当于&arr[0],&arr[0]代表的是第一个元素的地址,&就是取这个元素的内存地址,也就是元素的首个地址相当于arr,同样就是&arr[0]
  
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[5] = {1,2,3};
	
	cout << arr << endl;
	cout << (int)arr << endl;
	cout << (int)&arr[0] << endl;
	cout << (int)&arr[1] << endl;
	cout << (int)&arr[2] << endl;


	system("pause");
	return 0;

}

image-2021012

二维数组

此处列出常见方式,第一种:
数据类型  数组名 [行数][列数]={{a1,a2},{a3,a4}};
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[2][2]=
	{
		{1,2},
		{3,4}
	};
	system("pause");	
	return 0;

}

第二种:
数据类型  数组名[行数][列数]={a1,a2,a3,a4};
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[2][2]=
	{
		1,2,3,4
	};
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			cout << arr[i][j] << endl;
		}
	}
	system("pause");
	return 0;
}

第三种:
数据类型  数组名[][列数]={a1,a2,a3,a4};
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[][2]=
	{
		1,2,3,4
	};
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			cout << arr[i][j] << endl;
		}
	}
	system("pause");
	return 0;
}

查看二维数组所占用的内存地址

同样此处已知一个int占用4个字节
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[][2]=
	{
		1,2,3,4
	};
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			cout << arr[i][j] << endl;
		}
	}
	cout << sizeof(arr) << endl;
	system("pause");
	return 0;
}

一些数量的获得

获得数组的个数
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[][2]=
	{
		1,2,3,4
	};
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			cout << arr[i][j] << endl;
		}
	}
	cout << sizeof(arr)/sizeof(int) << endl;
	system("pause");
	return 0;
}

获得每行数组的个数
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[][2]=
	{
		1,2,3,4
	};
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			cout << arr[i][j] << endl;
		}
	}
	cout << sizeof(arr[0])/sizeof(int) << endl;
	system("pause");
	return 0;
}

获得详细的行&列
#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[][2]=
	{
		1,2,3,4
	};
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			cout << arr[i][j] << endl;
		}
	}
	cout << sizeof(arr)/sizeof(arr[0]) << endl;//行数
	cout << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;//列数
	system("pause");
	return 0;
}

获得数组首地址

#include <iostream>
#include<string>

using namespace std;

int main()
{
	int arr[][2]=
	{
		1,2,3,4
	};
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			cout << arr[i][j] << endl;
		}
	}
	cout << (int)arr << endl;
	cout << (int)&arr[0] << endl;
	cout << (int)&arr[0][0] << endl;
	system("pause");
	return 0;
}

一维数组基本元素逆置

#include<iostream>
#include<string>

using namespace std;

int main()
{
	int arr[3] = { 1,2,3 };
	int length = sizeof(arr) / sizeof(arr[0]);
	int start = 0;
	int end = length - 1;
	for (int i = 0; i < length; i++)
	{
		if (start < end)
		{
			int tem = arr[start];
			arr[start] = arr[end];
			arr[end] = tem;
			start++;
			end--;
		}
		cout << arr[i] << endl;
	}

	system("pause");
	return 0;
}

二维数组分数分配

#include<iostream>
#include<string>

using namespace std;

int main()
{
	int fenshu[3][3]{
		{1,2,3},
		{4,5,6},
		{7,8,9}
	};
	string tar[3] = { "a1","a2","a3" };
	int sum = 0;
	for (int a = 0; a < 3; a++) {
		cout << tar[1] << "分数为:";
		for (int b = 0; b <= 2; b++) {
			sum = sum + fenshu[a][b];
			cout << fenshu[a][b] << endl;
		}
		cout << tar[a] << "总分为:" << sum << endl;
		sum = 0;
	}

	system("pause");
	return 0;
}

标签:arr,main,int,C++,++,二维,数组,using,include
来源: https://www.cnblogs.com/Yang34/p/14322559.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有