A, string
1. String containing header file; namespace std
#include <string>using std::string
2. When the read operation, the string object automatically ignores the beginning of the white space until it encounters the next blank
If you want to preserve whitespace, use the Getline function: Read the newline character, but do not write the line break to string
String>> s1 >> S2; // enter " Hello World "cout << s1 << S2 << Endl; // get "HelloWorld" Getline (CIN, S3); // enter " Hello World "cout << S3 << Endl; // get " Hello World "
3, string can get the length, use the size () function, return is an unsigned integer, so be aware that when used for circular judgment do not use with the signed number
4, the comparison of String objects, first compare the length, if the same, and then compare the first different characters
5, the addition of the string object: The + number must be on both sides is a string type
6, usually the C + + program header file is a CNAME, and the C program header file is name.h
7. Range for
for (declaration:expression) // each iteration, the declaration part is initialized to the next element of the expression section Statement
- Dclaration type can be used auto; If you want to change the value of expression, you need to be a reference type
string s ("HelloWorld"); for (Auto &c:s) // defined as a reference type, you can change the value of S c =<< s << endl;
Vector: a collection of objects; a class template
1. Initialization
- If you are using parentheses, construct the vector object with the provided value
vector <int > V1 (10 ); // V1 has 10 elements, each default initialized to 0 vector <int > V ( Span style= "COLOR: #800080" >10 , 1 ); //
V2 has 10 elements, each initialized to 1
curly braces, list of initialized vector objects
vecotr<string > V3{ " hello ", " Span style= "COLOR: #800000" >world "}; //
with a list of two values inside The method for direct initialization of
- applies To: The initial value is known and the number is small; The initial value is a copy of the other vector object; All element initial values are the same
2. Add element: V.push_back (t): Add an element with a value of T to the tail
- Once the value of the element is different, an empty vector object is defined before the runtime adds the concrete value push_back (), which is more efficient than opening its size earlier
Iterators: All standard library containers can use iterators, but only string and vector,map support subscript operators;
1. Begin and END members: The end member returns to the next position of the "tail element" of the container
Auto B = V.begin (), E = V.end (); // B and e are of the same type; we don't care what the exact type of the iterator is .
2, if the object is only for reading, but not write operation it is best to use constant type, reference cbegin and Cend function
Auto it = V.cbegin () // type vector<int>:: Const_iterator
3. Dereference an iterator: the object that the iterator refers to can be obtained, and if the object's type is exactly the class, then further access to the member
(*it). Empty () ======== it->empty ()
Loop body with iterators, do not add elements to the container to which the iterator belongs
Four, array
1. C + + 11 introduces a function of begin and end, the same as the begin,end effect of an iterator
2. String functions in library C, such as strlen,strcmp,strcat,strcpy, the pointer to the function must be a null character as the end of the array
3, String type->const char * type, using the C_str () function
char *str = s; Const char *str = S.c_str ();
4. Initialize the vector object with an array, just specify the first element address of the copy area and the post-end address
int arr[] = {012345};vector< int> Ivec (Begin (ARR), End (arr));
5. Use vectors and iterators as much as possible in C + + to avoid using built-in arrays and pointers
string and other elements of the vector are supplemented in later chapters
Chapter Three: string, vector, and array