c++学习二
2015-06-03 12:21
381 查看
#include <iostream>
using namespace std;
template<typename T>
T max(T a, T b, T c)
{
if(a > b) b = a;
if(c > b) return c;
return b;
}
void swap(int *p1, int *p2)
{
int *temp=(int *)malloc(sizeof(int));
//int *temp;//if directly use the temporary/iterim/extemporaneous/provisional variant to store some value, the compiler will give a error
//we can use the pointer which is not initialized to point a another address, it means if compiler or system not intialize the pointer temp,
// the pointer temp will have no space in system's memory, so you wouldn't store a data into the memory which you think to be owned by the temp
// but you can use temp to store a pointer, for example, temp = p1 will no error(p1 must be exist already and has its address), this is not to operate for memory, only variant itself
*temp = *p1;
*p1 = *p2;
*p2 = *temp;
free(temp);
}
void swap(int *p1, int *p2)
{// if we write the funciton as this above, we will not get the expected result, so we should know the value paased into the function is unidirectional, not
// bidirctional, so you can change the value of formal parameter, not to change the actual parameter, this is the reason why you cann't change the value of
// a1 and b1
int *temp;
temp = p1;
p1 = p2;
p2 = temp;
}
int main(int argc ,char **argv)
{
char a[20]="l am a teacher";
int i1=185,i2=-76,i3=567,i;
double d1=56.87,d2=90.23,d3=-3214.78,d;
long g1=67854,g2=-912456,g3=673456,g;
int a1, b1;
int *point1, *point2;
cin>>a1>>b1;
point1 = &a1;
point2 = &b1;
if(a1 < b1 )
swap(point1,point2);
cout << "a1----"<<a1 << "---b1---"<< b1 <<endl;
i=max(i1,i2,i3); //调用模板函数,此时T被int取代
d=max(d1,d2,d3); //调用模板函数,此时T被double取代
g=max(g1,g2,g3); //调用模板函数,此时T被long取代
cout<<"i_max="<<i<<endl;
cout<<"f_max="<<d<<endl;
cout<<"g_max="<<g<<endl;
cout<<"memory length--"<<sizeof(a)<<endl;
cout<<"real length-----"<<strlen(a)<<endl;
getchar();
getchar();
return 0;
}
using namespace std;
template<typename T>
T max(T a, T b, T c)
{
if(a > b) b = a;
if(c > b) return c;
return b;
}
void swap(int *p1, int *p2)
{
int *temp=(int *)malloc(sizeof(int));
//int *temp;//if directly use the temporary/iterim/extemporaneous/provisional variant to store some value, the compiler will give a error
//we can use the pointer which is not initialized to point a another address, it means if compiler or system not intialize the pointer temp,
// the pointer temp will have no space in system's memory, so you wouldn't store a data into the memory which you think to be owned by the temp
// but you can use temp to store a pointer, for example, temp = p1 will no error(p1 must be exist already and has its address), this is not to operate for memory, only variant itself
*temp = *p1;
*p1 = *p2;
*p2 = *temp;
free(temp);
}
void swap(int *p1, int *p2)
{// if we write the funciton as this above, we will not get the expected result, so we should know the value paased into the function is unidirectional, not
// bidirctional, so you can change the value of formal parameter, not to change the actual parameter, this is the reason why you cann't change the value of
// a1 and b1
int *temp;
temp = p1;
p1 = p2;
p2 = temp;
}
int main(int argc ,char **argv)
{
char a[20]="l am a teacher";
int i1=185,i2=-76,i3=567,i;
double d1=56.87,d2=90.23,d3=-3214.78,d;
long g1=67854,g2=-912456,g3=673456,g;
int a1, b1;
int *point1, *point2;
cin>>a1>>b1;
point1 = &a1;
point2 = &b1;
if(a1 < b1 )
swap(point1,point2);
cout << "a1----"<<a1 << "---b1---"<< b1 <<endl;
i=max(i1,i2,i3); //调用模板函数,此时T被int取代
d=max(d1,d2,d3); //调用模板函数,此时T被double取代
g=max(g1,g2,g3); //调用模板函数,此时T被long取代
cout<<"i_max="<<i<<endl;
cout<<"f_max="<<d<<endl;
cout<<"g_max="<<g<<endl;
cout<<"memory length--"<<sizeof(a)<<endl;
cout<<"real length-----"<<strlen(a)<<endl;
getchar();
getchar();
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++中拷贝构造函数的应用详解
- C++中引用(&)的用法与应用实例分析
- C++使用CriticalSection实现线程同步实例
- C++智能指针实例详解
- 解析C++ 浮点数的格式化输出