从C到C++

1. 引用 reference

1.1 概念

引用与指针类似,但是使用更加简便,功能更加简单,可以认为引用是一个变量的“别名”,对引用进行操作与直接操作变量完全相同。

#include <iostream>using namespace std;int main(){    int a = 100;    int& b = a; // b是a的引用    b++;    cout << a << " " << b << endl; // 101 101    cout << &a << " " << &b << endl; // 0x61fe88 0x61fe88    return 0;}

1.2 性质

1. 可以改变引用的变量值,但是不能再次成为其它变量的引用。

#include <iostream>using namespace std;int main(){    int a = 100;    int& b = a; // b是a的引用    int c = 200;    b = c; // 赋值操作    cout << &a << " " << &b << " " << &c << endl; // 0x61fe88 0x61fe88 0x61fe84//    &b = c; 错误//    int& b = c; 错误    return 0;}

2. 声明引用时,必须对其进行初始化。

#include <iostream>using namespace std;int main(){    int a = 100;//    int& b;   错误//    b = a;//    cout << b << endl;    return 0;}

3. 声明引用时初始化的值不能是NULL

#include <iostream>using namespace std;int main(){    int a = 100;//    int& b = NULL; 错误    cout << b << endl;    return 0;}

4. 声明引用时,初始化的值如果是纯数值,需要给引用增加const关键字修饰,表示该引用是常引用,这样的引用不能改变其数值。

#include <iostream>using namespace std;int main(){//    int& b = 100; 错误    const int& b = 100;    cout << b << endl;    return 0;}

5. 可以将变量引用的地址赋值给一个指针,此时指针指向的还是原来的变量。

#include <iostream>using namespace std;int main(){    int a = 10;    int& b = a;    int* c = &b; // 指针c指向b,相当于直接指向a    cout << *c << endl; // 10    return 0;}

6. 可以对指针建立引用。

#include <iostream>using namespace std;int main(){    int a = 10;    int* p = &a; // 指针p指向a    int*& t = p; // t是p的引用    cout << t << " " << *t << endl; // 0x61fe88 10    return 0;}

7. 可以使用const对引用进行修饰,此时虽然不能直接改变引用的值,但是可以间接改变原变量值。

#include <iostream>using namespace std;int main(){    int a = 1;    const int& b = a; // b是a的常引用//    b++; 错误    a++;    cout << a << " " << b << endl; // 2 2    return 0;}

1.3 引用参数

【思考】写一个函数,通过传入两个参数可以交换传入参数的两个整型数值。

#include <iostream>using namespace std;// 【思考】写一个函数,通过传入两个参数可以交换传入参数的两个整型数值。/** * @brief swap1 交换失败 */void swap1(int a,int b){    a = a^b;    b = a^b;    a = a^b;    cout << a << " " << b << endl;}/** * @brief swap2 交换成功,但是繁琐 */void swap2(int* a,int* b){    *a = *a^*b;    *b = *a^*b;    *a = *a^*b;}/** * @brief swap3 交换成功 */void swap3(int& a,int& b){    a = a^b;    b = a^b;    a = a^b;}int main(){    int a = 1;    int b = 2;    swap1(a,b);    cout << a << " " << b << endl; // 1 2    swap2(&a,&b);    cout << a << " " << b << endl; // 2 1    swap3(a,b);    cout << a << " " << b << endl; // 1 2    return 0;}

使用引用参数还可以使参数传递的效率提高,因为不产生副本。

引用参数应该在能被定义为const的情况下,尽量定义const,以达到引用的安全性。

void show(const int& a){    cout << a << endl;}

2. 赋值

C++中除了可以使用=赋值外,还可以使用下面的方式赋值。

#include <iostream>using namespace std;int main(){    int a(1); // 给a赋值1    int b(a); // 给b赋值a    cout << a << " " << b << endl; // 1 1        return 0;}

C++11中增加了数据窄化的写法与提示。

#include <iostream>using namespace std;int main(){    double a = 3.14;    int b(a); // 传统C++写法    int c{a}; // C++11写法:给出警告    cout << b << endl;    cout << c << endl;    return 0;}

3. 键盘输入

在C++中可以使用cin来获得键盘输入,也支持连续输入,也在头文件iostream里。连续输入的内容可以使用空格或回车来分割。

cin输入的字符串类型是string,这是C++的字符串类型。

#include <iostream>using namespace std;int main(){    string s; // 创建一个内容为空的字符串变量    cout << s << endl; // 无输出结果    int i;    cout << "请输入一个字符串和一个数字:" << endl;    cin >> s >> i;    cout << "请输入的内容是:" << s << endl << i << endl;    return 0;}

4. 字符串类型 string

4.1 基础使用

