当前位置:网站首页>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;
}
}
边栏推荐
- LeetCode练习——跳跃游戏、组合求和
- 第 2 篇:繪制一個窗口
- Cloud development who is the source code of undercover applet
- 1-4metasploitable2介绍
- 3-list introduction
- Keep one decimal place and two decimal places
- Live wire, neutral wire and ground wire. Do you know the function of these three wires?
- Take my brother to make a real-time Leaderboard
- Hilbert Huang Transform
- 3-列表简介
猜你喜欢

单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案

Screenshot recommendation - snipaste

Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its

第 2 篇:绘制一个窗口

GraphMAE----论文快速阅读

How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect

exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的

Oracle advanced SQL qualified query

Ad-gcl:advantageous graph augmentation to improve graph contractual learning

The two most frequently asked locks in the interview
随机推荐
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?
auto使用示例
Thread considerations
GraphMAE----論文快速閱讀
ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决
Using kubeconfig files to organize cluster access
. No main manifest attribute in jar
The startup mode of cloudbase init is \Cloudbase init has hidden dangers
第 2 篇:绘制一个窗口
一文理解同步FIFO
Part 2: drawing a window
某问答社区App x-zse-96签名分析
Chrono usage notes
报错“Computation failed in `stat_summary_hex()`”
Echart 心得 (一): 有关Y轴yAxis属性
[C language] system date & time
Thread support
闲谈:3AC到底发生了什么?
GraphMAE----论文快速阅读