首页 > 代码库 > Java程序猿学习C++之数组和动态数组

Java程序猿学习C++之数组和动态数组

数组:

#include <iostream>
using namespace std;

//模板函数
template <class T>
void dump(T val)
{
	cout << ">>>>" << __FUNCTION__ << endl;//内置的宏,打印当前函数的名字
	cout << sizeof(val) << ":" << typeid(val).name() << endl;
	cout << "<<<<" << __FUNCTION__ << endl << endl;
}

void dump1(int a[])
{
	cout << "a=" << a << endl;//第一个元素的地址
	cout << "&a=" << &a << endl;//指针的指针
	cout << "a+1=" << a + 1 << endl;//数组第二个元素的地址(+4)
	cout << "&a+1=" << &a + 1 << endl;//指向指针地址的下一个变量(+4) 
}

int main()
{
	const int len = 6;
	int a[len] = { 0, 2, 4, 6, 8 };//赋值少一个没关系,没有赋值会有默认值0
	int *pa = a;
	int *pa2 = a;
	//a是一个指针指向数组的第一个元素
	cout << "a=" << a << endl << "&a[0]=" << &(a[0]) << endl;

	for (int i = 0; i < len;i++)
	{
		a[i] = i;
		cout << "a[" << i << "]=" << a[i] << " "
			 << "*(a+i)=" << *(a + i) << " "
			 << "*(i+a)=" << *(i + a) << " "  //由上面表达式依据加法的结合律
			 << "i[a]=" << i[a] << " "
			
			 //cout << *(a++) << endl;//编译出错。a是数组的首地址(相当于常量),不能再对a进行赋值了
			 << "*(pa++)=" << *(pa++) << " "
			 << "pa[" << i << "]=" << pa2[i] << " "
			 << "*(pa+i)=" << *(pa2 + i) << " "
			 << "i[pa]=" << i[pa2] << " "
			 //综上,操作数组能够通过数组名,也能够通过指针变量
			 << endl;
	} 
	cout << "===================数组类型和指针类型=======================" << endl;
	cout << sizeof(a) << ":" << typeid(a).name() << endl;//24:int[6]
	dump(a);//4:int*
	//在数组声明的作用域之内,a是数组类型;在作用域之外(包含函数传递),a退化成一个整形指针
	
	
	cout << "====================数组类型取地址======================" << endl;
	cout << "a=" << a << endl;
	cout << "&a=" << &a << endl;//和上面的值一致都是第一个元素的地址
	cout << "a+1=" << a+1 << endl;//数组第二个元素的地址(+4)
	cout << "&a+1=" << &a+1 << endl;//数组后一个变量的地址(+4*6)

	cout << "====================指针类型取地址======================" << endl;
	dump1(a);
	return 0;
}

动态数组:

#include <iostream>
using namespace std;

/*C++内存管理
  栈:编译器自己主动分配和删除变量的区
      存储如局部变量,函数參数等。
	  高效但空间有限
  堆:存储由应用程序分配和删除的区
      不释放分配的内存会造成内存泄露
  全局/静态存储区:存储全局变量和静态变量
  常量存储区:存放常量,不同意改动
*/
int main()
{
	//动态数组定义格式:int *a = new int[array_size];
	//释放格式:delete []a;
	int len = 0;
	cin >> len;
	int *p = new int[len];

	

	for (int i = 0; i < len;i++)
	{
		p[i] = i;
		cout << "p[i]=" << p[i] << endl;
	}

	delete []p;
	return 0;
}

差别:

技术分享



二维的数组:

#include <iostream>
using namespace std;


void print1(int a[2][3], int rowLen, int colLen)
{
	cout << "=============print1===========" << endl;
	//4:(*)[3]事实上就是指向二维数组的指针
	cout << sizeof(a) << ":" << typeid(a).name() << endl;
	for (int row = 0; row < rowLen; row++)
	{
		int *p = a[row];
		for (int col = 0; col < colLen; col++)
		{
			cout << "a[" << row << "][" << col << "]="
				<< a[row][col] << " "
				<< *(p + col) << " "
				<< p[col] << " "
				<< *(*(a + row) + col) << " "
				<< endl;
		}
	}
}

void print2(int (*a)[3], int rowLen, int colLen)
{
	cout << "=============print2===========" << endl;
	for (int row = 0; row < rowLen; row++)
	{
		int *p = a[row];
		for (int col = 0; col < colLen; col++)
		{
			cout << "a[" << row << "][" << col << "]="
				<< a[row][col] << " "
				<< *(p + col) << " "
				<< p[col] << " "
				<< *(*(a + row) + col) << " "
				<< endl;
		}
	}
}

int main()
{   
	//二维数组
	const int I_ROW = 2;
	const int I_COL = 3;
	int a[I_ROW][I_COL] = { 1, 2, 3, 4, 5, 6 };
	//int a[][I_COL] = { 1, 2, 3, 4, 5, 6 };第一维的能够省略
	print1(a, I_ROW, I_COL);


	//指向二维数组的指针变量。格式:数据类型 (*指针名)[一维数组长度]    
	//int(*p)[4]
	print2(a, I_ROW, I_COL);

	//指针数组,格式:数据类型 *数组名[数组长度]
	//char *ary[2] = {str1,str2}
	//二维数组和指针数组的区别?
	char str1[] = "hello";
	char str2[] = "test";
	char str3[] = "!!";

	char array1[3][8] = { "hello", "test", "!!" };//数组里面存储了字符串数据
	char *array2[3] = { str1, str2, str3 };//数组里面存储了指向字符串数据的地址
	char *array3[3] = { array1[0], array1[1], array2[2] };//数组里面存储了指向字符串数据的地址

	return 0;
}





Java程序猿学习C++之数组和动态数组