Standard library type String
String represents a variable-length sequence of characters. is part of the C + + standard library type and has many excellent performance.
When a string object is defined, the compiler defaults to an empty string, if not artificially initialized.
There are many types of initialization of string objects, broadly divided into two categories: copy initialization and Direct initialization . All initializations with an equal sign (=) are copy-initialized, and instead of the equals sign, they are directly initialized. When there is only one initial value, both copy and direct initialization are possible, but when more than one value is required, you can generally use direct initialization only. It is recommended to use Direct initialization whenever possible.
String s1;//is initialized by default to an empty string s2=s1;//copy initialization, S2 is a copy of the S1 string S3 (N, "C");//Direct initialization, S3 is a string of continuous n C
When reading a string object, the string object automatically ignores the beginning of whitespace ( spaces, tabs, actuators, and so on) and starts reading from the first real character until it encounters the next blank.
The Getline function can read an entire line including whitespace in a string, and the parameters of the Getline function are an input stream and a string object. Stores a line of string read from the input stream into a string object. When Getline encounters A switch, it ends and returns the result (Getline is the one that reads the shift character, but the result is a newline character discarded).
String Line;while (Getline (Cin,line))//read the input stream repeatedly, know to meet the end flag EOF cout<<line<<endl; output each line and manually add a line break operation
The size of string (the wrapper function of the string class) operation returns the length of the string object, and the return type is String::size_type, which is a mysterious type that only knows it is an unsigned shape and can hold any The size of the string object. Since the function return value is an unsigned integer, it cannot be compared with the signed number , because when the unsigned number is compared to a negative value, the negative value automatically turns into a larger unsigned number, making the comparison error.
string Line;auto len = Line.size (),//len is the Size_type type, and the value is 0,line is an empty string
The string class defines an operator that compares strings, and when two string objects are identical, these two string equals (= =);
Other comparisons such as < ', <=,>, >=, string objects follow two principles:
1. When a string object A is less than the length of string object B, and A and B are at the same length as the characters of A, a < b;
2. If two string objects are inconsistent at some corresponding locations, the result of the comparison of the string object is the result of the comparison of the first distinct character (following the case-sensitive dictionary order).
string str = "Hello"; string phrase = "Hello world"; string slang = "Hiya";//is determined by rule 1, phrase is greater than str; by rule 2, phrase is greater than STR and Slang
The string object overloads the + operator, and the + operator enables the connection of string to string, literal value, and string literal. Literals and string literals cannot be added directly (string literals are different types than strings) because the + operator is encapsulated with the string class.
string S1 ("Hello"), S2 ("World"), string s3 = S1 + s2;//s3 content is Hello worldstring s4 = s1 + ', ';//S4 is hello,string S5 = S1 + ", world";//s5 's content is Hello, world
Standard library type vector
Vectors represent A collection of identical objects . Vector is a class template , vector itself is not a class, but it is a description of the compiler generated classes , when using a template instantiation class must indicate what type of instantiation, vector use angle brackets <> to indicate the instantiation type. Vectors can hold most objects as their elements (except references, references are not objects), and even the vector itself.
Vector initialization is the same as string, and c++11 also adds a method to list initialization for vectors. The 0 or more initial element values enclosed in curly braces are assigned to the vector object.
Vector<string> vs = {"cs", "DN"};
List initialization is confusing for reading because the values in the list can either represent the initial value or the number of elements, and the compiler will first think of the list initialization (which treats the values in the list as the initial value) and, if not, the other initialization methods.
Vector<int> V1 (ten);//v1 has 10 elements, 0vector<int> v2{10};//v2 has 1 elements, 10vector<int> v3 (10,1);// V3 has 10 elements, all 1vector<int> v4{10,1};//v4 have two elements, 10 and 1vector<string> v5{"Hello", "Hi"};//V5 has two elements, list of initialized vectors <string> V6{10};//v6 has 10 empty string elements
Adding an element to a vector must use the vector's member function, Push_back (), to add a value to the end of the vector. You cannot use subscript operators. the subscript operator is used only to access elements that already exist for a vector (string) object. The use of subscript operators must ensure that they are within reasonable range (greater than 0 and less than capacity).
Vector to use the Size_type type, you must explicitly indicate which type it is defined by.
vector<int>::size_type;//correct vector::size_type;//Error
The elements in a vector object can be compared only if the elements themselves support comparison operators (some custom classes do not have a comparison operator defined).
Range for statement
The range for statement iterates through each element within a given range and does some action on each element in the sequence. You can use the range for statement to eliminate the need for a relationship beyond the valid range.
String str ("Hello World"), for (auto c:str)//traversal of each character element of str cout<< c <<endl;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A probe into string and vector