"C + + Primer (fifth edition)" Knowledge Consolidation

Source: Internet
Author: User

Operating platform: Ubuntu 12.04/GCC 4.8.0

Chapter Two: Basic built-in types

1.decltype Type Indicator

When we infer the type to be defined from the type of the expression, you can use Decltype () to parse it, and decltype, unlike auto, Decltype applies to the variable and returns the type of the variable.

2. Escape sequences

\xxx octal

\x (XXX) hex

unsigned char c =-1;   Assuming 8-bit chars, C has value 255i = C;  The character with value 255 is a unprintable character        //assigns value of C (i.e., 255) to an intstd::cout <& Lt I << Std::endl; Prints 255
unsigned u = 10;int i = -42;std::cout << i + i << Std::endl;  Prints-84std::cout << U + i << Std::endl;  If 32-bit ints, prints 4294967264

Use of 3.getline ()

String line;//read input a line at a time until End-of-filewhile (Getline (CIN, line)) cout << line << Endl;

4.str.size () returns to the String::size_type type, which is one of the standard library-independent features of the machine.

As in the following example, enter a number in the size of 0-15 to find the hexadecimal symbol that corresponds to it:

Const string hexdigits = "0123456789ABCDEF";  Possible hex digitscout << "Enter a series of numbers between 0 and"     << "separated by spaces.  Hit ENTER when finished: "      << endl;string result;        Would hold the resulting hexify ' d stringstring::size_type N;  Hold numbers from the Inputwhile (CIN >> N) if (n < hexdigits.size ())    //Ignore invalid Inputresult + = Hexd Igits[n];  Fetch the indicated hex digitcout << "Your hex number is:" << result << Endl;

It is related to the size_t type, size_t is defined in the standard C library and should be unsigned int, which is a long unsigned int in a 64-bit system.

#include <cstddef>using std::size_t;constexpr size_t rowcnt = 3, colcnt = 4;int ia[rowcnt][colcnt];   Uninitialized elements     //For each row for    (size_t i = 0; I! = rowcnt; ++i) {        //for each column within The row for        (size_t j = 0; J! = colcnt; ++j) {            //Assign the element ' s positional index as its value            ia[i][j] = i * colcnt + j;   }}

The fourth chapter: expression

1. Overflow of values

Consider the results of the following operations:

Short short_value = 32767; Max value if shorts is bitsshort_value + = 1; This calculation overflowscout << "Short_value:" << short_value << Endl;

Sixth chapter: Functions

1. Returns the function of the array pointer, these three kinds of expression, note:

int *p = ELEMPTR (6);         P points to an intint (*ARRP) [5] = arrptr (5);  ARRP points to an array of five intsint (&ARRR) [5] = Arrref (4);  Arrr refers to a array of five ints/two arraysint odd[] = {1,3,5,7,9};int even[] = {0,2,4,6,8};//function that retur  NS A pointer to an int in one of these arraysint *elemptr (int. i) {//Returns a pointer to the first element in one of these Arraysreturn (i% 2)? Odd:even;  } Returns a pointer to an array of five int elementsdecltype (odd) *arrptr (int i) {return (i% 2)? &odd: &even;/ /Returns a pointer to the array}//returns a reference to an array of five int elementsint (&arrref (int i)) [5]{retur N (i% 2)? Odd:even;}

2. Functions with variable parameters

Two methods are available in C11: If the parameter type is the same, you can pass a standard library type named Initializer_list, and if the type is different, you can use the Variadic template, see Chapter 16. Here's how to use it:

Overloaded version takes only a list of the stringsvoid error_msg (initializer_list<string> il) {for (Auto Beg = Il.beg In (); Beg! = Il.end (); ++beg) cout << *beg << ""; cout << Endl;}  String expected = "description", actual = "Some other case";initializer_list<int> li = {0,1,2,3};//expected, actual is strings if (expected! = Actual) error_msg ({"Functionx", expected, actual}); Elseerror_msg ({"Functionx", "Okay"});

3. Function pointers

C11 the return type in the tail, as stated below:

Use trailing return Typeauto GETFCN (const string&), String::size_type (*) (const string&, const string& );

The other two declarations of the function pointers are defined, one is () parenthesis parsing and the other is borrowing decltype to determine the return type as a function pointer type, using the following:

Decltype (sumlength) *GETFCN (const string &);//Direct Definitionstring::size_type (*GETFCN (const string&)) ( const string&, const string&);//define GETFCNDECLTYPE (sumlength) * GETFCN (const string &fetch) {if (fetch = =) Sum ") return Sumlength;return Largerlength;}

"C + + Primer (fifth edition)" Knowledge Consolidation

Related Article

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.