最近在看这个:LearnCpp

主要是一些我自己容易忽视的地方,因为原来的网站更新很快,所以章节序号不一定准


CH1.10 Preprocessing

The preprocessor copies the contents of the included file into the including file at the point of the #include directive

Conditional Compilation:

#ifndef SOME_UNIQUE_NAME_HERE
#define SOME_UNIQUE_NAME_HERE
#endif

CH2.1 Fundamental variable definition, initialization, and assignment

Favor implicit initialization over explicit initialization

int nValue = 5; // explicit initialization
int nValue(5); // implicit initialization

Uniform(list) initialization in C++11

int value{}; // default initialization to 0
int value{4.5}; // error: an integer variable can not hold a non-integer value

If you’re using a C++11 compatible compiler, favor uniform initialization


CH2.4 Integers

While short int, long int, and long long int are valid, the shorthand versions short, long, and long long should be preferred. In addition to being less typing, adding the prefix int makes the type harder to distinguish from variables of type int. This can lead to mistakes if the short or long modifier is inadvertently missed.

long long int lli; // valid
long long ll; // preferred

All integer variables except char are signed by default. Char can be either signed or unsigned by default (but is usually signed for conformity).

Generally, the signed keyword is not used (since it’s redundant), except on chars (when necessary to ensure they are signed).

Favor signed integers over unsigned integers


CH2.4a Fixed-width integers
<cstdint>:fixed width integers: int8_t/uint8_t/...uint64_t

Until this is clarified by a future draft of C++, you should assume that int8_t and uint8_t may or may not behave like char types.

int can be used when the integer size doesn’t matter and isn’t going to be large.

Fixed-width integers should be used in all other cases.

Only use unsigned types if you have a compelling reason.


CH2.5 Floating point numbers

Summation precision: Kahan summation algorithm

#include <iomanip> // for std::setprecision()

Float values have between 6 and 9 digits of precision, with most float values having at least 7 significant digits. Double values have between 15 and 18 digits of precision, with most double values having at least 16 significant digits. Long double has a minimum precision of 15, 18, or 33 significant digits depending on how many bytes it occupies.

Favor double over float unless space is at a premium, as the lack of precision in a float will often lead to challenges.

Rounding Error matters 0.1+...+0.1 \neq 1.0. Ref. CH3.5 -- Relational operators


CH2.7 Chars

Note that even though cin will let you enter multiple characters, ch will only hold 1 character. Consequently, only the first input character is placed in ch. The rest of the user input is left in the input buffer that cin uses, and can be accessed with subsequent calls to cin.

\n and endl:

Use std::endl when you need to ensure your output is output immediately (e.g. when writing a record to a file, or when updating a progress bar). Note that this may have a performance cost, particularly if writing to the output device is slow (e.g. when writing a file to a disk).

Use ‘\n’ in other cases.

wchar_t should be avoided in almost all cases (except when interfacing with the Windows API). Its size is implementation defined, and is not reliable. It has largely been deprecated.

You won’t need to use char16_t(UTF-16) or char32_t(UTF-32) unless you’re planning on making your program Unicode compatible and you are using 16-bit or 32-bit Unicode characters.


CH2.9 Const, constexpr, and symbolic constants

Making a function parameter const does two things. First, it tells the person calling the function that the function will not change the value of myValue. Second, it ensures that the function doesn’t change the value of myValue.

Any variable that should not change values after initialization should be declared as const (or constexpr in C++11).

Avoid using #define to create symbolic constants, but use const variables to provide a name and context for your magic numbers.

A recommended way:

  1. Create a header file to hold these constants
  2. Inside this header file, declare a namespace
  3. Add all your constants inside the namespace (make sure they’re const)
  4. #include the header file wherever you need it

