<> Learning objectives :

* master C++ Introductory knowledge
* master STL
* The training problem of Valley algorithm
<> Learning content :

<>C++ Introductory knowledge

one , Basic introduction :C++ differ C language , This is an object-oriented high-level programming language .
two , Object oriented and process oriented : What is object-oriented ? What is the object ?
Objects are abstractions of objective things , in other words , Anything can be regarded as an object . Speaking of object orientation , You have to mention process oriented . We can give an example to understand them :

for example : We're going to the vending machine to buy drinks , If process oriented : We need to go to the vending machine , Flip a coin , Get the drink , It's about the process of buying drinks . Object oriented is : What are the functions of vending machines , Collect money , give change , Deliver drinks , It faces the whole process .
three ,C++ Basic structure
as everyone knows , Every language has its own basic structure , Let me introduce C++ The basic structure of !
#include <iostream> using namespace std; // main() Is where the program starts int main() { cout
<< "Hello World"; // output Hello World return 0; }
* C++ Language defines some header files , These header files contain necessary or useful information in the program . In the above procedure , Contains header file .
* next row using namespace std; Tell the compiler to use std Namespace . Namespace is C++ A relatively new concept in .
* next row // main() Is where the program starts Is a single line comment . Single line comment with // start , End at end of line .
* next row int main() Is the main function , The program starts from here .
* next row cout << “Hello World”; A message is displayed on the screen “Hello World”.
* next row return 0; termination main( ) function , And return a value to the calling process 0.
four , variable
1, What is a variable : The variable name itself is a pointer to you and a space to store your data , Its function is to name a specified memory , Convenient for us to operate this memory .
2, Variable creation syntax : data type Variable name = Variable initial value
3, Example
#include <iostream> using namespace std; int main() { int a=10; cout<< a<<endl;
return 0; }
Specify that all variables used should be defined before use , The compiler is easy to handle , No ambiguity . because C++
inside , Variables with the same name are in different scopes , It can be declared repeatedly , And one {} Is a scope .
five , constant
1, effect : Used to record unchangeable data in the program
2, Definition method :
<1> #define Macro constant #define Constant name constant value Usually defined above the file
<2> const Decorated variable const data type Constant name = constant value
3, Example
#include <iostream> #define day 7 using namespace std; int main() { const int
time=24; cout<<" One week "<<day<<" day "<<endl; cout<<" One day "<<time<<" hour "<<endl; }
five , keyword
effect : Keyword is C++ Pre reserved words in , Also known as identifier .

* When defining variables or constants , Keywords are not allowed , Otherwise, there will be ambiguity

six , Identifier naming rules
effect :C++ Prescribed punctuation ( variable , constant ) When naming , Have their own set of rules
* Identifier cannot be a keyword
* Identifier can only be composed of letters , Composed of numbers and underscores
* The first character must be a letter or underscore
* Letters in identifiers are case sensitive
When naming an identifier , You must know what you mean
seven , data type
Existential significance : Allocate appropriate memory space to variables .

1, integer

2,sizeof keyword
<1> effect : utilize sizeof Keyword can count the memory size occupied by the data type
<2> grammar :sizeof( data type / variable )
<3>
#include <iostream> using namespace std; int main(){ cout <<"short Space occupied by type "<<
sizeof(short)<<endl; cout <<"int Space occupied by type "<<sizeof(int)<<endl; cout <<"long Space occupied by type "
<<sizeof(long)<<endl; cout <<"long long Space occupied by type "<<sizeof(long long)<<endl; cout <<
"float Space occupied by type "<<sizeof(float)<<endl; cout <<"double Space occupied by type "<<sizeof(double)<<endl;
cout<<"long double Space occupied by type "<<sizeof(long double)<<endl; cout <<"char Space occupied by type "<<
sizeof(char)<<endl; cout <<"unsigned char Space occupied by type "<<sizeof(unsigned char)<<endl;
cout<<"unsigned short Space occupied by type "<<sizeof(unsigned short)<<endl; cout <<"unsigned
int Space occupied by type "<<sizeof(unsigned int)<<endl; cout <<"unsigned long Space occupied by type "<<sizeof(
unsigned long)<<endl; cout <<"unsigned long long Space occupied by type "<<sizeof(unsigned long
long)<<endl; cout <<"bool Space occupied by type "<<sizeof(bool)<<endl; }
3, float
<1> effect : Used to represent decimals
<2> classification : There are two types of floating point variables : Single precision (float) And double precision (double)

Data type occupied space significant digit range
float4 byte 7 Significant digits
double8 byte 15~16 Significant digits
<3> Example
#include<iostream> using namespace std; int main(){ float f1=3.14f;
// No f The system will think that double Type cout<<"f1="<<f1<<endl; double dl=3.14; cout<<"dl="<<dl<<
endl; }
4, character
<1> effect : Character variables are used to display a single character
<2> grammar :char ch='a'

be careful : When displaying character variables , Enclose characters in single quotation marks

* c/c++ Medium character variables only occupy one byte
* Character variables do not store characters themselves in memory , Instead, the corresponding ASCII Code into storage unit
<3> Example
#include<iostream> using namespace std; int main(){ char ch='a'; cout <<ch<<
endl; cout<<sizeof(char)<<endl; cout<<(int)ch<<endl; }
5, String type
<1> effect : Used to represent a string of characters
<2> grammar :string Variable name =“ String value ”
<3> Example :
#include <iostream> #include <string> using namespace std; int main(){ string
str="adfg"; cout<<str<<endl; }
6, Boolean type
<1> effect : Boolean data types represent true or false values
bool Type has only two values :

