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循环增加、删除容器的元素,原因如下:
![ellipse](https://i-blog.csdnimg.cn/blog_migrate/be361c067625861a98f9d8027360c99c.jpeg)
图1 容器操作可能使迭代器失效
3. 声明一个返回数组指针的函数
![ellipse](https://i-blog.csdnimg.cn/blog_migrate/fd59eaa42541c2051f7560d34827056a.png)
图2函数返回数组指针
举例说明: 方法一:
#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;
}
方法三:使用尾置返回类型
![ellipse](https://i-blog.csdnimg.cn/blog_migrate/6d97bdbd8f33db3222fd387ec3b6ec32.png)
图3使用尾置返回类型
#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
![ellipse](https://i-blog.csdnimg.cn/blog_migrate/aeb13d449091d3147eb5d8bdcc64a86b.png)
图4使用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
。