关于引用参数输出 && 内联函数 && 二元组比较 && 引用

内联函数inline

常规函数调用是使程序跳到另一个地址(函数地址),并在函数返回时结束。
内联函数:编译器将使用相应的函数代码替换函数调用。
对于内联代码,程序无需跳到另一个位置处执行代码,再跳回来。但是代价是需要占用更多的内存。
程序员请求将函数作为内联函数时,编译器并不一定会满足这种要求。它可能认为该函数过大或者注意到函数调用了自己(内联函数不能递归)。

引用变量:
1、 引用是已定义的变量的别名(另一个名称)。将引用变量用作参数,函数将使用原始数据,而不是其副本。
2、 将引用用作函数参数 demo如下
3、 将引用用于结构
4、 将引用用于类对象
#include
#include <stdio.h>
using namespace std;

/典型例子:三种交换
值传递、引用传递、指针传递
/
//值
void swapv(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
//引用
void swapr(int &a, int &b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
//指针
void swapp(int *a,int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
int main()
{

//demo2 将引用用作函数参数
/*引用经常被用作函数参数,使得函数中的变量名称称为被调用程序中的变量的别名
*/
int wallet1 = 100;
int wallet2 = 200;
cout << "wallet1=" << wallet1;
cout << ",wallet2=" << wallet2 << endl;

swapv(wallet1, wallet2);
cout << "wallet1=" << wallet1;
cout << ",wallet2=" << wallet2 << endl;

/*
前面讲过:应在定义引用变量时对其进行初始化,函数调用使用实参初始化形参
*/
swapr(wallet1, wallet2);
cout << "wallet1=" << wallet1;
cout << ",wallet2=" << wallet2 << endl;


swapp(&wallet1, &wallet2);
cout << "wallet1=" << wallet1;
cout << ",wallet2=" << wallet2 << endl;
system("pause");
return 0;

}

5、 将引用用于结构
#include
#include <stdio.h>
#include
using namespace std;

struct free_throws
{
string name; //string 不是一种基本数据类型,其是一个类
int made;
int attempts;
float percent;
};
void display(const free_throws &ft);
void set_pc(free_throws & ft);
free_throws & accumulate(free_throws & target,const free_throws & source);

int main()
{
//部分初始化,剩余部分为0
free_throws one = { “aaaaa”,13,14 };
free_throws two = {“bbbbb”,10,16};
free_throws three = {“ccccc”,7,9};
free_throws four = {“ddddd”,5,9};
free_throws five = {“eeeee”,6,14};
free_throws team = {“Throwgoods”,0,0};

//未初始化对象
free_throws dup;

//操作
set_pc(one);
display(one);
accumulate(team,one);
display(team);

//使用返回值作为参数
display(accumulate(team,two));
accumulate(accumulate(team,three),four);
display(team);

dup = accumulate(team,five);

system("pause");
return 0;

}

void display(const free_throws &ft)
{
cout << “Name:”<<ft.name << endl; //不添加string头文件,会出现没有与操作数匹配的运算符
cout << “Made:” << ft.made << endl;
cout << “Attempts:” << ft.attempts << endl;
cout << “Percent:” << ft.percent << endl;

//ft.made += 1;  //肯定是错误的,结构体被定义为const,不可以修改,表达式为不可修改的左值

}
void set_pc(free_throws & ft)
{
if (ft.attempts != 0)
{
ft.percent = 100.0f*float(ft.made) / float(ft.attempts); //没有const,可以修改结构体中的数据
}
else
{
ft.percent = 0;
}
}
//返回值为结构体的引用
//返回作为一个参数传递给函数的引用
free_throws & accumulate(free_throws & target, const free_throws & source)
{
target.attempts += source.attempts;
target.made += source.made;
set_pc(target);

return target;

}
/*
为何要返回引用?
1、double m=sqrt(16.0);
cout<<sqrt(25.0);
值4.0被复制到一个临时位置,然后被复制给m;
值5.0被复制到一个临时位置,然后被传递给cout;
2、dup = accumulate(team,five);
如果accumulate()返回一个结构,而不是指向结构的引用,将整个结构复制到一个临时位置,再将这个拷贝复制给dup;
但在返回值为引用时,将直接将team复制到dup中,其效率更高。

返回引用需要注意的问题?
重点:应避免返回函数终止时 不再存在的内存单元的引用

//错误!!!
函数返回一个指向局部变量/临时变量(newguy)的引用,函数运行完毕后它将不再存在。【同时应避免返回临时变量的指针】

const free_throws & clone2(free_throws & ft)
{
free_throws newguy;
newguy = ft;
return newguy;
}

//正确!!!
//这样写 程序在删除newguy之前对它进行了拷贝(副本),调用函数将得到该拷贝
const free_throws clone2(free_throws & ft)
{
free_throws newguy;
newguy = ft;
return newguy;
}

解决方法:
1、返回一个作为参数传递给函数的引用。作为参数的引用将指向调用函数的使用的数据;
2、用new来分配新的存储空间,并返回指向该空间的指针;
demo:
const free_throws & clone(free_throws & ft)
{
free_throws *pt;
*pt=ft;
return *pt;
}
*/

引用参数输出

/*二元比较
引用输出
*/
#include
using namespace std;
int max(int x,int &y,int &z);

int main()
{
short a = 4;
cout << sizeof(short) << endl; //2
int x = 1;
int y = 2;
int z = 3;

max(x,y,z);

cout << "x=" << x << endl;//1
cout << "y=" << y << endl;//3
cout << "z=" << z << endl;//4

getchar();
return 1;

}
int max(int x, int &y, int &z) // y、z可以当作输出使用 引用别名
{
x++;
y++;
z++;
return x;
}

/*
引用:就是某一个变量的别名,对引用的操作与对变量的操作完全一样
1、引用声明完毕后,相当于目标变量名有两个名称,即该目标原名称和引用名,且不能再把该引用名作为其他变量名的别名。
2、引用本身不占内存单元,系统也不给引用分配存储单元,所以,对引用求地址就是对目标变量求地址。
3、不能建立数组的引用,因为数组是一个由若干个元素组成的集合,所以无法建立一个数组的别名。
4、引用必须在创建时被初始化
*/

#include
#include <stdio.h>
using namespace std;
//引用作为参数 输出
void swap(int& x, int& y)
{
/int tmp;
tmp = x;
x = y;
y = tmp;
/
//有初始化的值
x = 10;
y = 20;
x++;
y++;
}
int add(int w)
{
int ah, bh;
swap(ah, bh); //ah,bh作为输出
cout << “ah=” << ah << “\tbh=” << bh << endl;

return(w+ah+bh);

}
int main()
{
/*int a = 10, b = 20;
cout << “a=” << a << “\tb=” << b << endl;

swap(a, b);
cout << "a=" << a << "\tb=" << b << endl;*/

int c = add(3);
cout << "c=" << c << endl;

getchar();
return 1;

}

/*
输出结果:
ah=11 bh=21
c=35
*/

二元组比较

//先找到最小IP,再找到最小PID

if (tmpIPAddr < minIPAddr)
{
minIPAddr = tmpIPAddr;
minPID = tmpPID;
}
else if (tmpIPAddr == minIPAddr)
{
if (tmpPID < minPID)
{
minIPAddr = tmpIPAddr;
minPID = tmpPID;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值