Tips for C + + Primer Chapter 3

Source: Internet
Author: User

Chapter three string, vector and array

Name space
Using Std::cin;

using namespace Std;

String S4 (3, ' C '); The value of S4 is "CCC"

Use Getline to read a whole line

String line;

Getline (cin, line);

If a row is successfully read, the Getline function returns True (reading to a blank line also returns True, because the read succeeded to a ' \ n '), where line is an empty string, and the read failure returns false.

Getline reads the newline character, and the newline character is also read in, but the newline character is not stored when the string is stored in an object.

Action on a String object

Os<<s writes s to output stream OS, returns OS

Is>>s reads the string from input stream is to S, returns is

Getline (IS, s) reads a row from an input stream is assigned to s

S.empty ()

S.size () s.length () There's no difference.

S[i]

+

=, = =,! =

<, <=, >, >=

The size function return type of string is the Size_type type, which is an unsigned integer.

string S ("abc"); The S.size () value is 3, note that it is an unsigned number

BOOL B1 = S.size () <-1; The value of B1 is true, because S.size () is an unsigned number, and when mixed with signed and unsigned numbers, the signed number is automatically converted to an unsigned number, where 1 becomes a large positive integer (the way the conversion is discussed in the 2nd chapter)

int len = S.size (); Unsigned number s.size () first converted to a signed number Len

bool B2 = Len <-1; The value of B2 is false, because Len is a signed number, and an unexpected auto-conversion problem occurs when there is no signed and unsigned number mixed with the 1 comparison

Dictionary order size Comparison of strings

The comparison result of the string size equals the first pair of distinct characters

"ABC" < "AC"

If the corresponding position characters are all the same, the smaller string size is small

"ABC" < "ABCD"

Otherwise two strings are equal

Tips for C + + Primer Chapter 3

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.