Reference To Array And Pointer To Array
来源:互联网 发布:26了转行程序员好伤心 编辑:程序博客网 时间:2024/05/21 07:11
数组引用和数组指针和两种非常优美而又用途广泛的语言设施,但很多其名气不是很大,因为这两设施C++中新引入的,大家一般不怎么注意的.
下面通过一小段代码,来展示其用法.
#include <iostream>
using namespace std;
template <class T, int size>
T _min(T (&r_a)[size]) // 函数模板的形参是数组引用
...{
T min_val = r_a[0];
for (int i = 1; i < size; ++i)
if (min_val > r_a[i]) min_val = r_a[i];
for (int i = 0; i < size; ++i)
r_a[i] = 1;
return min_val;
}
template <class T, int size>
T _min(T (&p_a)[size]) // 函数模板的形参是数组指针
...{
T min_val = (*p_a)[0];
for (int i = 1; i < size; ++i)
if (min_val > (*p_a)[i]) min_val = (*p_a)[i];
for (int i = 0; i < size; ++i)
(*p_a)[i] = 1;
return min_val;
}
int main()
...{
int a[] = ...{2,3,5,3,67,123,4};
double b[] = ...{2.1, 4.23, 56.32, 2.0, 1.2};
int (&ria)[5] = a; // 定义数组引用
double (*pda)[5] = &b; // 定义数组指针
for (int i = 0; i < 5; ++i)
cout << ria[i] << " "; // 使用数组引用
cout << " ";
for (int i = 0; i < 5; ++i) ...{
cout << (*pda)[i] << " "; // 数组指针
cout << " ";
cout << _min(a) << " "; // 数组引用作为函数的实参
cout << _min(&b) << " "; // 数组指针作为函数的实参
system("pause");
return 0;
}
using namespace std;
template <class T, int size>
T _min(T (&r_a)[size]) // 函数模板的形参是数组引用
...{
T min_val = r_a[0];
for (int i = 1; i < size; ++i)
if (min_val > r_a[i]) min_val = r_a[i];
for (int i = 0; i < size; ++i)
r_a[i] = 1;
return min_val;
}
template <class T, int size>
T _min(T (&p_a)[size]) // 函数模板的形参是数组指针
...{
T min_val = (*p_a)[0];
for (int i = 1; i < size; ++i)
if (min_val > (*p_a)[i]) min_val = (*p_a)[i];
for (int i = 0; i < size; ++i)
(*p_a)[i] = 1;
return min_val;
}
int main()
...{
int a[] = ...{2,3,5,3,67,123,4};
double b[] = ...{2.1, 4.23, 56.32, 2.0, 1.2};
int (&ria)[5] = a; // 定义数组引用
double (*pda)[5] = &b; // 定义数组指针
for (int i = 0; i < 5; ++i)
cout << ria[i] << " "; // 使用数组引用
cout << " ";
for (int i = 0; i < 5; ++i) ...{
cout << (*pda)[i] << " "; // 数组指针
cout << " ";
cout << _min(a) << " "; // 数组引用作为函数的实参
cout << _min(&b) << " "; // 数组指针作为函数的实参
system("pause");
return 0;
}
- Reference To Array And Pointer To Array
- array,pointer and reference
- Reference to array and hash
- reference to function and pointer to function
- simplexml to array and array to xml
- pointer to ref of an array, and to the func that return a ref of an array
- pointer to pointer vs. reference to pointer
- Pointer and Array
- pointer and array
- const pointer and array
- c pointer and array
- array and pointer
- Reference and array clone
- Difference between pointer and array
- array and pointer in c
- attempt to reference field of non-structure array
- reference to a zero-sized array is illegal解决办法
- [Matlab]Attempt to reference field of non-structure array
- [瓶颈] 从SafeArray无法销毁 到 ocx生成
- asp.net回答问题
- 在VS2005怎样自动生成属性
- JAVA ProgressBar系列之一
- JAVA ProgressBar系列之二
- Reference To Array And Pointer To Array
- JAVA ProgressBar系列之三
- JAVA ProgressBar系列之四
- Tomcat 6.0安装好了后无法启动服务 解决方法
- 解决IE7下本地图片预览问题
- JAVA ProgressBar系列之五
- JAVA ProgressBar系列之六
- JAVA ProgressBar系列之七
- JAVA ProgressBar系列之八