string并不是C++的基本数据类型,string是C++内置的字符串类,使用时需要引入头文件<string>,而不是<string.h>,string在绝大多数情况下可以代替char*,不必担心内存是否足够和字符串长度。

string使用ASCII编码,只支持英文字符,严禁使用中文字符!!!

string内部集成了大量的字符串处理函数。

#include <iostream>using namespace std;int main(){    string s = "yui%^";    // 获取字符串长度    cout << s.size() << " " << s.length() << endl; // 5 5    cout << "--------for循环遍历--------" << endl;    for(int i = 0;i<s.size();i++)    {        // 使用下标取出对应的字符        cout << s[i] << " ";    }    cout << endl;    // C++11支持    cout << "--------for each循环遍历--------" << endl;    for(char c:s)    {        cout << c << " ";    }    // 迭代器:略,后面讲解    return 0;}

4.2 取出元素

string不光支持中括号 [ ] 的方式取出元素,还支持使用at函数的方式取出元素。

这两种方式的区别是:

● 前者取出的效率更高

● 后者更加安全

#include <iostream>using namespace std;int main(){    string s = "yui%^";    // 使用中括号取出元素    cout << s[1] << endl; // u    // 使用at函数取出元素    cout << s.at(1) << endl; // u    // 范围越界    cout << s[-999] << endl; // '\0'    cout << s.at(20) << endl; // 运行终止    cout << "主函数执行完毕" << endl;    return 0;}

5. 函数

5.1 内联函数 inline

内联函数的目的是取代宏定义的函数,使用关键字inline放在函数定义(非声明)的前面,可以将函数制定为内联函数。

内联函数在编译时,会直接展开函数体到主函数中编译,因此可以提升程序的运行效率。一般将代码长度较小(5行以内,不能包含复杂的控制语句)且频繁使用的函数写为内联函数。

#include <iostream>using namespace std;// 声明函数void test();// 定义函数inline void test(){    cout << "aaa" << endl;}int main(){    test();    return 0;}

所有的成员函数默认为内联函数,无需手动使用inline修饰。

5.2 函数重载 overload

C++中允许使用同一个函数名臣定义多个函数,这就是函数重载。函数重载的前提是各个重载的函数之间参数(类型或个数)不同,与返回值类型无关。

#include <iostream>using namespace std;void show(){    cout << "1" << endl;}void show(int a){    cout << "2" << a << endl;}void show(double d){    cout << "2.5" << d << endl;}void show(string s){    cout << "3" << s << endl;}void show(string s1,string s2){    cout << "4" << s1 << s2 << endl;}int main(){    show(1); // 21    show(3.14); // 2.53.14    show("Hello"); // 3Hello    show(); // 1    show("AA","BB"); // 4AABB    return 0;}

除了上述的普通函数支持重载外,成员函数和构造函数等也支持函数重载,但是析构函数不支持函数重载。

5.3 函数的参数默认值(缺省值)

C++中允许给函数的参数设定默认值,在调用函数时,如果传入参数,则传入的参数会覆盖默认值;如果不传入参数,则使用默认值作为参数值。

参数的默认值只允许在声明和定义中出现一次。

#include <iostream>using namespace std;void func1(int a = 0); // 声明时制定参数默认值void func2(int a);void func1(int a){    cout << a << endl;}void func2(int a = 1) // 定义时制定参数默认值{    cout << a << endl;}int main(){    func1(); // 0    func1(666); // 666    func2(); // 1    func2(888); // 888    return 0;}

向右(后)原则:如果函数参数有多个,此时给某个参数设定了默认值后,其右边(后边)所有的参数都必须设定默认值。

#include <iostream>using namespace std;void method1(int a,int b=1,int c=2){    cout << a << b << c << endl;}int main(){//    method1(); 错误    method1(0); // 012    method1(0,0); // 002    return 0;}

当函数的参数默认值与函数重载同时使用时,非常容易导致二义性问题(ambiguous),二义性问题指的是编译器无法做出两种情况的抉择。

尽量不要同时使用函数重载和默认值。

#include <iostream>using namespace std;void method(int a=0,int b=1,int c=2){    cout << a << b << c << endl;}void method(){    cout << "aaa" << endl;}int main(){//    method(); 错误    return 0;}

5.4 哑元函数

如果一个函数的参数只有类型,没有名字,这个参数在函数体中无法使用,这样的参数被称为哑元,这样的函数被称为哑元函数。

#include <iostream>using namespace std;/** * @brief test 哑元函数 */void test(string){    cout << "hahah" << endl;}int main(){    test("Tom"); // hahah    return 0;}

哑元函数的主要有以下用途:

● 区分重载函数,后面在运算符重载中使用。

● 保持函数的向前兼容性

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值