C++_day2

[复制链接]
发表于 2025-12-31 07:36:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目次
  1. 引用 reference(重点)
  1.1 底子使用
  1.2 特性
  1.3 引用参数
  2. C++窄化(相识)
  3. 输入(熟悉)
  4. string 字符串类(把握)
  4.1 底子使用
  4.2 取出元素
  4.3 字符串与数字转换
  5. 函数
  5.1 内联函数 inline(把握)
  5.2 函数重载 overload(把握)
  5.3 函数的参数默认(缺省)值(把握)
  5.4 哑元函数(熟悉)
  

         
本章紧张解说非面向对象的升级。

   

   1. 引用 reference(重点)

   
   1.1 底子使用

   
   
    引用就是某个变量或常量的别名,对引用举利用用与使用原变量或常量完全雷同。

   
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a = 1;
  6.     int& b = a; // b是a的引用
  7.     b++;
  8.     cout << a << " " << b << endl; // 2 2
  9.     cout << &a << " " << &b << endl; // 0x61fe88 0x61fe88
  10.     return 0;
  11. }
复制代码

       1.2 特性

   
   

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

   
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a = 1;
  6.     int& b = a; // b是a的引用
  7.     int c = 8;
  8.     b = c; // 仅仅是赋值,b仍然是a的别名
  9.     cout << a << " " << b << " " << c << endl; // 8 8 8
  10.     cout << &a << endl; // 地址1
  11.     cout << &b << endl; // 地址1
  12.     cout << &c << endl; // 地址2
  13. //    &b = c; 错误
  14. //    int& b = c; 错误
  15.     return 0;
  16. }
复制代码

      

  • 声明引用时,必须初始化。
  

   

   

   

  • 声明引用时,初始化的值不能为NULL。
  

   

   

   

  • 声明引用时,初始化的值可以是纯数值,但是此时引用须要使用const修饰,表现常引用,不允许修改引用值。
  

  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //    int& b = 123; 错误
  6.     const int& b = 123; // 常引用
  7.     cout << b << endl; // 123
  8. //    b++; 错误
  9.     return 0;
  10. }
复制代码
  
               

  • 可以将变量引用的地点赋值给一个指针,此时指针指向的照旧原来的变量。
         
  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a = 1;
  6.     int &b  = a;
  7.     int* c = &b;
  8.     cout << *c << endl; // 1
  9.     cout << c << " " << &a << endl; // 0x61fe84 0x61fe84
  10.     return 0;
  11. }
复制代码
  
               

  • 可以创建指针变量的引用。
         
  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a = 1;
  6.     int* b = &a;
  7.     int*& c = b; // c是b的引用
  8.     cout << c << " " << b << " " << endl; // 0x61fe88 0x61fe88
  9.     cout << *c << endl; // 1
  10.     return 0;
  11. }
复制代码
  
               

  • 可以使用const修饰引用,表现常引用,此时不允许改变引用的值,但是可以改变原变量。
         
  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a = 1;
  6.     const int& b = a; // b是a的常引用
  7. //    b++; 错误
  8.     a++;
  9.     cout << b << endl; // 2
  10.     return 0;
  11. }
复制代码
  
               1.3 引用参数

     
     
【思索题】写一个函数,功能为互换两个int变量参数的数值。

         
  
  1. #include <iostream>
  2. using namespace std;
  3. /**
  4. * @brief swap1 错误的交换方式
  5. */
  6. void swap1(int a,int b)
  7. {
  8.     a = a ^ b;
  9.     b = a ^ b;
  10.     a = a ^ b;
  11.     cout << a << endl; // 2
  12.     cout << b << endl; // 1
  13. }
  14. /**
  15. * @brief swap2 C的交换方式
  16. * 指针比较复杂繁琐
  17. */
  18. void swap2(int* a,int* b)
  19. {
  20.     *a = *a ^ *b;
  21.     *b = *a ^ *b;
  22.     *a = *a ^ *b;
  23. }
  24. /**
  25. * @brief swap3 使用引用参数传递,方便简洁
  26. */
  27. void swap3(int& a,int& b)
  28. {
  29.     a = a ^ b;
  30.     b = a ^ b;
  31.     a = a ^ b;
  32. }
  33. int main()
  34. {
  35.     int a = 1;
  36.     int b = 2;
  37.     swap1(a,b);
  38.     cout << a << endl; // 1
  39.     cout << b << endl; // 2
  40.     swap2(&a,&b);
  41.     cout << a << endl; // 2
  42.     cout << b << endl; // 1
  43.     swap3(a,b);
  44.     cout << a << endl; // 1
  45.     cout << b << endl; // 2
  46.     return 0;
  47. }
复制代码
  
               
    使用引用作为函数参数时,不产生副本,可以使参数通报的服从提升,相比指针又更加简便。

     
