C++ Attentions
1. switch内部的变量定义
C++语言规定,不允许跨过变量的初始化语句直接跳转到该变量作用域内的另一个位置。
#include <iostream>int main()
{bool switch_on = false;switch (switch_on){case true:int j;j = 110;std::cout << j << std::endl;// 运行此句必须有上面j=110的初始化break;case false:j = 120;std::cout << j << std::endl;// 运行此句必须有上面j=120的初始化,跟上一个case中有没有执行j=110的初始化没关系break;default:break;}
}
下面的写法是错误的,
错误形式一:
#include <iostream>int main()
{bool switch_on = false;switch (switch_on){case true:int j;//j = 110;std::cout << j << std::endl;break;case false://j = 120;std::cout << j << std::endl;break;default:break;}
}
报错会是:
错误 2 error C4700: 使用了未初始化的局部变量“j”
错误形式二:
#include <iostream>
#include <string>
int main()
{bool switch_on = false;switch (switch_on){case true:int j = 110; // 错误,控制流绕过一个显式初始化的变量std::cout << j << std::endl;std::string file_name; // 错误,控制流绕过一个隐式初始化的变量break;case false://j = 120;//std::cout << j << std::endl;break;default:break;}
}
报错会是:
错误 1 error C2360: “j”的初始化操作由“case”标签跳过
错误 2 error C2361: “default”标签跳过“j”的初始化操作
2. 范围for语句
范围for语句的语法形式是:
for (declaration: expression)statement
expression
表示的必须是一个序列,比如初始值列表、数组、vector、string等类型的对象,这些类型的共同特点是拥有能返回迭代器的begin
、end
成员。
例如:
#include <iostream>
#include <typeinfo>
int main()
{auto alist = { 1, 2, 3, 4 }; // 类型是:class std::initializer_list<int>std::cout << typeid(alist).name() << std::endl; //输出class std::initializer_list<int>for (auto it : alist) // 如果想要修改alist的元素,可使用&it,然后再循环体中对it进行操作即可{std::cout << it << std::endl;}system("pause");
}
不能通过范围for循环增加、删除容器的元素,原因如下:
3. 声明一个返回数组指针的函数
举例说明: 方法一:
#include <iostream>int garr[10] = { 0 };int (*func(int a))[10] {for (auto &it : garr) {it += a;}return &garr;
}int main(){int a = 2;int (*it)[10] = func(a);for (int i = 0; i !=10; ++i ) {std::cout << (*it)[i] << " ";}std::cout << std::endl;
}
方法二:
#include <iostream>using arrT = int[10];
arrT garr = { 0 };
arrT* func(int a) {for (auto &it : garr) {it += a;}
return &garr;
}int main() {int a = 2;arrT *it = func(a);for (int i = 0; i != 10; ++i) {std::cout << (*it)[i] << " ";}std::cout << std::endl;
}
方法三:使用尾置返回类型
#include <iostream>int garr[10] = {0};auto func(int i) ->int(*)[10]{for (int & it : garr){it += i;}return &garr;
}int main(){int i = 2;auto result = func(i);for (int j = 0; j != 10; ++j){std::cout << (*result)[j] << " ";}std::cout << std::endl;
}
方法一、方法二、方法三输出的结果都是:2 2 2 2 2 2 2 2 2 2
。
方法四:使用decltype
#include <iostream>int garr_odd[] = {1,3,5};
int garr_even[] = {2,4,6};decltype(garr_odd) *func(int i){return (i % 2 == 0 ? &garr_even : &garr_odd);
}int main(){int i = 3;auto result = func(i);for (int j = 0; j != 3; ++j){std::cout << (*result)[j] << " ";}std::cout << std::endl;
}
输出结果是:1 3 5
。
C++ Attentions相关推荐
- Dynamic Head: Unifying Object Detection Heads with Attentions
Dynamic Head 论文标题:Dynamic Head: Unifying Object Detection Heads with Attentions 论文地址:https://arxiv.o ...
- Dynamic Head: Unifying Object Detection Heads with Attentions论文阅读
Dynamic Head: Unifying Object Detection Heads with Attentions论文阅读 摘要 介绍 相关工作 方法 Dynamic Head 扩展到现存的检 ...
- Dynamic Head Unifying Object Detection Heads with Attentions 论文阅读笔记
Dynamic Head Unifying Object Detection Heads with Attentions论文阅读笔记 这是微软在CVPR2021发表的文章,在coco数据集上取得了目前 ...
- Triformer: Triangular, Variable-Specific Attentions for Long Sequence Multivariate Time Series Fo...
Triformer: Triangular, Variable-Specific Attentions for Long Sequence Multivariate Time Series Fo... ...
- 11. Coupled multilayer attentions for co-extraction of aspect and opinion terms阅读笔记
Title: Coupled multilayer attentions for co-extraction of aspect and opinion terms 简称:CMLA 作者:Wenya ...
- 【论文笔记】Why Attentions May Not Be Interpretable?
Why Attentions May Not Be Interpretable? arxiv地址 工作背景 关于attention 机制的可解释性有很强的争议,所以本文作者试图找到影响attentio ...
- Dynamic Head :Unifying Object Detection Heads with Attentions
Dynamic Head :Unifying Object Detection Heads with Attentions 作者针对目标检测中通过backbone提取特征金字塔后的输出后,会形成最基 ...
- RASNet阅读笔记:Learning Attentions: Residual Attentional Siamese Network for High Performance Online Vis
CVPR2018 论文在此 文章全称:Learning Attentions: Residual Attentional Siamese Network for High Performance On ...
- CVPR 2018 RASNet:《Learning Attentions: Residual Attentional Siamese Network for Tracking》论文笔记
理解出错之处望不吝指正. 本文模型叫做RASNet,在Siamese框架下重构了CF,提出了三种attention机制(general.residual.channel),这三种attention的提 ...
- Dynamic Head: Unifying Object Detection Heads with Attentions 阅读
Abstract 这里就是说在目标检测领域,很多工作都想提高检测头的性能,这篇文章提出了动态头,也就是Dynamic Head,来将检测头和注意力(Attention)结合.在尺度(scale-awa ...
最新文章
- python set union_python – set.union()抱怨它在传入生成器时没有参数
- 流程图伪代码计算机语言,流程图与伪代码 PPT课件
- 包管理工具conda极简教程
- 异步生成器_使用生成器实现异步并等待
- 【数据操作】优化SQL server性能 之 索引 (转上)
- php 统计二维数组次数最多_前端面试题(数组篇)
- Flutter SwitchListTile 开关组件使用详情
- JavaScript字符串String常用方法介绍
- 伯克利推出世界最快的KVS数据库Anna:秒杀Redis和Cassandra
- 总结一下网站注入与防范的方法
- 报表制作1(传入对象和其他参数)
- 自己组装nas服务器万兆,我用最便宜的方法,配了一套万兆 NAS
- linux中rpm -q命令,Linux中的RPM
- 电脑重装系统后DirectX12旗舰版禁用了怎么解决?
- 游戏音效制作中难or不难?
- 大数据知识面试题-Flink(2022版)
- 99_包(package)
- WHERE EXISTS
- PMP的一个新术语:关键链法
- 「计算机基础」进制转换