当前位置:网站首页>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 :
- Generally, we can infer from the expression ;
- The compound type , Infer a reference 、 The pointer , Will retain const;
- Non reference 、 Non pointer , Will ignore const;
- 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;
}
}
边栏推荐
- Hongmeng development IV
- Graphmae - - lecture rapide des documents
- Standing at the center of the storm: how to change the engine of Tencent
- 行内元素、块元素、行内块元素
- exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的
- 运行npm run eject报错解决方法
- How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
- 关于h5页面苹果手机使用fixed定位tabbar最底部时遮挡内容问题
- Notes on the use of date and time base
- Moonwell Artemis is now online moonbeam network
猜你喜欢

What kind of experience is it when the Institute earns 20000 yuan a month!

Latest news of awtk: new usage of grid control
![[C language] system date & time](/img/de/faf397732bfa4920a8ed68df5dbc48.png)
[C language] system date & time

GraphMAE----论文快速阅读

云开发谁是卧底小程序源码

Examples of corpus data processing cases (reading multiple text files, reading multiple files specified under a folder, decoding errors, reading multiple subfolder text, batch renaming of multiple fil

ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决

免费ICP域名备案查接口

GPU is not used when the code is running

Graphmae ---- quick reading of papers
随机推荐
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
Live wire, neutral wire and ground wire. Do you know the function of these three wires?
Keep one decimal place and two decimal places
[data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board
Duilib display memory picture
[测试开发]初识软件测试
Chapter 2 line graph of canvas
Backup and restore SQL Server Databases locally
1-4metasploitable2介绍
Redolog and binlog
Cloud development who is the source code of undercover applet
第 3 篇:绘制三角形
火线,零线,地线,你知道这三根线的作用是什么吗?
Notes on the use of date and time base
Introduction of model compression tool based on distiller
Q & A on cloud development cloudbase hot issues of "Huage youyue phase I"
位运算
Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible
5-if语句(选择结构)
[run the script framework in Django and store the data in the database]