Giter Club home page Giter Club logo

cpp-primer's Introduction

C++ Primer

目录

变量和基本类型

基本内置类型

  • 当明知数值不可能为负数的时候使用无符号类型(unsigned)。
  • 尽量使用 int 执行整数操作,若超过 int 的表示范围,则使用 long long 类型。
  • 尽可能使用 double 而不是 float 类型。
  • 当一个表达式既有带符号类型也有无符号类型时,若带符号类型取值为负,则带符号类型会被转化为无符号类型。

例子:

int a = -1; // a = 4294967295 if 32-bit
unsigned b = 1;

变量

  • 初始化时在创建变量时赋予其初始值;赋值时用一个新值取代其当前值。
  • 建议初始化每一个内置类型的变量。
  • 变量只能定义(define)一次,而可以多次声明(declare)。

要声明一个变量而不定义的话需要使用 extern 关键字,并且使用 extern 后可以让变量在其它文件中使用,但不要显式初始化变量:

extern int i; // DO NOT ASSIGN.
  • 尽量在变量第一次使用的地方定义它,这样有助于找到它的定义和赋合适的初始值。

复合类型

  • 指针(pointer)是对象,而引用(reference)不是。
  • 因此允许对指针进行赋值和拷贝,且指针可以先后指向不同对象。
  • 指针无须在定义时赋初始值。
  • 建议初始化所有指针。

指向指针的引用:

int *p;
int *&r = p; // r 是对指针 p 的引用

const 限定符

const 指针

int x = 10, y = 50;
int *const cptr = &x; // 可以改变 cptr 的值而不可以改变 cptr 所在的地址
const int *const ccptr = &y; // 既不可以改变 ccptr 的值也不可以改变 ccptr 所在的地址

顶层 const

  • 顶层 const 表示指针本身是常量;可以表示任何对象是常量,对任何数据类型适用。
  • 底层 const 代表指针所指的对象是常量;则与指针和引用等符合类型的基本类型部分有关。
  • 而指针类型既可以是顶层也可以是底层 const。

常量表达式和 constexpr

可以将变量声明为 constexpr 类型以便由编译器验证变量的值是否为常量表达式,例子:

constexpr int size = 20;
  • 当使用 constexpr 声明一个指针时,它仅对指针有效,对指针所指的对象无效。并把它锁定义的对象置为顶层 const

例子:

const int *p = nullptr; // p 是一个指向整型常量的指针
constexpr int *q = nullptr; // q 是一个指向整数的常量指针

处理类型

decltype((var)) 的结果永远是引用,而 decltype(var) 只有当 var 本身是引用时才是引用。

int i;
decltype(i) e; // e 是未初始化的 int
decltype((i)) d; // d 是 int& 必须进行初始化

自定义数据结构

  • 头文件改变后相关的源文件必须重新编译获得更新后的声明。
  • 一般来说应该习惯性加上头文件保护符。

字符串、向量和数组

命名空间的 using 声明

  • 头文件不应该包含 using 声明。

string 类型

  • 如果表达式中有了 size() 的函数就不要再使用 int ,这样可以避免混用 intunsigned 可能带来的问题。
  • 字符串字面值与 string 不是同一种类型。
  • 下标运算符([ ... ])接受的输入类型是 string::size_type 的值。并且下标必须大于 0 小于 size() 的值。

vector 类型

  • 如果使用圆括号,则提供的值是构造(construct)vector 对象;如果是花括号,则提供的值是列表初始化(list initialize)vector 对象。

例子:

vector(int) v(10, 2); // 有 10 个元素,每个元素是 2
vector(int) v2{10, 2}; // 有 2 个元素,分别是 2 和 10
  • vector 和 string 对象的下标运算课用语访问已存在的元素,而不能用于添加元素。因此尽可能使用范围 for 语句。

迭代器

  • 所有标准库容器都支持迭代器。

作者

Jing Guo

版权

MIT. Copyright (c) Jing Guo

cpp-primer's People

Contributors

guojing0 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.