Use the scope resolution operator (:

C++笔记(to be cont'd)的更多相关文章

  1. Haskell语言学习笔记(30)MonadCont, Cont, ContT

    MonadCont 类型类 class Monad m => MonadCont m where callCC :: ((a -> m b) -> m a) -> m a in ...

  2. C++学习笔记(1)

    本学习笔记是C++ primer plus(第六版)学习笔记.复习C++基础知识的可以瞄瞄. 转载请注明出处http://www.cnblogs.com/zrtqsk/p/3874148.html,谢 ...

  3. 提取KIndle中每本书的笔记并单独保存

    整体思路 目标:将Kindle中的每本书的笔记标注单独提取出保存为一个Markdown文件 其中检测KIndle是否已经正常插入的判断方法: 思路1:读取媒介挂载记录 思路2:直接判断挂载地址是否存在 ...

  4. Solr In Action 笔记(3) 之 SolrCloud基础

    Solr In Action 笔记(3) 之 SolrCloud基础 在Solr中,一个索引的实例称之为Core,而在SolrCloud中,一个索引的实例称之为Shard:Shard 又分为leade ...

  5. 最全的ORACLE-SQL笔记

    -- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...

  6. memcached学习笔记——存储命令源码分析上篇

    原创文章,转载请标明,谢谢. 上一篇分析过memcached的连接模型,了解memcached是如何高效处理客户端连接,这一篇分析memcached源码中的process_update_command ...

  7. Hibernate 马士兵 学习笔记 (转)

    目录(?)[+] 第2课 Hibernate UML图 第3课 风格 第4课 资源 第5课 环境准备 第6课 第一个示例Hibernate HelloWorld 第7课 建立Annotation版本的 ...

  8. python全栈开发中级班全程笔记(第二模块、第三章)(员工信息增删改查作业讲解)

    python全栈开发中级班全程笔记 第三章:员工信息增删改查作业代码 作业要求: 员工增删改查表用代码实现一个简单的员工信息增删改查表需求: 1.支持模糊查询,(1.find name ,age fo ...

  9. memcached set命令的大致处理逻辑笔记

    这次记录状态机的主要逻辑,跟踪set命令的执行流程,暂不涉及到内存申请这一块,下面内容基本都是代码注释 首先还是补充了解下客户连接在发送数据到数据被处理并返回过程中conn的各种状态的表示 enum ...

随机推荐

  1. zookeeper使用跟原理

    zookeeper使用和原理 zookeeper介绍zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目中的一个子项目,并且根据google发表的<The Chu ...

  2. ADS的go to命令

    我们有时候在一个函数右键没有看到“go to”选项,这是因为没有Make,要先Make之后才能使用go to 命令

  3. Hackers’ Crackdown-----UVA11825-----DP+状态压缩

    题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. 使用DBUnit实现对数据库的测试

    这是一个JavaProject,有关DBUnit用法详见本文测试用例 首先是用到的实体类User.java package com.jadyer.model; public class User { ...

  5. c++代码的陪伴下----菜鸟的转变

    在c++代码的陪伴下快乐的长大 学了c++也快一年了,在这里把自己的心得体会说一下吧. 1.感觉自己这一年做过的代码行数也不少,博客点击量和浏览量却不是很多,可能是自己所编的程序里面都是很基础的吧,对 ...

  6. Android开发(21)--有关Spinner控件的使用说明

    下拉列表 Spinner,Spinner是一个每次只能选择所有项的一个项的控件.它的项来自于与之相关联的适配器中. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下 ...

  7. 算法线性编程珠玑读书笔记之----->使用线性算法求解连续子序列的最大和

    这段时间笔者几篇文章介绍了改算法线性的文章. 关联文章的地址 这个算法我在我的博客里应用动态规划做过,详细实现请参阅我的dp板块,下面给出书上最快的算法,时间复杂度为O(n),称之为线性算法. #in ...

  8. Java 在本地文件中查找固定字符串

    适用范围:只适用于在文本文档中查找(如,txt.java.c等等,并不适用与doc.xls等等这些文件),可嵌套文件夹.但是对中文不支持. 例如:文件夹:F:/demo 子文件夹:F:/demo/er ...

  9. C语言之总结3

    23). 内存中的五大区域 a. 栈 是专门用来存储局部变量的.所有的局部变量都是声明在栈区域中的. b. 堆 允许程序员手动的从堆申请空间来使用.(对象) c. BSS段 是用来存储未初始化的全局变 ...

  10. Linux编程之UDP SOCKET全攻略

    这篇文章将对linux下udp socket编程重要知识点进行总结,无论是开发人员应知应会的,还是说udp socket的一些偏僻知识点,本文都会讲到.尽可能做到,读了一篇文章之后,大家对udp so ...