首页 / C++ / 复习C++语法--基础篇
复习C++语法--基础篇
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了复习C++语法--基础篇,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3590字,纯文字阅读大概需要6分钟。
内容图文
![复习C++语法--基础篇](/upload/InfoBanner/zyjiaocheng/631/2aab8dd8278d4552ae9322d4e4273c09.jpg)
1,生成一个C++程序共有三个步骤。首先,代码在预处理器中运行,预处理器会识别代码中的元信息。其次,代码被编译或转换成计算机可识别的目标文件。最后,独立的目标文件被链接在一起变成一个应用程序。
2,名称空间
定义:namespace mycode { void foo(); }
实现:void mycode::foo() { ... }
使用方法:a, using namespace mycode; foo(); b, using mycode::foo; foo();
C++17中的名称空间嵌套:namespace MyLib::NewWorking::FTP { ... }
给名称空间起别名:namespace MyFTP = MyLib::NewWorking::FTP;
3,类型转换
float myFloat = 3.14f;
int i3 = static_cast<int>(myFloat);
4,强类型的enum class:
定义:enum class PieceType { King = 1, Queen, Rook = 10, Pawn };
使用:PieceType piece = PieceType::King;
枚举值不会自动转换为整数:if (PieceType::Queen == 2) { ... } // 这段代码不合法
可以这样改变枚举值类型(默认是整型):
enum class PieceType : unsigned long { King = 1, Queen, Rook = 10, Pawn };
5,if 语句的初始化器
if ( Employee employee = GetEmployee(); employee.salary > 1000 ) { ... }
switch 语句也有这种用法
6,数组与容器
array<int, 3> arr = { 1, 2, 3 };
vector<int> myArr = { 11, 22, 33 };
7,基于区间的for循环
std::array<int, 4> arr = { 1, 2, 3, 4 };
for ( int i : arr ) { std::cout << i << std::endl; }
8,初始化列表
利用初始化列表可轻松地编写能接收可变数量参数的函数
#include <initializer_list>
using namespace std;
int makeSum( initializer_list<int> lst )
{
int total = 0;
for ( int value : lst ) { total += value }
return total;
}
int c = makeSum( {1, 2, 3, 4, 5} );
9,空指针在布尔表达式中使用时会被转换为false,对null或未初始化的指针解除引用会导致不可确定行为
10,动态分配数组
int arrSize = 8;
int* myVariableSizeArr = new int[arrSize];
指针变量仍在堆栈中,但动态创建的数组在堆中
11,C++11之前,常量NULL用于表示空指针,将NULL定义为常量0会导致一些问题,应使用nullptr;
12,智能指针
std::unique_ptr
auto employee = make_unique<Employee>();
auto employee = make_unique<Employee[]>(10); employee[0].salary = 1000;
std::shared_ptr
auto employee = make_shared<Employee>();
shared_ptr<Employee[]> employee(new Employee[10]);
13,引用
int x = 32;
int& xReference = x;
按引用传参的函数传递字面量是会编译错误
14,异常
#include <stdexcept>
double divideNumbers(double numerator, double denominator)
{
if ( denominator == 0 )
throw invalid_argument("Denominator cannot be 0.");
return numerator / denominator
}
try {
cout << divideNumbers(2.5, 0.5) << endl;
cout << divideNumbers(3.2, 0) << endl;
cout << divideNumbers(5.2, 1.2) << endl; // 第二句报错,此句不会执行
} catch (const invalid_argument& exception) {
cout << "Exception caught: " << exception.what() << endl;
}
15,类型推断
auto: auto去除了引用和const限定符,会创造副本!如果不需要副本,可使用auto& 或 const auto&
decltype: 把表达式作为实参,计算出该表达式的类型,decltype不会除了引用和const限定符!int x = 123; decltype(x) y = 234;
16,类与对象,如果构造函数只是初始化数据成员而不做其他事,实际上没有必要使用构造函数,因为可在类定义中直接初始化数据成员
17,统一初始化
CircleStruct myCircle1 = { 10, 10, 2.5 };
CircleClass myCircle2 = { 10, 10, 2.5};
CircleStruct myCircle3 { 10, 10, 2.5 };
CircleClass myCircle4 { 10, 10, 2.5}; // 这四句一样效果
并不局限于结构体和类,例如:
int a = 3;
int b(3);
int c = {3};
int d{3}; // 这四句效果也一样
统一初始化还可用于将变量初始化为0,只需要指定对应的空大括号:int e{};
统一初始化还可以阻止C++隐式的窄化,如:
int x = 3.14; // 有些编译器只会发出警告甚至没有警告
int x = {3.14}; // 这样编译会报错
统一初始化还可以动态分配数组:int* pArr = new int[4]{1, 2, 3, 4};
18,直接列表初始化与复制列表初始化
从C++17开始:
// 复制列表初始化
auto a = {11};
auto b = {11, 22};
// 直接列表初始化
auto c {11};
auto d {11, 22}; // 报错,太多元素
内容总结
以上是互联网集市为您收集整理的复习C++语法--基础篇全部内容,希望文章能够帮你解决复习C++语法--基础篇所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。