C++11-----Basic2

Source: Internet
Author: User

1. Use a using declaration

. Using namespace Spacename

. Using Namespace::name

2. Standard library type String

Initialization

. string S//default NULL string

. String s (S1)//Copy initialization

. String s = S1//Ibid.

. String S ("XXXX")

. String s = "xxxxx"

. string s (n, ' C ')//s initialized to a string consisting of a continuous n-character C

. String s = {' h ', ' e ', ' l ', ' l ', ' 0 '};

Manipulate

Iterator:

Begin ()

End ()

Capacity:

Size ()

Length ()

Capacity ()

Resize ()

Reserve ()

Clear ()

Empty ()

Access:

[]

at ()

Front ()

Back ()

Modifiers:

operator+=

Append ()

Push_back ()

Insert ()

Erase ()

Swap ()

String Operations:

C_str ()

Data ()

Copy ()

Find ()

SUBSTR ()

Compare ()

Compare:

==

!=

> >=

< <=

Other:

is >> s

OS << S

Getline (is, s)

Note:

    • Length () & Size (): Length () for C-style, size () for STL introduction
    • [] & at (): [] When the subscript of the access is out of range, the result is undefined; at () The scope is checked, the subscript is exceeded, and the Out_of_range exception is thrown
    • C_STR () & Data (): C_str () returns a null-terminated pointer to the content, and data () simply returns a pointer to the content that does not include NULL, providing only valid character data
    • String object cannot be truncated by ' + ' and is only meaningful in C-style string.

As below, we have written a teststring.cpp program for testing

Compile run:

#g + +-wall-g-std=c++0x teststring.cpp//default generate executable program a.out

#./a.out

Description

1. Initial s initialized to an empty string with a print length of 0

2.s add content, even if there are null characters, the number is also counted, so size is 9;c style string, always to the end of the, so size is 5

3. When a C-style string is initialized or added to a string object, it is truncated, the content is discarded, and the new string object is no longer truncated when it is initialized or added to a new string object using a string object with a \ n, as shown in S2 printing

Appendix:

  

Figure 1

Figure 2

Note:

The Push_back () function, the length count plus 1 of s, can always be drawn from the S + = ' + ' character from Figure 1.

It is deduced that when the + = C-style string is appended, only the length of strlen (CStr) is added, and when the + = string object is appended with the length of string.size ()

C_STR () & Data ()

Note:

The string object does not identify the end of the strings with a null character (null, ' \ s ')

3. Range for (range for)//c++11 Add

for (range_declaration:range_expresssion)

Loop_statement;

Range_declaration: A variable declaration, which is an element or reference to a sequence of range_expression, usually using auto to detect its type

Range_expression: An expression or curly brace that can be expressed as a sequence to initialize a list

4.vector

Initialization

Vector<t> V

Vector<t> V1 (v)

Vector<t> V1 = V

Vector<t> v {...} Initialize-list

Vector<t> v = {...}

Vector<t> V (N, Val)

Vector<t> V (N)

Other

V.empty ()

V.size ()

V.push_back ()

V[n]

= = = > >= < <=

5. iterators (iterator)

C++11-----Basic2

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.