3.1 Using Declarations for namespaces
The std::cin indicates that the content is read from standard input. The scope operator (::) is used here to indicate that the compiler looks for the right name from the scope shown in the name on the left of the operator.
The use of a using declaration is the safest method.
The using declaration has the following format:
Using Namespace::name;
For example:
#include <iostream>
Using Std::cin;
int main ()
{
int i;
cin>>i; CIN and std::cin mean the same
cout<<i; Error, no corresponding using declaration
std::cout<<i;
return 0;
}
3.2 Standard library type string
The standard library type string represents a variable-length sequence of characters that must contain a string header file using the string type. As part of the standard library, string is defined in the namespace Std.
#include <string>
Using Std::string;
3.2.1 Defining and initializing 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 literal value "value", except for the null character at the back of the literal value
String s3= "value"; Equivalent to S3 ("value")
String S4 (N, ' C '); Initializes the S4 to a string consisting of a continuous n-character C
Use = Copy initialization, others are direct initialization.
3.2.2 Action on a string object
Os<<s//writes s to output stream OS, returns OS
Is>>s//Read string from is assigned to S, string separated by white space, return is
Getline (is,s)//read a line from is assigned to S, return is
S.empty ()//s returns true for NULL, otherwise false
S.size ()//returns the number of characters in S
S[n]//Return a reference to the nth character in S, Position n from 0
S1+S2//Returns results after S1 and S2 are connected
S1=S2//Replace the original characters 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 you perform a read operation, the string object automatically ignores the beginning of whitespace (that is, space characters, line breaks, tab characters), starts reading from the first real character until it encounters the next blank
cout<<s<<endl; If the input is "Hello world! ", the output is Hello
return 0;
}
If you want to preserve whitespace characters, you need to use Getline (). Getline ends the read operation and returns the result whenever a newline character is encountered.
#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 into Word, and return cin
{
cout << word << endl;
}
return 0;
}
2017.11.12 chapter III string, vector and array