C + + Primer notes

Source: Internet
Author: User

  • Chapter 2
    • Low-level & top-level const
    • constexpr
    • Type Alias
      • Pointer alias
    • Decltype
      • Decltype and references
  • Chapter 3
      • Type aliases for multidimensional arrays
  • Chapter 6 function
    • Declaring a function that returns an array pointer
    • function overloading
      • Const_cast
    • function arguments
      • Default actual parameters
      • Default argument initial value
  • Chapter Class 7
    • Characteristics
      • Variable data members
      • Returns the member function of the *this
      • Const-based overloading
    • Delegate constructors
  • Chapter 8
    • String stream
      • Istringstream
      • Ostringstream
  • Chapter 9
    • Container operations
      • Emplace
    • Adapter
  • Chapter 10
    • Generic algorithms
    • Lambda expression
Chapter 2low-level & top-level const
int i = 0;int *const p1 = &i;         // top-levelconst int c1 = 42;          // top-levelconst int *p2 = &ci;        // low-levelconst int *const p3 = p2;   // low-level (left) & top-level (right)const int &r = ci;          // both low-level
constexpr
constexpr int mf = 20;
Type Alias
typedef double wages;       // classictypedef wages base, *p;using SI = Sales_item;      // C++11
Pointer alias
typedef char *pstring;const pstring cstr = 0;     // cstr是指向char的常量指针const pstring *ps;          // ps是一个指针,它的对象是指向char的常量指针const char *cstr = 0;       // [注意]与 const pstring cstr 不同!
Decltype
decltype(f()) sum = x;
Decltype and references
decltype(i) e;      // 正确decltype((i)) d;    // 错误, d 是 int&
Chapter 3 Type aliases for multi-dimensional arrays
using int_array = int[4];typedef int int_array[4];
Chapter 6 function declares a function that returns an array pointer
int (*func(int i))[10];auto func(int i) -> int(*)[10];    // lamabda
function Overloading Const_cast
const string &shorterString(const string&, const string &);// 使用const_cast重载原函数的非常量版本string &shorterString(string &s1, string &s2){    auto &r = shorterString(                const_cast<const string&>(s1),                const_cast<const string&>(s2));    return const_cast<string&>(r);}
function argument default argument
typedef string::size_type sz;string screen(sz, sz, char = ' ');string screen(sz, sz, char = '*');      // 错误string screen(sz = 24, sz = 80, char);  // 正确
Default argument initial value
sz wd = 80;char def = ' ';sz ht();string screen(sz = ht(), sz = wd, char = def);string window = screen(); // 调用 screen(ht(), 80, ' ');
Chapter 7 Class attribute variable data member

keyword mutable

Returns the member function of the *this

Returning *this means returning an object as an lvalue, meaning that a series of operations can be concatenated in an expression

myScreen.move(4, 0).set('#');
Const-based overloading
class Screen {public:    Screen &display(std::ostream &os) {        do_display(os); return *this;    }    const Screen &display(std::ostream &os) const {        do_display(os); return *this;    }}Screen myScreen(5, 3);const Screen blank(5, 3);myScreen.set('#').display(cout);    // 非常量版本blank.display(cout);                // 常量版本
Delegate constructors

A delegate constructor performs its own initialization process using the other constructors of the class it belongs to

Chapter 8string Stream Istringstream
istringstream record(line);record >> info.name;
Ostringstream
ostringstream formatted;formatted << anyString << endl;cout << formatted.str();
Chapter 9 Container Operation Emplace

The following equivalence

c.emplace_back(args);c.push_back(T(args));
Adapter
stack<int, vector<int>>stk; // 使用vector构造stack适配器
Chapter 10 Generic algorithm
    • back_inserterInserting iterators
    • for_each
Lambda expression
    • Value capture
    • Reference capture
    • Implicit capture

C + + Primer notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.