美文网首页
C++ 引用

C++ 引用

作者: zcwfeng | 来源:发表于2023-07-12 18:47 被阅读0次

C 语言常用小点
C字符串
C 基础-指针,函数处理器
C 文件操作
JNI 基础 C语言版

C++ 基础知识点大纲

[C++ 基础经验知识点]
C++ 基础代码模板和使用
C++ 基础-定点模式
C++ 宏定义
C++ 指针区分
C++ 指针特别篇-指针转换和智能指针
C++ 类的继承和多继承
C++ this 原理
C++浅拷贝和深拷贝的原理
C++ 函数
C++ 仿函数
C++ 友元函数理解
C++ STL
C++ 模板函数纯虚函数和Java对比
C++ 函数运算符重载(二)化简版
C++ 多线程
C++ 算法包和源码简析
C++ 引用

引用变量,作为别名,共享存储

引用就想夫妻俩,共用一个内存 ,形参影响实参。

引用基本使用

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int rats = 101;
    int &rodents = rats;

    cout << "rats=" << rats << endl;
    cout << "rodens=" << rodents << endl;
    rodents++;

    cout << "rats=" << rats << endl;
    cout << "rodens=" << rodents << endl;
    cout << "&rats=" << &rats << endl;
    cout << "&rodens=" << &rodents << endl;   

    int bunnies = 50;
    rodents = bunnies;
    cout << "bunnies = " << bunnies;
    cout << ",rats = " << rats;
    cout << ",rodents = " << rodents << endl;
    cout << "bunnies addr = " << &bunnies;
    cout << ",rats addr = " << &rats;
    cout << ",rodents addr = " << &rodents << endl;
    return 0;
}

引用传惨,是否可以修改,const可以限制参数修改

#include <iostream>
#include <string>
using namespace std;
void swapr(int & a, int & b);
void swapr2(int * a, int * b);
void swapr3(int a, int  b);
int main()
{
    int wallet1 = 100;
    int wallet2 = 350;
    cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
    cout << "using references to swap contents:\n";
    swapr(wallet1,wallet2);
    cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;

    cout << "using pointers to swap contents:\n";
    swapr2(&wallet1,&wallet2);
    cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
    cout << "using value to swap contents:\n";
    swapr3(wallet1,wallet2);
    cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;

    cout << &"abc" << endl;

    return 0;
}

void swapr(int & a, int & b)
{
    int temp = a;
    a = b;
    b = temp;
}
void swapr2(int * a, int * b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void swapr3(int a, int  b)
{
    int temp = a;
    a = b;
    b = temp;
}

结构体的引用

#include <iostream>
#include <string>
struct free_throws
{
    std::string name;
    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()
{
    free_throws one = {"Ifelsa Branch", 13, 14};
    free_throws two = {"Andor Knott", 10, 16};
    free_throws three = {"Minnie Max", 7, 9};
    free_throws four = {"Whily Looper", 5, 9};
    free_throws five = {"Long Long", 6, 14};
    free_throws team = {"Throwgoods", 0, 0};
    free_throws dup;
    set_pc(one);
    display(one);
    accumulate(team, one);
    display(team);
// use return value as argument
    display(accumulate(team, two));
    accumulate(accumulate(team, three), four);
    display(team);
// use return value in assignment
    dup = accumulate(team,five);
    std::cout << "Displaying team:\n";
    display(team);
    std::cout << "Displaying dup after assignment:\n";
    display(dup);
    set_pc(four);
// ill-advised assignment
    accumulate(dup,five) = four;
    std::cout << "Displaying dup after ill-advised assignment:\n";
    display(dup);
    // std::cin.get();
    return 0;
}

void display(const free_throws & ft)
{
    using std::cout;
    cout << "Name: " << ft.name << '\n';
    cout << "  Made: " << ft.made << '\t';
    cout << "Attempts: " << ft.attempts << '\t';
    cout << "Percent: " << ft.percent << '\n';
}
void set_pc(free_throws & ft)
{
    if (ft.attempts != 0)
        ft.percent = 100.0f *float(ft.made)/float(ft.attempts);
    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;
}

引用和类,如果是char * 类型字符串和 string 参数不匹配,我们需要在前面加上const

最后一个函数的引用不存在,所以打印不到内容。

include <iostream>
#include <string>
using namespace std;
string version1(const string &s1, const string &s2);
const string &version2(string &s1, const string &s2);
const string &version3(string &s1, const string &s2);
int main()
{
    string input;
    string copy;
    string result;
    cout << "Enter a string: ";
    getline(cin, input);
    cout << "Your string as entered: " << input << endl;

    result = version1(input, "***");

    cout << "Your change string: " << result << endl;
    cout << "Your original string: " << input << endl;
    cout << "-----------------------" << endl;
    result = version2(input, "***");
    cout << "Your change string: " << result << endl;
    cout << "Your original string: " << input << endl;
    cout << "-----------------------" << endl;
    cout << "Reset original stirng: \n";
    input = copy;
    result = version3(input,"@@@");
    cout << "Your change string: " << result << endl;
    cout << "Your original string: " << input << endl;
    return 0;
}

string version1(const string &s1, const string &s2)
{
    // return s2 + s1 + s2;
    string temp;
    temp = s2 + s1 + s2;
    return temp;
}

// const string &s2 类型不匹配,必须加入const,前面右值“***”付给左值s2
// 返回值 const string & 
const string &version2(string &s1, const string &s2)
{
    s1 = s2 + s1 + s2;
    return s1;
}


// 这个是错误的temp被释放,引用已经不存在
const string &version3(string &s1, const string &s2)
{
    string temp;
    temp = s2 + s1 + s2;
    return temp;
}

ostream 引用等

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int LIMIT = 5;
void file_it(ostream &os, double fo, double *fe, int n);
int main()
{
    ofstream fout;
    const char *fn = "ep-data.txt";
    fout.open(fn);
    if (!fout.is_open())
    {
        cout << "can not open " << fn << "Bye ." << endl;
        exit(EXIT_FAILURE);
    }
    double objective;
    cout << "Enter the focal length of your tlescope objective in mm: ";
    cin >> objective;
    double eps[LIMIT];
    cout << "Enter the focal length of eyeleces in mm:\n";
    for (int i = 0; i < LIMIT; i++)
    {
        cout << "Eyepiece # " << i + 1 << ": ";
        cin >> eps[i];
    }
    file_it(cout, objective, eps, LIMIT);
    file_it(fout, objective, eps, LIMIT);

    return 0;
}

void file_it(ostream &os, double fo, double *fe, int n)
{
    os << "Focal length of objective: " << fo << " mm\n";
    os << "f.l. eyepiece\t"
       << "magnification" << endl;
    for (int i = 0; i < n; i++)
    {
        os << fe[i];
        os << int(fo / fe[i] + 0.5) << endl;
    }
}

相关文章

网友评论

      本文标题:C++ 引用

      本文链接:https://www.haomeiwen.com/subject/vtypudtx.html