Use the auto keyword in C++ STL

后端 未结 6 1693
野趣味
野趣味 2020-12-23 00:10

I have seen code which use vector,

vectors;
s.push_back(11);
s.push_back(22);
s.push_back(33);
s.push_back(55);
for (vector::iterator i         


        
6条回答
  •  旧时难觅i
    2020-12-23 00:26

    The auto keyword gets the type from the expression on the right of =. Therefore it will work with any type, the only requirement is to initialize the auto variable when declaring it so that the compiler can deduce the type.

    Examples:

    auto a = 0.0f;  // a is float
    auto b = std::vector();  // b is std::vector()
    
    MyType foo()  { return MyType(); }
    
    auto c = foo();  // c is MyType
    

提交回复
热议问题