C++ more than STL

stream

<< >>到具体primitive type(自定义类型则重载运算符),会识别到第一个invalid的字符,返回布尔值!isFail。其他的分割符包括: 等。

Some Details:

  • once an error is detected, the input stream’s fail bit is set, and it will no longer accept input。

  • 如果你需要控制读入空格,使用std::getline(istream& is, string& str, char delim), extracts but does not store delim。

  • istream只接受>>,相对应ostream只支持<<

Give any data type to the istringstream, it’ll store it as a string!

STL

bound

upper_bound:the smallest element strictly greater than(>) value lower_bound:the smallest element greater or equal to (>=) value

Iterators

(a type of pointer)

Tricks

  • stable_partition:满足条件的元素移到首部。保序

  • Structured Binding

Keywords

const correctness

  • We need to promise: a const(disallow modify) function calls const functions - need defining them as const functions.

  • read from right to left to understand:

const* const在左:指向const type的指针,指针指向的内容不能被修改; const在右:const的指针,指针本身不能被修改;

static correctness

  1. 修饰全局变量(函数)时,限制作用域在当前源文件(在main函数执行前构造)

  2. 修饰局部变量时,限制作用域在当前函数中(声明时构造,程序结束时随全局数据区释放)

  3. 修饰类的成员变量时,限制作用域为类的所有实例对象,其内存被分配在全局数据区;该变量可以直接通过类名访问。

  4. 修饰类的成员函数时,该静态成员函数不能操作类中的其他非静态成员函数;该函数可以直接通过类名调用。

type conversion

cast
description

static_cast

不执类型检查,通常用于基本数值数据类型的转换 可以在整个类层次结构中移动指针,子类转化为父类安全(向上转换),父类转化为子类不安全

dynamic_cast

用于多态类型的转换,只适用于指针或引用,对不明确的指针的转换将失败(返回 nullptr),但不引发异常,失败时引发 bad_cast 异常

const_cast

用于删除 const,volatile,unaligned 限定

reinterpret_cast

允许将任何指针转换为任何其他指针类型,简单重解释

decltype可获取变量类型,或检查实体的声明类型或表达式的类型及值分类

RTTI(Runtime Type Identification,运行时类型识别)

  • typeid 运算符,该运算符返回其表达式或类型名的实际类型

Class

Better than struct, 可见性语义。 An Object is an instance of a Class, with memory allocated. Use initializer lists for speedier construction!

Template class

Templates don't emit code until instantiated, so include the .cpp in the .h instead of the other way around! Sometimes, we need a name for a type that is dependent on our template types, such as iterators. So:

Type Aliases You can use using type_name = type

std::optional<T> is a template class which will either contain a value of type T or contain nothing (expressed as nullopt)

Template Functions

We can also implicitly return value for the compiler to deduce.(same for call)

Actually lambda is: [capture-closure]: like [&] for catch all as reference, [=] as value.

Functor: is any class that provides an implementation of operator ()

Operators

  • Non-member overloading: bool operator< (const Student& lhs, const Student& rhs);

  • Membered overloading: with friend keyword, allows non-member functions or classes to access private information in another class! friend bool operator < (const Student& lhs, const Student& rhs) const;

Special Member Functions

alloc memory

  1. malloc:申请指定字节数的内存。申请到的内存中的初始值不确定。

  2. calloc:为指定长度的对象,分配能容纳其指定个数的内存,初始化为 0

  3. realloc:更改以前分配的内存长度(增加或减少)。当增加长度时,可能需将以前分配区的内容移到另一个足够大的区域,而新增区域内的初始值则不确定。

  4. alloca:在栈上申请内存。程序在出栈的时候,会自动释放内存。

可用 try { new } catch (const bad_alloc &b) {} 捕捉new异常

member functions

There are six special member functions!

For =default, keep same For =0, must define new ones.

THE RULE OF: 0: if compiler works fine, don't define your own! 3: have to define a destructor, copy constructor, or copy assignment operator

LValue and RValue

l-values can appear on the left or right of an =, have names and are not temporary. r-values can ONLY appear on the right, have no name and is temporary. 右值不可被直接&来传参。

= automatically makes a copy! Must use & to avoid this.

Copy and Move

Copying isn’t always simple!

Deep copy: an object that is a complete, independent copy of the original

Move constructors and move assignment operators will perform "memberwise moves", which uses for R-value reference.

Smart Pointers

Pointers Protocol RAII: Resource Acquisition is Initialization (like 2PC)

  • unique_ptr:Uniquely owns its resource, can't be copied

  • shared_ptr:Can make copies, destructed when underlying memory goes out of scope(reference_count)

Always use std::make_unique<T> and std::make_shared<T>!

  • weak_ptr:Models temporary ownership: when an object only needs to be accessed if it exists (convert to shared_ptr to access)

指向一个由 shared_ptr 管理的对象而不影响所指对象的生命周期,也就是将一个 weak_ptr 绑定到一个 shared_ptr 不会改变 shared_ptr 的引用计数,以处理循环引用的问题。

模板元编程

可以理解为,对type进行编程。

元函数

A meta-function is a struct that has public member types/fields which depend on what the template types/values are instantiated with. 如,返回“输入”:

static,因为我们不需要实例化此struct

模板特化

template <[un-specified types]> class vector<bool>[specified types] {};

编译器会根据具体度降序判断是哪个特化的模板。

Concept(since C++20)


单例模式

Eager Singleton

利用 static member variable 的特性,在程序进入 main 函数之前进行初始化,这样就绕开了线程安全的问题:

两个问题:

  1. 即使单例对象不被使用,单例类对象也会进行初始化;

  2. static initialization order fiasco,即 t_ 对象和 GetInstance 函数的初始化先后顺序是不固定的;

Meyer’s Singleton

  1. 仅当程序第一次执行到 GetInstance 函数时,执行 instance 对象的初始化;

  2. 在 C++ 11 之后,被 static 修饰的变量可以保证是线程安全的;

最后更新于

这有帮助吗?