- 1. typename和using
- 2. 从txt文本里读取自定义的struct变量
- 3. STL的vector
- 4. 跨平台
- 4.0 C++标准数据类型数据范围
- 4.1 标准数据类型名称
- 4.2 visual studio程序跨平台运行
- 5. 通用功能代码片段
- 5.1 argv接受命令行传递参数
- 5.1.1 visual studio中设置命令行传递的参数
- 5.2 测量某段C++代码执行时间
- 5.3 C++终止程序执行的库函数
- 5.4 C++中bool转int
- 6. C++风格指南/开发规范
- 6.1 文字版
- 6.2 代码加注释的配图
- 6.3 一些说明
- 其它
- C++在线编辑器
参考:
- using, typename的用法, typedef、using, remove_cv,void_t
- 在C++中,using声明语句=后面的typename起什么作用?
- Using typename的理解
参考:
- Reading file into array of struct c++
- example
- c plus plus网站:
- 函数定义:https://cplusplus.com/reference/fstream/ifstream/
- 函数示例:https://cplusplus.com/doc/tutorial/files/
传递结构体作为函数参数
- C++中结构体作为函数参数的使用
参考:
- CSDN博客:std::vector使用简介
- c中文网:C++ STL vector添加元素(push_back()和emplace_back())详解
| 数据类型 | 存储空间/字节数 | 取值范围 |
|---|---|---|
| char | 1字节 | 默认-128 到 127 ,编译时使用/j则是0 到 255 |
| unsigned char | 1字节 | 0 到 255 |
| signed char | 1字节 | -128 到 127 |
| int | 4 字节 | -2147483648 到 2147483647 |
| unsigned int | 4 字节 | 0 到 4294967295 |
| signed int | 4字节 | -2147483648 到 2147483647 |
| short 别名:short int/signed short int | 2 字节 | -32768 到 32767 |
| unsigned short int | 2 字节 | 0 到 65,535 |
| float | 4 字节 | 精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字) |
| double | 8 字节 | 双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字) |
参考:
- 菜鸟教程:C++ 数据类型
- 上面的部分内容翻译自:Data Type Ranges
- 类似于菜鸟教程的网站还有:
- C++ Data Types
- geeksforgeeks: C++ Data Types
可以在visual studio中对类似uint8_t的标识符进行右击,就会跳转到stdint.h这个头文件中,就会看到类似于下面的代码
#includetypedef signed char int8_t; typedef short int16_t; typedef int int32_t; typedef long long int64_t; typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; typedef unsigned long long uint64_t;
其中,关于unsigned char,不太有印象的可以看看
- 百度百科——unsigned char
- 百度百科-char
参考:
- C++规范:Fixed width integer types (since C++11)
- CSDN博客:int8_t、int16_t、int32_t、int64_t、uint8_t、size_t、ssize_t区别
- 知乎文章:C语言尽量使用int8_t int64_t等数据类型
常规情况下,visual studio写的程序都是在windows上编译的。
但是感谢如何使用Visual Studio 2017作为Linux C++开发工具
- 我以前以为visual studio可以直接选择linux的编译环境
- 但是其实都是连接远程linux环境进行调试的
- 具体使用需要进行一些配置。
详见:
- 如何使用Visual Studio 2017作为Linux C++开发工具
- Visual C++ for Linux Development with CMake
之前就觉得python添加参数/配置就挺麻烦的,现在看来,C++更麻烦,哈哈哈。
5.1 argv接受命令行传递参数 5.1.1 visual studio中设置命令行传递的参数根据在VS中向命令行添加参数的方法
右击项目,选择属性,找到配置属性->调试,在命令参数中添加要传递的args参数
参考:
- stackoverflow:Use command line arguments argv to create data file with the same name
- 一个C编程教学系列网站:Command line arguments in C++ using argc and argv
参考:
- geeksforgeeks:Measure execution time of a function in C++
- stackoverflow:Measuring execution time of a function in C++
- stackoverflow:How to Calculate Execution Time of a Code Snippet in C++
- 8 Ways to Measure Execution Time in C/C++
- 知乎:测量程序运行时间(C++篇)
示例:
#includeusing namespace std; main() { bool my_bool; my_bool = true; cout << "The int equivalent of my_bool is: " << int(my_bool) << endl; my_bool = false; cout << "The int equivalent of my_bool is: " << int(my_bool); }
输出:
The int equivalent of my_bool is: 1 The int equivalent of my_bool is: 0
参考:
- CSDN博客:C++终止程序执行的三个函数
参考:
- Bool to int conversion in C++
一般都用Google的C++开发规范:
- 英语原文链接:Google C++ Style Guide
- 中文在线文档:Google 开源项目风格指南——中文版
- 离线pdf文档:Google 开源项目风格指南(中文版v0.3)
还有一些精简版的C++开发规范:
- Google C++编码规范学习
- 关于代码风格,个人推荐Google C++ Style Guide
除了这种文字说明,还找到了一个非常贴心的格式说明,直接一张图,代码旁边标注,非常清晰。
感谢一张图总结Google C++编程规范(Google C++ Style Guide)
6.3 一些说明文件
- 编码是UTF8
- LineEnd是LF,文件批量修改用dos2unix
其中关于第2点的说明,可以参考:
- 知乎问题:为什么大多开源项目的.editorconfig总是设置end_of_line=lf而不是 crlf?
- CSDN文章:关于脚本文件(文本文件)的 Line Endings
有时候想快速测试某段代码,可以直接用在线版本。感谢
推荐10个好用的C++在线编译器,去网吧学习不用配置环境了
我就记住这一个就行
- https://www.cainiaojc.com/tool/cpp/
- https://cppinsights.io/ (不太会用,std::out东西出不来)



