c++学习二
来源:互联网 发布:淘宝上真正的原单店 编辑:程序博客网 时间:2024/06/06 09:27
#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;
}
0 0
- C#Task 学习 二
- Objective-C学习二
- [C语言学习]作业二
- C++test 学习(二)
- Objective-C学习大纲 二
- 学习《c++primer》笔记二
- object c 学习(二) : Objects
- c语言学习笔记二
- object-c 学习(二)
- C语言学习笔记<二 >
- C/C++学习笔记(二)
- C++ 学习 二
- C基础学习(二)
- c#winform学习(二)
- Objective-c 学习笔记(二)
- C语言指针学习 二
- Objective C学习笔记(二)
- C语言指针学习二
- 设置linux终端字符颜色
- [Erlang]erlang与php的通信
- Matplot中文乱码完美解决方案
- Android Animation动画(很详细)
- IOS——UITextField被键盘遮蔽解决方案
- c++学习二
- Android-IntentService
- Windows 7程序开发系列之二(JumpList篇2 - Destination)
- Linux应用程序学习之制作静态函数库
- 记录一个JS异常Uncaught NotFoundError
- Android-BoundService
- iOS 获取系统相册全部照片以及照片信息
- IOS银联支付的流程
- R语言离线安装二进制包的几个问题