Summarized from C++ Primer 5th Edition, as a quick review of these features
-
long long type Defined to be at least 32 bits in length. Generally not very useful.
-
List initialization A list is a set of elements composed of braces and commas, for example {1,2,3}. We can use braces to initialize variables. int a{0}; The equal sign in a declaration is not assignment but initialization, so the following form can also be used for initialization: int a = {0}; This has the same effect as int a = 0;
-
nullptr constant This is because before C++11, assigning a null pointer was done directly with 0. Now using nullptr is better. nullptr is essentially an object that can only have a right value and can only be type-converted to a pointer with a value of address 0, which solves some problems. Related information can be found in how to implement nullptr.
-
constexpr variables Stands for “const expressions”. The difference from const is that const indicates that the variable name must be a constant when used later, while constexpr not only requires this but also requires that the expression used to initialize the variable must also be a constant.
-
Type aliases using uint = unsigned int typedef uint unsigned int These two statements are equivalent
-
auto type specifier When declaring and defining a variable, the compiler automatically deduces the type of the declared variable based on the definition type.
-
decltype type indicator When declaring a variable, you can use decltype(expressions) to determine the type of an expression as the type of the declared variable
-
In-class initialization
class A {
public:
int a = 7;
};
This is equivalent to the following in previous versions:
class A {
public:
int a;
A() : a(7) {}
};
-
Using auto or decltype to deduce types auto len = line.size(); automatically deduces string::size_type which is better
-
Range-based for statements Print characters in a string for(auto c:str) cout « c « endl; Similar to for in statements in Python
-
Nested vectors vector<vector on older compilers, a space is needed, so the two angle brackets aren’t adjacent >
-
List initialization of vector objects vector strs = {“a”, “bb”, “ccc”};
-
cbegin and cend Return iterators of type const_iterator
-
Using auto in two-dimensional arrays
int mat[3][4];
int main()
{
for (int(*p)[4] = mat; p != mat + 3; p++)
{
/* ... */
}
for (auto p = mat; p != mat + 3; p++)
{
/* ... */
}
}
-
Division rounding C++11 specifies that division always rounds toward zero
-
List assignment to vector objects vector strs; strs = {“a”, “bb”, “ccc”};
-
139 Using sizeof to get the size of class members Normally class members can only be accessed through objects. But sizeof can directly determine the size of members.
-
168 Range-based for statements In range-based for statements, you cannot add elements to vector objects because it may cause the end element of the range-based for statement to become invalid. This is similar to issues in Python range loops.
-
197 initializer_list initializer_list is a template type, the object elements are always constants, and the types must be the same. Lists in braces are objects of this type.
-
203 List initialization of return values Returning an initializer_list can initialize temporary quantities for objects with a return type of vector.
-
206 Defining trailing return types Declaring a function that returns an array pointer int * func(int i) this is not quite right, as it returns a pointer, not an array pointer int (* func(int i))[10] this would return a pointer to an array of size 10 You can also write: auto func(int i) -> int(*)[10];
-
206 Using decltype to simplify return types decltype can be used to simplify return types, but this need is less common.
-
214 constexpr functions A constexpr function is a function that returns a literal type and can only have one return statement.
-
237 default default constructor You can assign an equal sign to a constructor to indicate using the compiler-provided default constructor.
-
246 In-class initialization The new standard allows for initializing members with values directly within the class
-
261 Delegating constructors When initializing a constructor, use an initializer list to initialize members. In addition, you can use other constructors to help construct. For example, a constructor is func(int a,int b), and a delegating constructor func():func(0,0){} so it can be constructed with default values 0,0.
-
268 constexpr constructors Used to generate constexpr objects
-
284 Using string type to represent file paths In stream types, previously only C character strings could be used, now strings of string type can be used
-
293 array and forward_list Very fast, comparable to handwritten lists, safer than arrays
-
300 Using list initialization for containers list authors = {“milton”, “shakespeare”, “austen”}; vector<const char*> articles = {“a”, “b”, “cc”};
-
303 Non-member swap for containers Using swap can quickly exchange two content containers
-
308 Using the return value of insert The new standard’s insert returns an iterator after successful insertion, and the position of the iterator points to the newly inserted element. Using this feature, multiple elements can be repeatedly inserted at that position.
-
308 Using emplace functions The emplace function can achieve construction rather than copying. This method can pass the parameters needed by the constructor without constructing the object and then making a copy.
-
shrink_to_fit Calling this function on a container will release extra space occupied by a vector or string. Generally not very useful
-
String type numeric conversion functions to_string represents a group of overloaded functions that can convert data of int, double types to string type. stoi can convert string to int, similar to other functions, including different bases and floating-point numbers
-
346 lambda expressions Simple lambda expression auto f = []{return 42;}; Here, a lambda expression is assigned to f. Later, f() can be called to get 42 How to pass parameters to a lambda expression? [](int a, int b){return a>b;}; But sometimes you don’t want to pass a parameter, yet still have the lambda expression use external variables? [&out](int a, int b){return a<out && out < b;}; But what if I don’t want the above expression to return int, but double? [&out](int a, int b)->double{return a<out && out < b;}
-
354 bind The bind function can change a function call to a simple call
-
377 List initialization of associative containers map<string, string> authors={ {“joncy”, “james”} }; set exclude = {“the”, “but” };
-
380 List initialization of pair return types Returning a list represented by {} to a pair type automatically converts.
-
384 List initialization of pairs When inserting pairs into a map, the simplest way is to insert a pair of key-values represented by braces. The inserted content will be initialized as a pair.
-
394 Unordered containers Add unordered in front of map and set to get unordered containers. Implemented using hash underneath. Requires implementation of a hash template for stored elements.
-
400 Smart pointers shared_ptr allows multiple pointers to point to the same object. unique_ptr exclusively owns the object it points to. Smart pointers are templates, and when declaring them, you need to explicitly provide the type, similar to using vector. shared_ptr p1;