Chapter 3: strings, vectors, and arrays, and vectors of 2017.11.12

Source: Internet
Author: User

Chapter 3: strings, vectors, and arrays, and vectors of 2017.11.12

3.1 namespace using Declaration

Std: cin indicates that the content is read from the standard input. Here, the scope operator (: :) is used to indicate that the compiler looks for the right name from the scope shown in the operator's left name.

Using declaration is the safest way.

The using Declaration has the following format:

Using namespace: name;

For example:

# Include <iostream>

Using std: cin;

Int main ()

{

Int I;

Cin> I; // The meaning of cin and std: cin is the same.

Cout <I; // error, no using Declaration

Std: cout <I;

Return 0;

}

3.2 standard library type string

String of the standard library type indicates a variable length character sequence. The string type must contain the string header file. As part of the standard library, string is defined in the namespace std.

# Include <string>

Using std: string;

3.2.1 define and initialize a string object

String s1; // s1 is an empty string

String s2 (s1); // s2 is a copy of s1

String s2 = s1; // equivalent to s2 (s1)

String s3 ("value"); // s3 is a copy of the nominal value "value", except for the empty character at the end of the nominal value

String s3 = "value"; // equivalent to s3 ("value ")

String s4 (n, 'C'); // initialize s4 as a string consisting of n consecutive characters c

Use = for copy initialization, and others for direct initialization.

3.2.2 operations on string objects

OS <s // writes s to the output stream OS and returns the OS

Is> s // read the string from is and assign it to s. The string is separated by spaces and the is returned.

Getline (is, s) // read a row from is and assign it to s. the return value is

S. empty () // if the value is null, true is returned. Otherwise, false is returned.

S. size () // returns the number of characters in s.

S [n] // returns the reference of the nth character in s. The position n starts from 0.

S1 + s2 // return the result after s1 and s2 are connected

S1 = s2 // replace the original character in s1 with a copy of s2

S1 = s2 // string object equality judgment is case sensitive to letters

S1! = S2

<, <=,>,> =

 

Read/write string

 

# Include <iostream>

# Include <string>

Using std: cin;

Using std: cout;

Using std: endl;

Using std: string;

Int main ()

{

String s;

Cin> s; // when the read operation is performed, the blank spaces (namely, space characters, line breaks, and tabs) at the beginning of the string object are automatically ignored and read from the first real character, until the next blank

Cout <s <endl; // If the input is "Hello World !", The output is Hello.

Return 0;

}

If you want to retain blank characters, you need to use getline (). Getline ends the read operation and returns the result as long as it encounters a line break.

# Include <iostream>
# Include <string>
Using std: cin;
Using std: cout;
Using std: endl;
Using std: string;
Int main ()
{
String word;
While (getline (cin, word) // read characters from input, put them in word, and return cin
{
Cout <word <endl;

}
Return 0;
}

 

 

 

 

 

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.