C++ | 数组逆序 | 问题总结

  1. 源代码

    #include
    #include
    using namespace std;
    typedef int ElemType;
    const int n = 10;
    
    void MyExchange(ElemType *a, ElemType *b){
    	ElemType temp;
    	temp = *a;  
    	*a = *b;
    	*b = temp;
    }
    
    void InverseArry(ElemType  arry[n], int  n){
    	ElemType *p = & arry[0];
    	ElemType *q = & arry[ n - 1];
    	while (p < q)//指针不相遇
    
    	{
    		MyExchange(p, q);
    		p++;
    		q--;
    	}
    
    }
    
    void PrintArrya(ElemType  arry[n], int  n) {
    	fstream f1;
    	f1.open("t1.txt", ios::in | ios::app);
    	cout << "逆序操作前:" << endl;
    	for (int i = 0; i < n; i++) {
    		f1 >> arry[i];
    		cout << arry[i] << endl;
    	}
    }
    void PrintArryb(ElemType  arry[n], int  n){
    	cout << "逆序操作后:" << endl;
    	for (int i = 0; i <  n; ++i) {
    		cout <<  arry[i] << endl;
    	}
    	cout << endl;
    
    }
    
    int main()
    {
    	ElemType arry[n] ;
    	PrintArrya(arry, n);
    	InverseArry(arry, n);
    	PrintArryb(arry, n);
    	system("pause");
    	return 0;
    
    }

     

  2. typedef

用途:为数据类型名 取个别名.这样就能批量声明变量

用法:

typedef char* PCHAR;

PCHAR pa, pb;  

为什么要用 typedef 关键字:

C++ | 数组逆序 | 问题总结_第1张图片

在上图中,char*类型名只声明了pa,未声明pb,pb指向字符型变量b时失败;

C++ | 数组逆序 | 问题总结_第2张图片像这样重复定义又太繁琐.

使用typedef后:

C++ | 数组逆序 | 问题总结_第3张图片

这是目前用得到的,还有其他用途,详见此处:http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.html

 

 

你可能感兴趣的:(C++)