SYSU programming c++ (second week) string, function overloading, constexpr, auto

Create a string object:

string s1 ; // s1 does not use initialization parameters , that is, the default initialization is an empty string

string s2 = " c++ " ; // not an assignment operation , it is equivalent to string s2("c++")   , it is initialization

string s3 (5, ' s'); //  s4 is initialized to 5 s ;

The string class implements the following operator overloads:

Join operation: +

Comparison operations: ==,!=,<,<=,>,>=

Assignment operations: = , += , rvalues ​​can be strings, characters, C strings, character arrays

Read the entire line into the string (including spaces):

getline(cin,str);

Review Constant pointers and pointer constants:

Note that const int * p1 is a constant pointer (a constant pointer), the space pointed to by p1 can be changed, but *p1 cannot be changed (it can be changed directly, but not through *p1)

int * const p2 is a pointer constant (a pointer is a constant), the space pointed to by p2 cannot be changed, but the value stored in the space pointed to by p2 can be changed

const int * const p3, then p3 and *p3 cannot be changed.

Function overloading:

In C language, a function name (and related formal parameters) can only define one function .
C++ allows multiple functions to have the same name , as long as their parameter lists are different . This is function overloading ( Function Overloading ), which will automatically find an adaptation when used.

string operation:

s.length ()       //Returns the length, excluding the terminator.

s.c_str()        //Return a c-style string, the type is const char* cs constant pointer

s.find(str, pos )     //Starting from the subscript pos, find the first substring equal to the given character sequence. The return value is a subscript rather than an address (strchr in c language returns an address). If find is changed to rfind  , the search starts from the right.

s.replace(pos,count, str) : The count bits starting from pos are erased and replaced with str, which can be longer or shorter than l bits.

append(str) : add the string str at the back

append(int count, char ch ) : add count characters ch at the back

insert(int index, int count , char ch) : insert count ch before the index subscript

insert(int index, str ):  Insert str before the index subscript

erase(int index, int count)  : delete count characters starting from subscript  index

clear() empty

substr(int pos, int count):  returns a substring object whose length is count starting from the subscript pos

compare( str): compare with str , return one of 1 , 0 , -1

compare(int pos, int count, str) : compare the substring whose length is count starting from subscript pos with str , return 1/ 0 / -1

copy(str , int pos, int count):  copy the substring to the target object str

swap(string other) : exchange content with other

const and constexpr:

Because there are two const semantics: (generally both meanings have both)

1. Limited to read-only variables (as a function parameter, no initial value is assigned, and the default is only this meaning)

 When combined with references, const int & con_b = a; then b will still change when the value of a changes.

2. Limit as a constant or literal

The C++11 standard adds the keyword constexpr , which is limited to constant expressions, and declares that rvalues ​​(functions or variables) can be evaluated at compile time , that is, functions that can be optimized for execution at compile time

auto:

auto is a type in c++. If a expression , auto can be used instead of the type declaration. The type of the object is automatically deduced from the expression type during compilation .

Notice:

1. Variables declared with auto must be initialized

2. Function and template parameters cannot be declared as auto

3. auto cannot be used for type conversion or some other operations, such as sizeof

4. Variables defined in an auto sequence must always be deduced to the same type

Guess you like

Origin blog.csdn.net/jz_terry/article/details/129300269