* true -- really ( The essence is 1)
* false– false ( The essence is 0)
bool Type only takes one byte
<2> Example :
#include<iostream> using namespace std; int main(){ bool flag=true; cout<<flag
<<endl; cout<<sizeof(flag)<<endl; }
eight , operator
1, Arithmetic operator
<1> effect : Used to process four operations
<2> classification

#include<iostream> using namespace std; int main(){ int a1=10; int b1=3; double
d1=3.14; double d2=3.1; cout<<a1+b1<<endl; cout<<a1-b1<<endl; cout<<a1*b1<<endl
; cout<<a1/b1<<endl; cout<<a1%b1<<endl; //cout<<d1%d2<<endl; // Must have integer }
2, Logical operator
<1> effect : Perform logical operations

<2> Example :
#include <iostream> using namespace std; int main() { int a = 5; int b = 20;
int c ; if ( a && b ) { cout << "Line 1 - Condition is true "<< endl ; } if ( a || b ) { cout
<< "Line 2 - Condition is true "<< endl ; } /* change a and b Value of */ a = 0; b = 10; if ( a && b ) {
cout<< "Line 3 - Condition is true "<< endl ; } else { cout << "Line 4 - Condition is not true "<< endl ; } if
( !(a && b) ) { cout << "Line 5 - Condition is true "<< endl ; } return 0; }
nine , Program flow structure
C/C++ Support three basic program running structures : Sequential structure , Select structure , Cyclic structure

* Sequential structure : The program is executed in sequence , No jump
* Select structure : Whether the dependent conditions are met , Perform corresponding functions selectively
* Cyclic structure : According to whether the conditions are met , Loop to execute a piece of code multiple times
The sequence structure is relatively simple , I won't go into details here
1, Select structure
<1> effect : Execute statements that meet the conditions
<2> format :

* Single line format if sentence
* Multiline format if sentence
* Multiple conditions if sentence
<4> Exercises :
It is recommended that you practice the algorithmic problem of Luogu , The title is as follows :
Enter a year ( greater than 1582 Integer of ), Judge whether this year is a leap year , If output 1, Otherwise output 0.
answer : #include <iostream> using namespace std; int main() { int year; cin>>year;
if ((year%4==0&&year%100!=0)||year%400==0){ cout<<"1"; } else cout<<"0"; }
2, Cyclic structure
<1> effect : Cycle conditions met , Execute loop statement
<2> classification :
①while sentence :while( Cycle condition ){ Loop statement }
#include <iostream> using namespace std; int main() { int n; cin>>n; while(n--)
{ cout<<n<<endl; } }
②for sentence :for( Start expression ; Conditional expression ; End loop body ){ Loop statement ;}
#include <iostream> using namespace std; int main() { int n; cin>>n; for (int i
= 0; i < n; i++) { cout<<i<<endl; } }
<3> explain : As long as the cycle condition position , Execute a loop statement .
<4> Two statements :break,continue
break
① effect : Terminate cycle
② Timing of use :

* Appears in switch In the statement , Function is to terminate case And jump out switch
* Appears in a loop statement , The function is to jump out of the current cycle
* Appears in a nested loop statement , Jump out of the latest inner loop statement
continue
① effect : In a loop statement , Skip the remaining statements in this loop , Continue to the next cycle
② Example :
#include <iostream> using namespace std; int main() { int n; cin>>n; for (int i
= 0; i < n; ++i){ if(i%2==0){ continue; } cout<<i<<endl; } }
ten , array
1, summary : So called array , Is a collection , It stores data elements of the same type
2, characteristic :<1> Each data element in the array is the same data type ;<2> Arrays are composed of contiguous memory locations
3, grammar : data type Array name []={ value 1, value 2};
4, Array name
<1> purpose :① You can count the length of the entire array in memory ② Can get the first address of the array in memory
<2> Example
#include <iostream> using namespace std; int main(){ int arr[10]={1,2,3,4,5,6,7
,8,9,10}; cout<<sizeof(arr)<<endl;// Memory space occupied by the entire array cout<< sizeof(arr[0])<<endl;
// Memory space occupied by each element cout<<sizeof(arr)/sizeof(arr[0])<<endl;// Array length cout<<(long long)arr
<<endl;// First address of array }
eleven , function
1, effect :
<1> Encapsulate a piece of frequently used code , Reduce duplicate codes
<2> Break a larger program into smaller code blocks , Each code block implements specific functions
2, Definition of function
Functions are generally defined as follows 5 Steps :
<1> return type
<2> Function name
<3> Parameter table column
<4> Function body statement
<5>return sentence
3, Function call
<1> grammar : Function name ( parameter )
<2> pass by value :
① Real parameter passes value to formal parameter during function call
② Here comes the point !
Arrays degenerate into pointers when passed
Why does it degenerate :C The language only passes parameters as copies of values , When passing parameters , If only the entire array is copied , Efficiency will be greatly reduced , And the parameter is on the stack , Too large array copy will cause stack overflow .
Example :
#include <iostream> using namespace std; void test(int *a){}// The parameter passed here is the first address of the array void
test2(int a[]){}
// Pass parameters as an array , You do not need to specify the size of the parameter , Because when one-dimensional array passes parameters , Formal parameters do not actually create arrays , Only the address of the first element of the array is passed .( If the value of a variable is passed , Then the formal parameter is a copy of the argument )
void test3(int b[][3]){}// 2D arrays are different , Must transfer data int main(){ int a[3]={1,2,3}; int b[][
3]={1,2,3}; test(a); test2(a); }

Technology
Daily Recommendation