栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++从头捡起——2. 零散的知识

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++从头捡起——2. 零散的知识

文章目录
  • 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++在线编辑器

1. typename和using

参考:

  • using, typename的用法, typedef、using, remove_cv,void_t
  • 在C++中,using声明语句=后面的typename起什么作用?
  • Using typename的理解
2. 从txt文本里读取自定义的struct变量

参考:

  • Reading file into array of struct c++
    • example
  • c plus plus网站:
    • 函数定义:https://cplusplus.com/reference/fstream/ifstream/
    • 函数示例:https://cplusplus.com/doc/tutorial/files/

传递结构体作为函数参数

  • C++中结构体作为函数参数的使用
3. STL的vector

参考:

  • CSDN博客:std::vector使用简介
  • c中文网:C++ STL vector添加元素(push_back()和emplace_back())详解
4. 跨平台 4.0 C++标准数据类型数据范围
数据类型存储空间/字节数取值范围
char1字节默认-128 到 127 ,编译时使用/j则是0 到 255
unsigned char1字节0 到 255
signed char1字节-128 到 127
int4 字节-2147483648 到 2147483647
unsigned int4 字节0 到 4294967295
signed int4字节-2147483648 到 2147483647
short 别名:short int/signed short int2 字节-32768 到 32767
unsigned short int2 字节0 到 65,535
float4 字节精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double8 字节双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)

参考:

  • 菜鸟教程:C++ 数据类型
    • 上面的部分内容翻译自:Data Type Ranges
  • 类似于菜鸟教程的网站还有:
    • C++ Data Types
    • geeksforgeeks: C++ Data Types
4.1 标准数据类型名称

可以在visual studio中对类似uint8_t的标识符进行右击,就会跳转到stdint.h这个头文件中,就会看到类似于下面的代码

#include 
typedef 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等数据类型
4.2 visual studio程序跨平台运行

常规情况下,visual studio写的程序都是在windows上编译的。

但是感谢如何使用Visual Studio 2017作为Linux C++开发工具

  • 我以前以为visual studio可以直接选择linux的编译环境
  • 但是其实都是连接远程linux环境进行调试的
  • 具体使用需要进行一些配置。

详见:

  • 如何使用Visual Studio 2017作为Linux C++开发工具
  • Visual C++ for Linux Development with CMake
5. 通用功能代码片段

之前就觉得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
5.2 测量某段C++代码执行时间

参考:

  • 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++篇)
5.3 C++终止程序执行的库函数

示例:

#include 
using 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++终止程序执行的三个函数
5.4 C++中bool转int

参考:

  • Bool to int conversion in C++
6. C++风格指南/开发规范 6.1 文字版

一般都用Google的C++开发规范:

  • 英语原文链接:Google C++ Style Guide
  • 中文在线文档:Google 开源项目风格指南——中文版
  • 离线pdf文档:Google 开源项目风格指南(中文版v0.3)

还有一些精简版的C++开发规范:

  • Google C++编码规范学习
  • 关于代码风格,个人推荐Google C++ Style Guide
6.2 代码加注释的配图

除了这种文字说明,还找到了一个非常贴心的格式说明,直接一张图,代码旁边标注,非常清晰。

感谢一张图总结Google C++编程规范(Google C++ Style Guide)

6.3 一些说明

文件

  1. 编码是UTF8
  2. LineEnd是LF,文件批量修改用dos2unix

其中关于第2点的说明,可以参考:

  • 知乎问题:为什么大多开源项目的.editorconfig总是设置end_of_line=lf而不是 crlf?
  • CSDN文章:关于脚本文件(文本文件)的 Line Endings
其它 C++在线编辑器

有时候想快速测试某段代码,可以直接用在线版本。感谢

推荐10个好用的C++在线编译器,去网吧学习不用配置环境了

我就记住这一个就行

  • https://www.cainiaojc.com/tool/cpp/
  • https://cppinsights.io/ (不太会用,std::out东西出不来)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1037679.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号