须要留意的是:

     
引用参数应该在内被const修饰的环境下,只管使用const修饰,以到达引用参数的安全性。

         
  
  1. #include <iostream>
  2. using namespace std;
  3. void show(const int& a,const int& b)
  4. {
  5.     cout << a << endl;
  6.     cout << b << endl;
  7. }
  8. int main()
  9. {
  10.     int a = 1;
  11.     int b = 2;
  12.     show(a,b);
  13.     cout << a << endl;
  14.     return 0;
  15. }
复制代码
  
               2. C++窄化(相识)

     
   
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     double a = 123234.8;
  6.     int b = (int)a; // C强制类型转换格式
  7.     int c(a); // C++格式
  8.     cout << b << endl;
  9.     cout << c << endl;
  10.     int d{a}; // C++11格式:可以给出数据窄化警告
  11.     cout << d << endl;
  12.     return 0;
  13. }
复制代码
   
                       3. 输入(熟悉)

      
      
在C++中使用cin举行键盘输入,与cout一样支持连续使用。

      

      
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a;
  6.     double b;
  7.     string c; // C++的字符串类型
  8.     cout << "依次输入整型、浮点型与字符串类型:" << endl;
  9.     // 连续输入三个数据,分别给abc三个变量
  10.     cin >> a >> b >> c;
  11.     cout << "您输入的数据是:" << endl;
  12.     cout << a << endl;
  13.     cout << b << endl;
  14.     cout << c << endl;
  15.     return 0;
  16. }
复制代码
      
                              
假如想输入空格,可以使用getline函数,可以一次得到一行的数据:

                     
      
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     string s;
  6.     cout << "请输入一行字符串,可以包含空格:" << endl;
  7.     getline(cin,s);
  8.     cout << "您输入的数据是:" << endl;
  9.     cout << s << endl;
  10.     return 0;
  11. }
复制代码
      
                               4. string 字符串类(把握)

         
         4.1 底子使用

         
         
string不是C++本身的根本数据范例,而是在C++标准库std中的一个字符串类。

         
在使用时须要引入头文件<string>,而不是<string.h>,string用于在绝大多数环境下代替char*,不必担心内存是否充足、字符串长度等标题。

         
string内部集成的函数可以完成绝大多数环境下字符串的使用。

         

         
string支持多种遍历方式:

         

  • for循环
  • for-each循环(C++11)
  • 迭代器(背面讲)
        

        
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     string s = "fsdhfjdhsjkf";
  6.     // size和length效果完全相同
  7.     cout << "字符串长度:" << s.size()
  8.          << " " << s.length() << endl;
  9.     cout << "for循环遍历:" << endl;
  10.     for(int i = 0;i<s.size();i++)
  11.     {
  12.         cout << s[i] << " ";
  13.     }
  14.     cout << endl << "for-each遍历:" << endl;
  15.     for(char i:s)
  16.         cout << i << " ";
  17.     return 0;
  18. }
复制代码
        
                                       4.2 取出元素

           

           
    在C++中除了使用[]取出元素外,还可以使用at函数取出元素。

           
    在绝大多数环境下,更保举使用at函数取出元素,由于at函数更安全,但是[]性能更好。

           

         
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     string s = "fsdhfjdhsjkf";
  6.     cout  << s[1] << endl; // 's'
  7.     // at函数
  8.     cout << s.at(1) << endl; // 's'
  9. //    cout << s[-100] << endl; // '\0'或乱码
  10.     cout << s.at(-100) << endl; // 检测到越界则终止运行
  11.     cout << "主函数结束" << endl;
  12.     return 0;
  13. }
复制代码
         
                                               4.3 字符串与数字转换

            
            
    转换方式有很多种,在此使用的是字符串流。

            
    整数 → 字符串

            

            
  1. #include <iostream>
  2. #include <sstream> // 头文件
  3. using namespace std;
  4. int main()
  5. {
  6.     int i = 2323;
  7.     stringstream ss;
  8.     ss << i;
  9.     string s = ss.str();
  10.     cout << s << endl;
  11.     return 0;
  12. }
复制代码
            
                                                                                 
字符串 → 整数

           

         
  1. #include <iostream>
  2. #include <sstream> // 头文件
  3. using namespace std;
  4. int main()
  5. {
  6.     string s = "12345";
  7.     istringstream iss(s);
  8.     int i;
  9.     iss >> i;
  10.     cout << i << endl;
  11.     return 0;
  12. }
