数组
一组具有相同数据类型的数据的集合
一维数组
包含一组数据对象或者一列数据对象的数组
(1)
数组长度为5时即a[5],但数组的下标是从0开始的。
初始化:
int a[5]={0,1,2,3,4};//记得是花括号{}
int a[10]={0,1,2,3,4};//其余元素默认为0
(2)
如果没有进行显式初始化的时候,C++会对数组元素进行默认初始化。在函数体外定义的内置数组,其元素全部初始化为0.
在函数体内定义的内置数组,其元素没有默认初始化。
如果数组元素的数据类型为类类型,则自动调用该类的默认构造函数进行初始化,如果该类型没有默认构造函数,则必须为该数组的元素提供显式初始化。
#include<iostream>
//#include<string>
using namespace std;
int a[5] = { 0, 1, 2 };
int b[5];
void main()
{
int i;
int c[5] = { 0, 1, 2 };
int d[5];
cout << "The elements of a are : ";
for (i = 0; i < 5; i++)
{
cout << a[i] << " ";
}
cout << endl;
cout << "The elements of b are : ";
for (i = 0; i < 5; i++)
{
cout << b[i] << " ";
}
cout << endl;
cout << "The elements of c are : ";
for (i = 0; i < 5; i++)
{
cout << c[i] << " ";
}
cout << endl;
cout << "The elements of d are : ";
for (i = 0; i < 5; i++)
{
cout << d[i] << " ";
}
cout << endl;
system("pause");
}
b[5]是在函数外定义的,所以默认初始化5个元素都为0;
d[5]是在函数内定义的,不进行默认初始化,如果强行输出的话就都为随机值。
(3)
当对全部数组元素进行赋初值的时候,可以不指定数组的长度
int a[]={0,1,2,3,4};
不能将一个数组直接赋值给另一个数组
一维数组的访问:下标可以是整型常量或是整型表达式
int a[5]={0,1,2,3,4};
a[0]=a[1]+a[2]*a[2*2];
#include<iostream>
using namespace std;
void main()
{
int a[] = { 0, 1, 2, 3, 4 }; //注意a初始化时方括号中没有数字
//int c[];
//这样没有后面的初始化列表,方括号内又没有数组,是非法的
int b = sizeof(a) / sizeof(a[0]);//求出b为数组a中的元素个数
cout << "a[] = {0,1,2,3,4};" << endl;
cout << "There are " << b << " elements in a ." << endl;
for (int i = 0; i < b; i++)
{
cout << "a[" << i << "] = " << a[i];
cout << endl;
}
system("pause");
}
字符数组
//字符数组元素个数
#include<iostream>
using namespace std;
void main()
{
char a[] = { 'H', 'e', 'l', 'l', 'o' }; //用字符字面值进行数组初始化
char b[] = "Hello"; //用字符串字面值进行数组初始化
//(末尾含有空字符,即字符长度+1)
int num_a = sizeof(a); //计算数组a的长度
int num_b = sizeof(b); //计算数组b的长度
cout << "char a[] = { 'H', 'e', 'l', 'l', 'o' };" << endl;
cout << "The length of a is : " << num_a << endl;
cout << "char b[] = Hello;" << endl;
cout << "The length of b is : " << num_b << endl;
cout << "The last member of b is : " << b[num_b - 1] << endl;
system("pause");
}
多维数组
维数在两个或两个以上的数组,多维数组是数组的数组。
数组类型 数组名称[整型常量表达式1][整型常量表达式2];
int a[10][10];
二维数组a包含10个一维数组,每个一维数组又包含10个整型数据
第一维称为行,第二维称为列,可以看成是矩阵形式
初始化时是先行后列,即先第一维后第二维
多元数组元素的访问:
需要给出行的下标和列的下标
//多维数组初始化
#include<iostream>
using namespace std;
int a[3][3]; //在函数体外定义的无初始化的二维数组(默认初始化为0)
void main()
{
int i,j;
int b[3][3];//在函数体外定义的无初始化的二维数组(无默认初始化)
int c[3][3] = { { 1 }, { 0, 2, } };//内嵌花括号
int d[3][3] = { 1, 0, 2 };//无内嵌花括号
cout << "Print out array a:" << endl;
for (i = 0; i < 3; i++)
{
cout << "a[" << i << "] : "; //遍历数组a中的三个一维数组
for (j = 0; j < 3; j++)
{
cout << "a[" << i << "][" << j << "] = " << a[i][j] << " ";//遍历1维数组中的三个元素
}
cout << endl;
}
cout << endl;
cout << "Print out array b:" << endl;
for (i = 0; i < 3; i++)
{
cout << "b[" << i << "] : ";
for (j = 0; j < 3; j++)
{
cout << "b[" << i << "][" << j << "] = " << b[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "Print out array c:" << endl;
for (i = 0; i < 3; i++)
{
cout << "c[" << i << "] : ";
for (j = 0; j < 3; j++)
{
cout << "c[" << i << "][" << j << "] = " << c[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "Print out array d:" << endl;
for (i = 0; i < 3; i++)
{
cout << "d[" << i << "] : ";
for (j = 0; j < 3; j++)
{
cout << "d[" << i << "][" << j << "] = " << d[i][j] << " ";
}
cout << endl;
}
system("pause");
}
//二维数组行列互换
#include<iostream>
using namespace std;
void main()
{
int a[3][3] = { { 1,2,3 },{ 3,4,5 },{ 6,7,8 } };
int b[3][3];
cout << "Before convert : " << endl;
for (int i = 0; i < 3; i++)
{
cout << "a[" << i << "] : ";
for (int j = 0; j < 3; j++)
{
cout << "a[" << i << "][" << j << "] = " << a[i][j] << " ";
b[j][i] = a[i][j];//a数组元素行列号互换赋值给b
}
cout << endl;
}
cout << endl;
cout << "After convert : " << endl;
for (int i = 0; i < 3; i++)
{
cout << "a[" << i << "] : ";
for (int j = 0; j < 3; j++)
{
cout << "a[" << i << "][" << j << "] = " << b[i][j] << " ";
}
cout << endl;
}
cout << endl;
system("pause");
}