第8章 函数探幽

8.1C++内联函数

        内联函数,编译器将使用相应的函数代码替换函数调用,程序无需跳到另一个位置处执行代码,在跳回来。因此,内联函数运行速度更快,但是需要占用更多内存。

  • 在函数声明和定义前加关键字inline
  • 不能做递归函数

C语言使用预处理器语句#define来提供宏----内联函数的原始实现。

#define SQUARE(X) X*X;

inline double square(double x) {return x*x};

内联函数更安全。比如a = SQUARE(c++); //is replaced by d=c++*c++;

8.2 引用变量

int rats;
int &rodents = rats;

rodents rats指向相同的值和内存单元

引用创建时必须初始化。

右值引用(需要研究一下)

右值引用详解_飞羚的博客-CSDN博客_右值引用

返回引用,要避免返回函数终止时不再存在的内存单元引用。

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

有两种常规方法避免上述问题:

1、返回一个作为参数传递给函数的引用。

free_throws &accumulate(free_throws &target,const free_throws &source)
{
    target.attempts += source.attempts;
    target.made += source.made;
    set_pc(target);
    return target;
}

2、用new来分配新的储存空间。这种方法需要手动delete内存,容易忽略。

const free_throws &clone(free_throws &ft)
{
    free_throws *pt;
    *pt = ft;
    return *pt;
}

const 用于引用返回类型

accumulate(dup,five) = four;

        函数返回值指向dup的引用,时一个可修改的内存,因此这条语句合法。常规函数返回值位于临时内存单元中,运行到下一条语句可能不再存在。

        要使用引用返回值,但又不允许上述函数赋值操作,将返回类型声明为const即可。

const free_throws &accumulate(free_throws &target,const free_throws &source)

对象、继承和引用

示例代码

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

void file_it(ostream &os,double fo,const double fe[],int n);
const int LIMIT = 5;

int main()
{
    ofstream fout;
    const char *fn = "ep-data.txt";
    fout.open(fn);
    if(!fout.is_open())
    {
        cout << "Can't open " << fn << endl;
        exit(EXIT_FAILURE);
    }
    double obj;
    cout << "Enter the focal length os your ";
    cin >> obj;
    double eps[LIMIT];
    for(int i=0;i<LIMIT;i++)
    {
        cout << "Eyepiece #" << i+1 << ": ";
        cin >> eps[i];
    } 
    file_it(fout,obj,eps,LIMIT);
    file_it(cout,obj,eps,LIMIT);
    fout.close();
    cout << "Done\n";
    return 0;
}

void file_it(ostream &os,double fo,const double fe[],int n)
{
    ios_base::fmtflags initial;
    initial = os.setf(ios_base::fixed);  //将对象置于使用定点表示法的模式
    os.precision(0);
    os <<"Focal length of obj:" << fo << " mm\n";
    os.setf(ios::showpoint); //显示小数点模式
    os.precision(1);  //显示1位小数点
    os.width(12); //设置下一次输出使用的字段宽度
    os << "f.1. eyepiece";
    os.width(15);
    os <<"magnification" << endl;
    for(int i=0;i<n;i++)
    {
        os.width(12);
        os << fe[i];
        os.width(15);
        os << int(fo/fe[i] + 0.5) << endl;
    }
    os.setf(initial);
}

运行结果:

Enter the focal length os your 4
Eyepiece #1: 1.1
Eyepiece #2: 2.2
Eyepiece #3: 3.3
Eyepiece #4: 4.4
Eyepiece #5: 5.5
Focal length of obj:4 mm
f.1. eyepiece  magnification
         1.1              4
         2.2              2
         3.3              1
         4.4              1
         5.5              1
Done

8.3默认参数

char* left(const char* str,int n=1);

对于带参数列表函数,必须从右向左添加默认值。

int harpo(int n,int m=4,int j=5) //有效
int harpo(int n,int m=4,int j) //无效

8.4函数重载

类型引用和类型本身不能作为重载函数

double cube(double x);
double cube(double &x);  

8.5函数模板

template <typename AnyType>
void Swap(AnyType &a,AnyType &b)
{
    AnyType temp;
    temp = a;
    a = b;
    b = temp;
}

显示具体化:

#include <iostream>

template <typename T>
void Swap(T &a,T &b);

struct job
{
    char name[40];
    double salary;
    int floor;
};

template <> void Swap<job>(job &j1,job &j2);
void Show(job &j);

int main()
{
    using namespace std;
    cout.precision(2);
    cout.setf(ios::fixed,ios::floatfield);

    int i=10,j=20;
    cout << "i,j=" << i << "," << j << ".\n";
    cout << "using compiler generated int swapper:\n";
    Swap(i,j);
    cout << "Now i,j = " << i << "," << j << ".\n";

    job sue = {"Susan Yaffee",73000.60,7};
    job sidey = {"Sidney Taffee",78060.72,9};
    cout << "before job swappong\n";
    Show(sue);
    Show(sidey);
    return 0;
}

template <typename T>
void Swap(T &a,T &b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}

template <> void Swap<job>(job &j1,job &j2)
{
    double t1;
    int t2;
    t1 = j1.salary;
    j1.salary = j2.salary;
    j2.salary = t1;
    t2 = j1.floor;
    j2.floor = j1.floor;
    j1.floor = t2;
}

void Show(job &j)
{
    std::cout << j.name << " :$" << j.salary << " on floor" << j.floor << std::endl; 
}

运行结果:

i,j=10,20.
using compiler generated int swapper:
Now i,j = 20,10.
before job swappong
Susan Yaffee :$73000.60 on floor7
Sidney Taffee :$78060.72 on floor9

显示实例化:template void Swap<int>(int ,int);

具体实例化:template <> void Swap<int>(int &,int &); 或者 template <> void Swap(int &,int &);

回答: 在C语言中,string函数是一个字符串处理函数库,它包含在<string.h>头文件中。其中常用的函数有strcpy、strlen和strnset。 strcpy函数用于将一个字符串复制到另一个字符串中。它的函数原型是:char *strcpy(char *dest, const char *src)。其中,dest是目标字符串,src是源字符串。这个函数会将src字符串的内容复制到dest字符串中,并返回dest字符串的指针。\[1\] strlen函数用于计算字符串的长度。它的函数原型是:size_t strlen(const char *str)。这个函数接收一个字符串的首地址,然后遍历字符串直到遇到'\0'字符,返回字符串的长度。\[2\] strnset函数用于将指定的字符替换字符串中的一部分字符。它的函数原型是:char *strnset(char *str, int c, size_t n)。其中,str是要操作的字符串,c是要替换的字符,n是要替换的字符个数。这个函数会将字符串中的指定部分字符替换为指定的字符。\[3\] 这些函数都是C语言中常用的字符串处理函数,可以帮助我们进行字符串的复制、长度计算和字符替换等操作。 #### 引用[.reference_title] - *1* *3* [C语言中string函数详解](https://blog.csdn.net/weixin_30902251/article/details/99781150)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [c语言String字符串函数探幽](https://blog.csdn.net/Duary/article/details/106163396)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值