复制代码
         
                                               5. 函数

            
             5.1 内联函数 inline(把握)

            
            
    在C++中使用内联函数紧张是为了代替C语言中宏界说的函数,内联函数在编译时,可以直接睁开函数体到主函数中,因此提升了步调实行的服从,消除了平凡的额外开销。

            
    发起把代码长度较小(1-5行且不能包罗复杂的控制语句)且频仍使用的函数界说为内联函数,内联函数只是给编译器的发起,编译器并不愿定采取。

            

            
    只要在函数界说处使用inline关键字修饰函数,就可以把函数设置为内联函数。

            

            
  1. #include <iostream>
  2. using namespace std;
  3. /**
  4. * @brief test 内联函数
  5. */
  6. inline void test(int a)
  7. {
  8.     cout << "fdfd" << endl;
  9.     cout << ++a << endl;
  10. }
  11. void func(); // 函数声明
  12. inline void func() // 函数定义
  13. {
  14.     cout << "翻江倒海发的" << endl;
  15.     cout << "法国活动经费" << endl;
  16. }
  17. int main()
  18. {
  19.     int a = 1;
  20.     test(a);
  21.     func();
  22.     return 0;
  23. }
复制代码
            
                                                      
    须要留意的是,背面要学习的成员函数默认都界说为内联函数。

               

               5.2 函数重载 overload(把握)

               
               
    C++中允许同一个函数名称界说多个函数,这就是函数重载。

               

               
    函数署名是编译器区分差别函数的方式,包罗以下几个构成部分:

               

  • 函数名称
  • 参数数目
  • 参数范例
              
    两个函数的署名不可以雷同,即一个函数只有一个独一无二的署名。

               

               
     当函数署名中函数名称雷同,剩余条件差别时(参数数目或参数范例差别),就构成了函数重载。

               

              
  1. #include <iostream>
  2. using namespace std;
  3. void test()
  4. {
  5.     cout << "没有参数" << endl;
  6. }
  7. void test(int a)
  8. {
  9.     cout << "一个int参数" << a << endl;
  10. }
  11. void test(int a,int b)
  12. {
  13.     cout << "两个int参数" << a+b << endl;
  14. }
  15. void test(string a,string b)
  16. {
  17.     cout << "两个string参数" << a+b << endl;
  18. }
  19. //int test(string a,string b) 错误
  20. //{
  21. //    cout << "两个string参数" << a+b << endl;
  22. //    return 1;
  23. //}
  24. int main()
  25. {
  26.     test(1,3);
  27.     test(54);
  28.     test();
  29.     test("aa","bb");
  30.     return 0;
  31. }
复制代码
              
                                                               
背面学习的函数中,须要留意的是:构造函数可以重载,但是析构函数不能重载。

                 

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

                 
                 
    C++允许给函数的参数设定默认值,调用函数时假如传入参数,传入的参数会覆盖默认值;调用时假如不通报参数,参数使用预设的默认值。

                 
   函数的参数默认值既可以在声明处(发起)设定,又可以在界说处设定,但是只能出现一次。

                 
    向右(后)原则:假如函数的参数有多个,只要某个参数设定了默认值,其右边(背面)的全部参数都要设定默认值。

                 

               
  1. #include <iostream>
  2. using namespace std;
  3. void func(int a = 1)
  4. {
  5.     cout << a << endl;
  6. }
  7. void test(int a = 2);
  8. void test(int a)
  9. {
  10.     cout << a << endl;
  11. }
  12. void method(int a,int b = 1,int c = 2)
  13. {
  14.     cout << a << b << c << endl;
  15. }
  16. int main()
  17. {
  18.     func(); // 1
  19.     func(2); // 2
  20.     test(); // 2
  21.     test(1); // 1
  22.     method(6); // 612
  23.     method(6,6);  // 662
  24.     method(6,6,6); // 666
  25.     return 0;
  26. }
复制代码
               
                                                                       
只管不要同时使用函数重载和参数默认值,由于非常容易出现二义性标题。

                  

                  
  1. #include <iostream>
  2. using namespace std;
  3. void method(int a,int b = 1,int c = 2)
  4. {
  5.     cout << "A"  << a << b << c << endl;
  6. }
  7. void method(int a)
  8. {
  9.     cout  << "B" << a << endl;
  10. }
  11. int main()
  12. {
  13.     method(8,8,8); // A888
  14.     method(5,5); // A552
  15. //    method(9); 错误
  16.     return 0;
  17. }
复制代码
                  
                                                                               5.4 哑元函数(熟悉)

                     
                     
    一个函数的参数只有范例没著名字,这个参数就是哑元。包罗哑元的函数就是哑元函数。

                     

                    
  1. #include <iostream>
  2. using namespace std;
  3. void test(int)
  4. {
  5.     cout << "AAA" << endl;
  6. }
  7. int main()
  8. {
  9. //    test(); 错误
  10.     test(34897);
  11.     return 0;
  12. }
复制代码
                    
                                                                                       
哑元函数的实际功能包罗但不限于:

                       

  • 在C++的运算符重载中区分重载函数(背面讲)。
  • 保持函数的向前兼容性。
                     
......

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表