当前位置:网站首页>Auto usage example

Auto usage example

2022-06-24 08:00:00 GarryLau

Use auto Why :
A. The type name is too long auto convenient ,std::vector<int> vec{5,6,7}; auto it = vec.begin();
B. An elusive type , Such as lambda,auto lamb = [](){};

auto A brief description of the rules of inference :

  1. Generally, we can infer from the expression ;
  2. The compound type , Infer a reference 、 The pointer , Will retain const;
  3. Non reference 、 Non pointer , Will ignore const;
  4. Use... For arrays auto Will get the pointer , And decltype Different .

main.cpp

#include <typeinfo>
#include <iostream>

namespace test_auto {
    
    auto testRangeForAuto() -> void {
    
        //  Preliminary knowledge , An array reference 
        int a[3][4] = {
    0,1,2,3};
        int (&arr_ref)[4] = a[2];
        for(auto &i : arr_ref){
    
            i = -99;
        }
        std::cout << "using array reference...\n";
        for(const auto &row : a){
    
            for(auto col : row){
    
                std::cout << col << std::endl;
            }
        }
        
        //  Using range for Statement handles multidimensional arrays , Except for the innermost cycle , All other loop control variables should be of reference type 
        std::cout << "Entering testRangeForAuto()...\n";
        for(auto &row : a){
                // row yes a Every element ( Every element is int[4]) References to 
            for(auto &col : row){
          // col yes int type 
                col += 1;
            }
        }
        
        for(const auto &row : a){
    
            for(auto col : row){
    
                std::cout << col << std::endl;
            }
        }
        
        /* for(auto row : a){ //  That's how it's written row Would be int*, The next line is right int* do range-for It's not appropriate  for(auto col : row){} // error: 'end' was not declared in this scope; did you mean 'std::end'? } */
    }
    
    auto main() -> int {
    
        std::cout << "testing auto......" << std::endl;

        int i = 19;
        const int ci = 18;

        //  Basic types ,auto Will ignore const
        auto a = i * i;
        auto b = ci;
        const auto c = ci;

        // auto When referenced , Will retain const
        auto &d = i;
        d = 10;          // [RIGHT]

        auto &e = ci;
        // e = 3; // error C3892: “e”:  Constant cannot be assigned 

        // auto When you get the pointer , Will retain const
        auto f = &i;
        auto g = &ci;
        // *g = 88; // error C3892: “g”:  Constant cannot be assigned 
        g = &i;          // [RIGHT],g Is the underlying pointer , Can point to different variables but not through g Modify the value of the pointed variable 
        // *g = 78; // error C3892: “g”:  Constant cannot be assigned 


        // other test
        int * const q1 = &i;
        const int * const q2 = &i;
        int const * q3 = &i;  // q3 and q4 Are all bottom pointers , The two ways of writing are the same 
        const int * q4 = &i;

        std::cout << "type(a) is: " << typeid(a).name() << std::endl;   // int
        std::cout << "type(b) is: " << typeid(b).name() << std::endl;   // int
        std::cout << "type(c) is: " << typeid(c).name() << std::endl;   // int
        std::cout << "type(d) is: " << typeid(d).name() << std::endl;   // int
        std::cout << "type(e) is: " << typeid(e).name() << std::endl;   // int
        std::cout << "type(f) is: " << typeid(f).name() << std::endl;   // int *
        std::cout << "type(g) is: " << typeid(g).name() << std::endl;   // int const *

        std::cout << "type(q1) is: " << typeid(q1).name() << std::endl;   //  the truth is that int * const, The compiler only writes int *
        std::cout << "type(q2) is: " << typeid(q2).name() << std::endl;   //  the truth is that int const * const, The compiler only writes int const *
        std::cout << "type(q3) is: " << typeid(q3).name() << std::endl;   // int const *
        std::cout << "type(q4) is: " << typeid(q4).name() << std::endl;   // int const *

        //  Use... For arrays auto
        int arr[] = {
    3,4,5};
        auto brr(arr);    // auto Array gets a pointer , therefore range-for When accessing multidimensional arrays in, you need to use references to all layers except the innermost layer , See testRangeForAuto()
        std::cout << "type(brr) is: " << typeid(brr).name() << std::endl;  // int *

        auto &ref_arr(arr);      // ref_arr yes arr References to 
        std::cout << "type(ref_arr) is: " << typeid(ref_arr).name() << std::endl;  // int [3]
        ref_arr[1] = 888;
        std::cout << "arr[1] = " << arr[1] << std::endl;
        
        // decltype(arr) crr = {5,6,7,8,9}; // error: too many initializers for 'int [3]'
        decltype(arr) drr = {
    5,6,7};           //  Be careful , The number of array elements is part of the array type 
        std::cout << "type(drr) is: " << typeid(drr).name() << std::endl;   // int [3]

        testRangeForAuto();

		std::cout << "test_auto pass" << std::endl;
		std::cout << "------------------------------" << std::endl;

        return 0;
    }
}
原网站

版权声明
本文为[GarryLau]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240421274668.html