Section2

原文档地址:https://www.yuque.com/yuqueyonghubpajeq/wdr7hn/dtockwla49ulgqhh

Flow control:Conditional programming

do different things based on the conditions that we have set up in our code 

ex:

1696684879390-9e794888-14d9-4764-bd48-68c0f67f68ed.png

keyword:

1696684940287-dcde0181-6242-4877-aea5-ab24a0e8ad74.png

If statement

if(a bool){

 

}

else{

 

}

1696685005893-1d888562-cf9d-4b35-9c96-0e60bcf491e5.png

else statement

if(a bool){

 

}

else{

 

}

1696685822631-f9b4a483-b761-4107-a79b-9c0e0a4d2930.png

nested conditions 嵌套条件

1696685889125-d25c4532-9cf2-464e-8fee-81c5417ef7ba.png

nested alternative

using logical operators

and &&

or || 

not !

1696686028112-86dd9407-1c42-46fb-b233-cac88e47163d.png

else if statement

if(a bool){

 

}

else if(a bool){

 

}

……

 

1696686147023-3d6b9940-18b0-4917-94e4-41fbefc4d083.png

switch statement

switch(condition){

case condition 1 :{

 

}

break;  ------if condition = condition 1, execute case and break switch

else,judge next case

 

case condition 2 :{

 

}

break;

 

……

 

default:{

 

}

break; --------if all cases don't satisfy,then execute default and break switch

no else

}

 

1696686859018-afa2a53b-8356-4eb4-9446-e79ec19f19ea.png

1696686383112-72b0618b-1e37-44f7-bdba-e6c2a1353418.png

1696746561122-0047f90c-b774-4229-9c6e-801e3814fc84.png

1696746691931-fa30049a-18cf-4fe0-b2f8-a78adab5c985.png

Ternary Operator

1.result =( a bool ) ? option1:option2;

result is the same variable/data type as option1/option2

 

2.result =( a bool ) ? option1:option2; --- is equivalent to below

1696747772319-5fb775a2-06bb-4ce2-8a28-c0fb7e22d378.png

3.a new initializtion:

1696747130426-1d87720d-4cad-4f3c-9932-1701e449f2a0.png

 

ex1:

1696747092620-31d65ffb-4a61-4e2f-89a0-af00688ad5fe.png

a compiler error

a,b should be convertible

 

ex2:

1696747503322-6c3496d2-c29b-41bc-ab3f-3a5823f27a0d.png

1696747649425-5cf7d48c-12c6-4231-9461-0821ba598c04.png

1696747603865-d15a8c99-12bc-488a-bf04-e31f2803daae.png 22.5f is turned into a integer

1696747579700-60d91eab-af15-4790-bc6b-8c5be51a36ae.png

1696747691372-165805f7-3a88-4b2c-90e4-a91e3bcf9463.png auto make sense

 

ex3:

1696746935250-8135db49-e5a2-456e-8e6c-699f0e8f6aca.png

1696746895642-e0819c89-9d4c-470f-b31f-19c456a7b91f.png

Loop

1696748225701-81ea2d42-dc72-4c31-a6e0-c8942edbde2a.png

For loop

format 1:

for(statring point of iterator; test(a bool) ; increment) {

}

ex:

1696748173906-fc9295f5-275e-445c-a173-82aae6e5c83e.png

1696748185692-b07116ba-b4c6-4940-ad1b-8dfbdf15d266.png

execute at i=0~9

format 2:

int a[10]{0,1,2,3,4,5,6,7,8,9}

for(size_t i : a){

 

}

注:.use character array to do for loop

1696905321568-b4ee3865-3e74-4339-bd44-0a90c7327991.png?x-oss-process=image%2Fresize%2Cw_750%2Climit_0

anther iterator --- size_t:

1696748829055-e1c6d09a-e0cc-41b7-946a-5267e8d6b12d.png

it occupy 8 bytes ----have a good range of value

can only represent positive number

alias(别名)

ex:1696748970074-e60c81d4-1c96-4061-905e-ba8900b33d49.png1696749092529-8801974c-5c8a-4b29-a052-b5932a20b755.png

iterator can be scope inside the body of for loop

1696749204068-2cbec538-4059-4708-a5b1-9f2ba372cc34.png

iterator can be initialized outside the loop 

and neglect iterator declaration in the loop

1696749285875-a645179c-3e09-4a07-b8e8-cb1c72619ad9.png

1696749394687-243799ee-921c-41c0-8b56-3b5ae617a624.png

changeable test

1696749835089-14eeee48-8744-4c87-8463-2df00b6a8fd8.png

Attention

for (int j = 0; j < nums.size(); ++j) 

for (int j = 0; j < nums.size(); j++) 

两种写法在大多数情况下是等价的,但在某些场景下可能会有微小的性能差异,这取决于编译器的实现方式。

等价于

for (int j = 0; j <= nums.size()-1; ++j) 

for (int j = 0; j <= nums.size()-1; j++) 

等价于:

   while (j < nums.size()) { // 循环条件
        statement;
        i++; // 递增循环变量
    }

    
    while (j < nums.size()) { // 循环条件
        statement;
        ++i; // 递增循环变量
    }
    

注:1.

i++是后缀递增运算符,它先返回i的当前值,然后再将i的值增加1。

++i是前缀递增运算符,它先将i的值增加1,然后再返回i的新值。

    • i++返回i的旧值,然后将i增加1。
    • ++i返回i的新值(增加1之后的值)

对单独成行的 i++ 和 i--来说没有区别

2.对num[i++]; ---num[i]; i++;/++i;

对num[++i]; --- i++;/++i; num[i];

 

 

While loop

format:

iterator initiliztion

while( test(a bool) ){

increment;

}

 

iterator is declare outside

increment is inside

 

注:use pointer of a character array to do while loop

1696907895476-42a36fdb-ca69-44f7-808c-2c77e8fc4be8.png

ex:

1696748173906-fc9295f5-275e-445c-a173-82aae6e5c83e.png?x-oss-process=image%2Fresize%2Cw_450%2Climit_0

1696749962226-eeca36c8-25b0-4bb4-947f-e2dbac0c51f8.png

Do while loop

format:

iterator initiliztion

do{

increment;

}while( test(a bool) );

 

1696750451376-3806fecf-ab4a-4ee0-b8cf-a3d6fa9b958e.png

the test is after increment

1696750928418-0d911352-d09e-43e0-807b-0cf842bc3803.png

use pointer as a bool in loop

任何非零的指针值,被视为真(true

零值指针(NULL)被视为假(false)。

1696908190484-b6414ca0-6688-4192-bfc1-f28dea7e30fc.png

1696908618515-4469ace9-3807-4f02-8f91-1ae9827528f3.png

//name is point (to char)
if (name)
        uart_printf("Board: %s", name);

Arrays

group all variables into one single unity or collection

manipulate them as a single unit

Array is a kind of data structure.

1696751068427-9987be1d-10a9-4bb5-b735-029180981cea.png

1696751169275-850c61ed-b229-4244-967c-035ecc634b2d.png

if a array call scores

indexs to refer to certain variable

indexs begin at 0 not 1

1696751318336-7c196e49-39ad-4746-89e6-059864d03b43.png

declare and use an arrays

declare an array:

data_type array_name[ quantities of data] ;

use(read) an array:

array_name[ certain index]

 

(1)once an array is declare 

there is no data we have put in

1696752000624-2a1bdaaa-0e18-4dd9-af53-1cf97abcda1c.png

(2)arrays are indexed from 0 not 1

reading out array bound may read garbage data

1696752066036-7208ed7c-5adc-4651-b79e-5e87575114eb.png

ex:

1696751652803-f6803bfa-e744-45a7-b976-7ce2c3793112.png

write data into array

write data into array singly

array_name[index 1] = value 1;

array_name[index 2] = value 2;

……

ex1:

1696752283826-a12a8606-284f-4b80-9019-f805c4484d96.png

ex2:

write data with a loop

read data with a loop

1696752373331-d234cba7-9e72-4c19-bfea-ad5821aeeb9d.png

declare and write data into array at same time

format: 

variable_type array_name[ number of variables] {value1,value2,value3,……};

(1)is value number less than number of variables,those leaving out are initilize to 0

1696752908117-2a9cc0e6-2531-4578-9e53-60cfef4a25f7.png

(2)neglect the number of variables

the compiler will dedeuce the number for the 

number of value

1696753025172-c907085a-aaac-42bc-9c19-06a6fdb5ed6c.png

(3)once a element of aryay is declare and write 

as a constant value,it can't be modified

1696753202233-d4fa4cde-2297-4e87-ad50-f4be7f333253.png

(4)use array to do math operation

1696754421494-0e8d850c-9aac-4551-bc08-60a2257c801a.png

int element : scores 是C++中的一种范围-based for 循环语法,也被称为 "range-based for loop"。它的作用是遍历一个容器(例如数组或容器类),将容器中的每个元素依次赋值给循环中的变量(在这里是element),然后执行循环体中的操作。

在你的代码中,scores 是一个整数数组,int element 声明了一个整数类型的变量 element,而 : scores 表示从数组 scores 中取出每个元素,并将其依次赋值给 element,然后执行循环体内的代码块。

具体来说,这个循环将遍历数组 scores 中的每个元素,将每个元素的值加到 sum 变量中,从而实现了对数组中所有元素求和的操作。这是一种更简洁的方式来遍历数组,特别是当你不需要索引时。

 

ex:

1696752506014-b55bbf24-f5eb-4e0e-8380-001df21ad012.png

Pointer

a special type of variable to store addresses to other variables

1696754584796-fe3aa657-2d2b-4686-8139-84d7db88e839.png

1696754487970-4c675389-931b-4a6d-887b-fca82611b3ac.png

declare and use pointers

declare a pointer:

format1:

variable_type * pointer_name ; 

it will randomly assign a memory to pointer to point

format2:

variable_type * pointer_name {}; 

equal to 

variable_type * pointer_name{nullptr}; 

 

(1)once declaration done,that poiner can only store an address of a variable of variable_type

(2)size of pointers are same (8 bytes),no matter what variable types' address they store

(3)make sure pointer is declared and initialized ,even initialized with nullptr

(2)position of * doesn't matter

1696755383580-beac8759-1339-44ad-a3c9-dbe9db10171a.png

(3)confuse case1696755417802-9cb40872-05da-49e4-8ed2-1a7d980a713d.png

1696755426919-bd2f3888-96fa-4267-8101-f746fde944c5.png

declare and initialize a pointer to nullptr:

format 1

variable_type * p_number {nullptr}; 

(1)can only store an address of a variable of variable_type

(2)address of nullptr means the pointers are not pointed in anywhere.

format 2:

variable_type * p_number {another pointer of the same variable_type}; 

or

variable_type * p_number {number of address}; 

or

variable_type * p_number=&another pointer of the same variable_type;

or

variable_type * p_number= number of address; 

format 3:

variable_type * p_number {nullptr}; 

(or variable_type * p_number {}; or variable_type * p_number; )

* p_number = a number of address;

(or p_number =&another variable of the same variable_type;)

1696754905560-4f71543b-5040-490d-a0ec-c47a9da620a1.png

use a pointer

*pointer_name --- print out the value of the memory address that the pointer point to

pointer_name --- print out the address the pointer store

1696756512852-158995f2-f2a4-442b-8a09-69c4923ee91c.png

  1. "p_number points to a VALID address : " << p_number << std::endl;这行代码打印的是指针 p_number 的值,也就是它所指向的地址。它会输出指针的十六进制地址,例如 0x7ffd5914f9d4。这是在告诉你指针 p_number 存储的内存地址是有效的,但它没有解引用指针来获取存储在该地址上的值。
  2. "p_number points to a VALID address : " << *p_number << std::endl;这行代码打印的是指针 p_number 所指向地址上存储的值,通过解引用指针 *p_number 来获取。如果 p_number 指向有效的内存位置,那么这行代码将打印该地址上的值。如果 p_number 不是一个有效指针(例如,它是空指针),这可能导致未定义行为或程序崩溃。

write data into pointer

if there exist a variable called variable 1

the address of variable1 can be represent as &variable1

we can assign this value to a pointer

format 1:

variabe1_type * pointer_name{&variable1}

format 2:

variabe1_type * pointer_name{the number of address}

 

1696755524689-ed866510-f268-49d8-a853-75f965d25199.png?x-oss-process=image%2Fresize%2Cw_750%2Climit_0

modify the pointer value after initialization

format 1:

pointer_name=&variable2;

format 2:

pointer_name=the number of address;

 

1696755524689-ed866510-f268-49d8-a853-75f965d25199.png

1696757406370-555c1b9f-a6ae-494f-b09a-3e006274c62b.png

type of variable2 must corrospond to variable1

1696756266272-276b5328-9d56-48ef-bf87-4d728690ca6f.png

pointer to char 

???????????

declare a pointer to a char

for pointer to one character :

format:

1696756630801-3e067d13-d693-419b-b8d8-5b0617e66d42.png

1696923645725-f05c3030-3150-4117-98b8-52a071e91223.png

for pointer to string:

format:

char * pointer_name { the string };

1696923115222-287985ff-a4d8-4616-88a7-7c01391ef661.png

1696923683744-ad2cdc49-3ae2-4429-af4f-e3c13a9afa72.png

message2 is declared as a pointer to a character (string)

*message2---- the first character of the string, which is 'H'. 

print message2------ the memory address where the string "Hello there" is stored.

for pointer to characters array:

format:

char * pointer_name[] {the character array} ;

1710554768612-92281b07-f406-45ca-97c0-29a5fb55270c.png

1696923670310-79efd44a-c2d9-4325-817f-fffcb5bcaf39.png

message2 is declared as an array of pointers to characters (strings) ,even it is initialize with a string -: "Hello there"

dereference *message2,-----the first character of the first string in the array, which is 'H'. 

print message2---- the memory address of the array of pointers

modify a pointer 

注:

if initialize with string literal

but the compiler specify string as an array of charaters

so *p_message is an char array pointer

it can only be modified to another address of char array 

1696757153354-18d8d037-79ca-4c77-8992-324439e77d91.png

next will get a compiler error

print out pointer to char

print pointer to one character :

1696756630801-3e067d13-d693-419b-b8d8-5b0617e66d42.png

1696923645725-f05c3030-3150-4117-98b8-52a071e91223.png

1696924041771-a4fd99d3-e522-4269-8eda-159f6edc1f7e.png

1696924048207-9b74178f-0fb0-4872-ac3c-9c15fc1c866a.png

print out pointer to a string

1696756677810-50a46bab-1b98-4fd5-b918-069dbeb8b1b1.png

1696756816362-d67edcb1-92d0-4f6a-bc75-a621c3b5d120.png

1696757311849-71aa3676-0203-4d06-afad-b6ea53a6e961.png

print out pointer to a character array

1696908190484-b6414ca0-6688-4192-bfc1-f28dea7e30fc.png

1696908618515-4469ace9-3807-4f02-8f91-1ae9827528f3.png

use pointer as a bool in loop

在C++中,指针可以用作条件表达式,而不必显式解引用指针。

当您使用if (output)时,它会检查指针output是否为非空(即指向有效的内存地址),

如果是非空的,则条件为真,进入if语句。

这是C++的惯用做法,而不需要显式解引用指针。

1696908190484-b6414ca0-6688-4192-bfc1-f28dea7e30fc.png

1696908618515-4469ace9-3807-4f02-8f91-1ae9827528f3.png

program memory map

program.cpp→compiler→ binary executable.exe

1696764814696-f2791fe7-9490-4a6d-b9d0-4ea98d5fbf2e.png

1696765458957-a9c028ac-8cac-48c3-a1f9-b6024bc77dcb.png

binary executable(application) can run on an operating system

binary executable(application) will load in computer memory(ram)→a special section of ram called program area

1696764944935-307ee05e-e7f0-4c03-b22d-b6154ab04fe9.png

1696765483970-61b191bd-a1d6-479a-adb8-25c7007ee33b.png

 

1696766115792-d28fd5a2-59ed-4147-96da-bc23ecf215e7.png

 

1696766256418-6f8d6525-2f9e-4c63-985d-8d6a76f20c60.png

1696766282516-6e478d99-cc76-4c07-9e8d-6b2a1cf410cb.png

1696766327609-d3710bf1-61c8-48ee-9cd1-56fad4f5ed1b.png

1696766373952-8a34a24b-6e2d-4765-8c36-b813f5a8f9f8.png

1696766400714-8313c878-64d4-439e-b89e-a4ad2c830ee7.png

Dynamic memory allocation

1696766534760-ecba4361-be0a-4894-8f17-906ee52d38b0.png

use that technique to start using heap storage in memory map

use dynamic memeory allocation to get additional memory

use it to do stuff if stack memory isn't enough 

use memory allocated on the stack

1696767256166-12a1306a-1e15-48a9-a20a-4ca00388facc.png

initilize a pointer without a certain address may lead to crash

initilize a pointer with nullptr may lead to crash

1696766962442-abf045b2-ec0b-4b6a-af16-6a70aa99a91e.png

 

use memory allocated on the heap

stack

developer isn't in full of control when variable die

-----mean once a variable is declared and initialize on the stack,the memory that the variable occupy can't be controled once it is wiped out of memory

only when scope (in which it was declared) is run out,the variable can be killed 

so in the stack we can't control when the varible die

because it depend on scope mechanism(only when scope is run out)

wipe out != kill

heap

heap is a addtional memory that you can use but don't worry it will run out 

developer isn't in full of control when the variable comes to life and die

1696767248512-42e062a5-e72c-46de-af21-b1e7b22a30ea.png

1696767327118-11403396-a567-4080-a0cf-4fe83daad9f8.png

 

lifetime through scope mechanism

local_scoupe_var is limited to the curly brace

is come to life when statement is executed

is going to die when } is execute

{ } -----scope

out of the scope the varibles inside it will die --mechannism

1696768524099-a73f4bff-276b-439e-9ec8-06e6da4955ce.png

dynamic memory using "new" operator

allocation dynamic memory on heap using "new"

format 1:

variable_type * pointer_name{nullptr};

pointer_name = new variable_type;

*pointer_name = a number of address; (or pointer_name=&another variable of same variable_type )

 

format 2:

variable_type * pointer_name{new variable_type(a number of address)};

or

variable_type * pointer_name{new variable_type {a number of address} };

 

  1. int* lots_of_ints1 { new int[1008808000080000000] }; 这句代码创建了一个指向整数的指针 lots_of_ints1,并尝试在堆上分配一个非常大的整数数组,数组的大小为 1008808000080000000。这个尝试分配如此大的数组可能会导致内存不足或其他问题,并且通常不是合理的做法。
  2. int* lots_of_ints1 { new int{1008808000080000000} }; 这句代码创建了一个指向整数的指针 lots_of_ints1,并在堆上分配一个整数,该整数的值为 1008808000080000000。这个操作通常是合法的,但要注意整数是否能容纳这么大的值,否则可能会导致溢出。
  3. int* lots_of_ints1 { new int(1008808000080000000) }; 这句代码与第二句类似,也是创建了一个指向整数的指针 lots_of_ints1,并在堆上分配一个整数,该整数的值为 1008808000080000000。这是使用括号初始化整数的另一种方式,与第二句的效果相同

format 3:

variable_type * pointer_name=new variable_type;

*pointer_name = a number of address;(or pointer_name=&another variable of same variable_type )

 

注:

1.pointer_name=&another variable of same variable_type 

*pointer_name= a number of address;

2.the expression of “new variable_type”

(1)new is used to let operating system alloate memory on the heap not the stack

(2)large enough to accomendate a integer(4 bytes)

(3)this program can use that piece of memory ,other program on the system can't until i return it to the system 

ex:

1696769402232-021ff8ad-437f-4d58-8a62-f3221f4b642c.png

release and reset 

release(delete) the memory that a pointer point and reset the pointer to nullptr

if no reset the pointer ,beacuse there is no memory that the pointer point ,once use the pointer ,it will lead to crash

return memory that this pointer occupy to the system

1696769593436-a4bd6930-c514-4085-b854-461777d14d03.png

1696769855376-ad7ef4cd-2812-48c9-b697-0d4b49b4c3d5.png

can't delete twice

1696770326305-229a30d3-d9da-42ef-97ab-022b3f4de8b8.png

Dangling pointers

1696770847855-2f69574c-740c-47f1-b675-775b3ba4dc36.png

three kind of dangling pointers

1696770984051-c40c89b8-8435-42e2-9b07-0bfdb7e4e884.png

declare but uninitialized pointer

don't know which address is stored in the point

put something there may lead to a crash

1696771022287-a47de880-acea-4353-a9e6-b3a63aae588e.png

deleted pointer

release(delete) the memory that a pointer point but not reset the pointer to nullptr

beacuse there is no memory that the pointer point ,once use the pointer ,it will lead to crash

1696771148931-39f067a0-737f-4ec7-8452-ec09e2d4cf68.png

pointers pointing to same address

pointers pointing to same address will not lead to crash

but once the memory that a pointer point is deleted

the memory that the rest pointers points is deleted

once use one of these all pointers,it will lead to a crash

1696771537037-e2cec2d4-9394-47c2-beca-a47eb7498d87.png

solutions

1.make sure a declared poiner is initialized,even with nullptr 

1696773059077-6bf874eb-ec22-40da-a8c1-028323b9c0ca.png

2.reset pointer to nullptr after the memory that it points is deleted

1696773126621-e7d30519-e251-447c-a4b5-0006fce44f11.png

1696829152856-7f78ce85-4d91-4f11-b29f-0a37818bdeb5.png

1696772070621-c28482bb-f313-414c-994d-194918023218.png

When "new" operator fails

'new' operator is used to allocate dynamic memory on the heap

1696829380618-915f343a-7656-4c0f-b060-79975591de22.png

1696829396238-cc6614f8-3885-4bb5-aa20-8957cfa7b21f.png

1696829435945-0768da4a-e98d-48e3-8984-bee029fdd1f1.png

1.int* lots_of_ints1 { new int[1008808000080000000] }; 这句代码创建了一个指向整数的指针 lots_of_ints1,并尝试在堆上分配一个非常大的整数数组,数组的大小为 1008808000080000000。这个尝试分配如此大的数组可能会导致内存不足或其他问题,并且通常不是合理的做法

2.多次 少量动态分配 多个数组 也 可能会导致内存不足

 

ways to handle the problem

1696830057616-5113ea72-b544-42af-b6f1-22703a6d8dc9.png

std::exception mechanism

#include <iostream>

#include <stdexcept>

 

int main() {

try {

throw std::runtime_error("This is a custom exception.");

} catch (const std::exception& e) {

std::cout << "Caught an exception: " << e.what() << std::endl;

}

return 0;

}

std::exception 包含了一个用于提供异常描述信息的虚拟成员函数 what(),它返回一个 C 风格的字符串(const char*),用于描述异常的信息。这个信息通常是一个人可读的文本,用于解释为什么发生了异常。

当在 C++ 中使用异常处理机制时,你可以捕获不同类型的异常,包括派生自 std::exception 的异常类,以及自定义的异常类。通过捕获和处理异常,你可以更好地管理程序中的错误和异常情况。

在上面的示例中,我们抛出了一个派生自 std::exception 的异常类 std::runtime_error,然后在 catch 块中捕获并使用 what() 函数获取异常的描述信息。这有助于提供有关异常的更多信息,以便进行适当的错误处理

ex is a variable used to catch what kind of problem happen in try

1696830104903-82dcab41-5dd4-4921-9d49-ea4cf597282e.png?x-oss-process=image%2Fresize%2Cw_750%2Climit_0

1696838655108-61443966-1fc7-48dd-97c9-3b89441a69c4.png

std::nothrow

std::nothrow 是 C++ 标准库中的一个对象,通常用于在动态内存分配时,告诉分配函数不要抛出异常。在标准 C++ 中,当使用 new 运算符分配内存时,如果分配失败,会抛出 std::bad_alloc 异常。但是,如果你使用了 std::nothrow,那么在分配失败时,它不会抛出异常,而是返回一个空指针或者一个表示分配失败的特殊值,具体取决于分配函数的实现。

使用 std::nothrow 的主要优势在于,它可以让你更好地控制和处理内存分配失败的情况,而不必处理异常。这在一些低级的系统编程或者对异常处理有特殊要求的情况下非常有用。

tell the sysytem not to throw a exception when "new" operator fail

1696830260423-fd2550d3-07d6-4ce2-98f3-ef210f211c98.png

1696838635549-1001668e-08f9-4651-a0e3-6f89e53a8960.png

Null pointer safely

1696839146307-5a0f65e1-c440-4cd6-a222-25e828e43a7e.png

1696839382223-428e6324-80bc-4c13-ba8f-5309f5085a00.png

pointer can impliccitly converted into a boolean exp 

1696839164056-ecd348aa-75a0-47d6-b994-1cbe01a89f3a.png

we can deleter nullptr same as other pointers ,don't need to check 

it will cause no problem

1696839356355-7f6210e9-8493-4f6a-9cbd-873e13ebcfef.png

Memory leak

1696839561193-04d636ad-8d57-4e3a-b3d6-91fabdd25de7.png

ex1:

1696839621102-18a4688a-923c-41c3-a1a1-5cb4dcb5161e.png

ex2:

1696839751618-f706aa5d-9700-4417-8e49-d6b57b9f393f.png

ex3: 

1696839814816-c79ceec5-f38a-4c14-88b8-88b01f7d8b9c.png

out of scope ,p_number 2 can't be accessed to delete

so delete it in the scope

1696840113067-f2fe0774-1a53-4fa1-a048-e9da0d4fc19e.png

 

Dynamic arrays

1696840414396-3a06327f-bfc4-4e7c-8a98-7e196d946df3.png

declare a dynamic array and its pointer

format 1:

1696840430721-7da07a10-6d38-4e97-94e5-96dc81e01343.png

format 2:

1696841169984-c8746f14-f94d-414a-9ed4-b86cf3305732.png

use dynamic array and its pointer

1696840817554-329aebb2-28a1-4c3e-b905-9901485400bb.png

relase the memory of dynamic array 's pointer

1696840873278-a329b738-4cf4-400d-9b75-a5f35c06d696.png

dynamic array different from array

error:dynamic array doesn 't have array properties that are needed for the range based for loop to work.

1696841430718-6b350d91-23d3-453a-ad0d-ea6197de842e.png

Reference

a way to set up alias for variable

go through the reference(alias variable ) and use it in pretty much the same way we use variable

1696898977775-a6aa0880-f144-46c4-99d3-9391c7d1dc82.png

declare reference

format1:

datatype& reference_name{varible_name of the same type}

format2:

datatype& reference_name=varible_name of the same type

注:& can be used in other place

& a varibale ---the address of that variable

1696899038246-f81a149c-277d-4147-9a7d-06a11ccaf351.png

1696899489322-5cd1ac77-f760-48a5-b578-fc24359ea824.png

use reference

use reference_name as same as original variable

&reference_name-----the address of original variable

1696899180314-c0c4702d-6bc9-4b36-8893-7e8c8f0bdb3f.png

modify data through reference

modify value of original varible through its reference

1696899359572-6d951935-742b-4b01-97dc-cad2aa4c2727.png

 

Comparison between pointer and reference

similarity:

pointer: store the address of a variable 

go through that pointer and modify the original variable

difference:

1696899793207-0a7c9033-7325-40d9-acc2-7f21ab3945ee.png

1.reference need to be initialize with a variable

pointer can choose not to initilize but contain a junk address

2.reference: reference directly

don't use dereference(间接引用 *) to get the value of original variable

1696900221738-8ed6f09f-4193-484c-83de-7d660b92a6ae.png

pointer: use dereference(*) operater to get the value of original variable

1696900214113-80990247-2125-4891-808e-08583bbb1ab5.png

1696900103679-7f787095-37ae-4523-8ef8-8ebc31452bdf.png

1696900340364-357747f4-a36c-4efa-89c4-2fff99990151.png

3.reference: only reference specific varibale

if change the reference,the value of original variable will change

pointer:change another variable to reference

reference are somewhat like const pointer

1696900479744-aeaa13a2-511b-410f-93bb-117b067bf3ec.png

1696900583650-a4974acc-9105-4f58-84e7-48939a170460.png

1696900711408-eb1fd536-fa90-45e0-b3d8-2369d21361a9.png

Reference and const reference

const reference

constant key word can be used with reference

1.set up a reference(alias) to a variable

2.initialize it with const 

the reference will lose the property that modify the value of original variable through that reference

format:

const variable_type & const_refernence_name

 

ex:with no const reference

1696901533407-002e31fb-e5b2-4768-9eef-b4e344902d8c.png

ex:with const reference 

1696901610526-fb4f6944-a8f6-49f3-b6ca-75370f93b985.png

const pointer

format:

const variable_name * const const_pointer_name(& a const)

 

the pointer will lose the property that modify the value of original variable through dereference(*)

1696902478469-7c5672be-5c9f-4c4c-9902-d0e8a1196dba.png

 

Character and String Manipulation

c

model a character array and store a bunch of characters

---character array 

can manipulate it in memory

this is the way text data is stored in c++

a lot of facilities(character array facilities) built on top of this character(character array construct) to easily work with text data

use character array to model c string which are facilities we used in c to handle text data

1696903980665-20e90487-7c58-418e-b030-788183c7e2e2.png

C-String:

1696903935045-d8eb6d1c-d4bd-46d6-b4be-e08d4ff76f9a.png

built-in facilities used to manipulate text data1696904336628-96805697-9709-4874-a21c-99092bc857cd.png

c-string are not safe and convenient to work with

std::string

another type that built on the top of character arrays

it is a easy interface to work with

just focus on storing any data and doing stuff with it in our c

1696904713610-38ad5526-ccae-43d9-80ce-206079e097af.png

1696904724529-7c8cfd7a-501a-4892-b31c-0fffe952c582.png

character manipulation

some facilities in c standard library that helo manipulate characters1696904872176-5c676558-18e5-4558-af21-998b159bbb15.png

cctype library:

<ctype.h>.

https://en.cppreference.com/w/cpp/header/cctype

1696905019837-f1230096-36ba-4d94-bf88-07c4ebd4e912.png

ex1:

Non-zero value if the character is an alphanumeric character, 0 otherwise.

1696905039273-7b1eccfb-dbc3-4bbb-8c46-2aa9c6ff2f7f.png

1696905650694-1c272d06-870d-4e31-8331-3f2a68fad2f7.png

1696905204324-d31b6848-c95c-4ef9-b5e8-366b148de50c.png

1696905663078-b3a81a29-1597-4da4-b248-1e92f7cd044a.png

ex2: 

1696905216253-a3a58441-9b72-4a69-a446-e33fd367eb05.png

1696905687902-bcc6cfab-3808-4ee4-99ff-1390e9c86814.png

ex:

1696905321568-b4ee3865-3e74-4339-bd44-0a90c7327991.png

1696906250842-9f57dc0e-d2f1-4c6c-bd22-36fdf105497d.png

ex3:

1696905339066-9dafcc65-d8ce-4a8a-8e42-121781ae0d8a.png

1696906261901-073b281f-6ce4-4a7b-80ba-95f38c1ceb5f.png

ex4: 

1696905380507-e79d6ac6-84a2-432d-a77b-2384d2a9508f.png

1696906282965-cf949058-cc8a-4d27-8aee-4a45256b3cc5.png

C-String manipulation

c-string library:

<string.h>.

https://en.cppreference.com/w/cpp/header/cstring

how to manipulate c string----contatenate ,put together ,copy,,,,

std::strlen

length of a string (ignore null character of the string)

 

sizeof operater :

also can calculater the length of a string (include null character of the string)

 

ex:

1696906599097-261fc871-4a63-40ba-9a51-25a5e9a118b1.png

1696908343737-8442ba88-4fd1-4aec-88f0-84f3b21c0982.png

std::strcmp

compare strings in lexicograpgical(词典学) order

format:

std::strcmp(string1,string2)

 

negative value if string1 appears before string2 in lexicographical order.

zero if compare equal.

positive value if string1 appears after string2 in lexicographical order.

 

ex:

1696907439045-6610eeb6-56d3-4ded-bdcd-8b023cf02f3c.png

1696908355036-8917ee8a-ae87-4908-a409-c93099d58c56.png

1696908550128-21f9b2b8-4c3d-4152-a0e8-7b99223d734b.png

ex: 

1696907428081-ae3eced7-fc18-40be-b484-46ce0181f170.png

1696908562212-10f1e771-0aa0-4cce-a856-7c751badb57d.png

 

  1. const char * string data1 { "Alabama" };这行代码定义了一个指向字符常量的指针 data1,并将它初始化为指向字符串字面值 "Alabama" 的首字符的地址。换句话说,data1 是一个指向字符串的指针,而该字符串是不可修改的,因为它是一个字符常量。
  2. const char * string data1 []{ "Alabama" };这行代码定义了一个字符指针数组 data1,并将其初始化为包含一个元素的数组,该元素是指向字符串字面值 "Alabama" 的首字符的地址的指针。这表示 data1 是一个指针数组,其中的元素都是字符指针。
  • 第一行代码定义了一个单一的字符指针,它指向一个字符串字面值。
  • 第二行代码定义了一个包含一个字符指针的指针数组,该字符指针指向一个字符串字面值。

根据需求,可以选择使用其中一个即可

 

std::strchr

find out the first occurrence of target character in a string

format:

std::strchr(string,target character)

 

std::strchr函数的返回值是一个指针

指向字符数组 str 中第一次出现字符 character 的位置的指针,如果未找到字符,则返回空指针(nullptr

1696907663587-3f4e7b3b-1fe8-4201-bea7-f59d226c893e.png

1696908584189-ee16e1a4-78dd-45a4-ad78-c8e4d80da8db.png

std::strrchr

find out the last occurrence of target character in a string

ex:
ouput is a pointer

1696908190484-b6414ca0-6688-4192-bfc1-f28dea7e30fc.png

1696908618515-4469ace9-3807-4f02-8f91-1ae9827528f3.png

C-string concatenation and Copying

std::strcat

allow to join strings

format

string1=std::strcat(string1,string2)

ex1:

1696924328537-88e65599-0dc7-456d-8373-9bbe1f02b038.png

1696925901096-2d2fd97b-b2cf-46bd-8cc3-22202a26346d.png

ex2:

1696925271180-194acc9f-b12b-4b32-a013-81a917da5d09.png

1696925942159-070120df-9fe4-4c38-bbb3-46e501f1d035.png

std::strncat

format

string1 =std::strncat(string1,string2,the number of character copied from string2)

1696925498027-0ae5a89e-9ee4-4c2e-9abf-fdc97bf84319.png

1696926245690-9af8d592-f0ea-4174-bc76-52553eaedb73.png

std::strcpy

format:

char_pointer1=std::strcpy(char_pointer1,char_pointer1)

 

copy from char_pointer2

paste in char_pointer1

 

1696925568898-d5a11d2c-95d2-4a27-b18b-d895059cdc42.png

1696926047690-bb98656d-7b9e-440a-9e9f-31899b05648a.png

std::strncpy

char_pointer1=std::strcpy(char_pointer1,char_pointer1,the number of character copied from char_pointer1)

1696925799101-ba382be2-c611-4368-bd44-92853e04b513.png

1696926279090-0276f15d-27a0-4a7f-a40a-eae22ceb2ea8.png

Introduction std::string

a variable(data) type make it easy to work with string data

and don't need to worry unly little details( array bounds,the null character,etc)

1696926464878-ab74e421-0192-4df5-9492-c3c7b7849a12.png

string library

1696926631083-4d9fffeb-9e87-4a42-80d4-92f274e2ca95.png

declare std::string variable

1696926707259-3902e6eb-33c2-4188-ad99-45009b923977.png

1696926830434-3e02bca1-3cdf-4c81-bc90-00d2921244b4.png

modify std::string1696927304174-42387cd9-5578-4824-ac7c-3dd5fc7d44af.png

1696927321238-b1433028-4d4e-436a-b67e-71ac199842be.png

One definition rule

1696927430722-ff7cf937-4d22-4906-a691-51b800af21f0.png

tune to:

1696927456624-35c0c663-c1c3-491a-a3b3-89c7bf28f1c2.png

ex for variaable:

1696927754652-29f472f9-af9e-457b-8f37-058f4ce1bab0.png

1696927779185-6bd9f895-75b2-4b97-b485-8ac19606d40c.png

exception for class:

1696927483360-7840fe2e-fc79-443c-83c0-fe423158e20a.png

First Hand on C++ Functions

1696928161231-80de212d-0b34-4c38-a069-8c1103a71787.png

1696928197155-86137764-dd00-4ba7-a9b8-80ba0d20bb04.png

define a function

function with input and output:

1696928360538-db7aa608-48cb-4546-a6b3-03dba8415b18.png

1696928253193-64be878b-1e0d-4d47-a790-c455411e6623.png

ex:

1696929217802-196683bd-52f4-4fcd-8ccd-314788be9749.png

function without input or output:

1696928529650-d406e113-4e06-4cb6-8b1c-64ebf5fe3ebb.png

viod ------this functin is not going to return anything

empty () -- no input

ex:

1696928846494-a4738354-2bb0-49e8-968e-7b316dd9476e.png

ex:

1696929468851-316a9b76-2360-4d4c-8f00-7a9c6c0bd36b.png

ex:

1696929528634-d091088d-3411-452d-92f3-988da25edf7b.png

function is unique because of its signature:

function signature include function name and function parameters

1696928652252-c11ce5b3-bbe9-4e63-bb6e-be1ca5e15226.png

use a function

use a function with input and output:

1696928740067-4d82e339-12f8-4337-abdc-d9e4566ab953.png

use a function without input and output:

1696928805591-d9722f1a-1a32-4dce-8ecf-290904eb5db0.png

ex:

1696929637762-918c604c-9845-4405-afdc-e65e0aa383df.png

argument scope

自变量范围

operations of inputs in the argument scope will not change the value of inputs outside the argument scope 

 

1696929654003-d9e46f10-a603-4114-b274-fdec5903bcdd.png

 

1696929905830-ee9b5ccd-a53f-41d2-a1f7-ee6739f82c75.png

1696929919362-305d13d0-801d-4df6-ad80-ed5b3c5ac41e.png

1696930133955-a5675a2d-ea62-430a-adfe-d1c15805a0ca.png

1696930225834-8684461c-9af5-4bdb-aca4-3ec0b8139e3d.png

compiler will make copy of h and i

and assign them to a and b

Function Declaration and Function Definitions

used to define a function first(even it is out of int main ) and then use

1696941516187-7bccef25-30d1-43a2-85f8-e2f21a8761cf.png

1696941524970-05e0659c-513e-4248-91e3-f87843ddd87d.png

 

in this part

seperate function in to two part

1.Declaration(prototype)----declare a function before use it (even ahead of int main )

2.use that function bewteen declaration and definition

3.Definition----------------define a function after use it (even after the } of int main)

1696941365545-6f7e49e7-97f2-43fd-ba99-c238f61ad009.png

ex1:

1696941142855-49161a2c-e9a2-4746-aed1-0055b39c01a9.png

ex2:

1696941770796-0ecbab82-b407-4c66-8283-4018d68212f4.png

1696941802593-ec61f0c1-d923-4f58-9e97-f9149f83b078.png

Function across Multiple Files:Compilation Model Revisited

ex;

1696942250909-1511d2cd-a710-47a6-be5d-8c00c5f9c187.png

1696942266288-d758c918-b7b8-4833-a11a-ffce53af7ef3.png

1696942307907-79b7c7fd-086d-47e5-8dd7-9185501164fc.png

ex:

1696942333573-15297bba-3743-4195-af5e-81f70308ec68.png

 

1696942620342-4ad399f0-fdca-4fe9-af21-4ccf8fbad22f.png

 

1696942707543-2c8b271d-2d13-419f-a6d1-186e26f8f189.png

definition of function can live any where in code

the linker is smart enough to parse entire project(include multiple files) looking for the definition of function

it keep looking until it finish looking in all translataion units

when it can find the definition of function but you use it,it will give a linker error

ex:

one file

1696943026590-22e442bc-37bf-486e-b31a-c4cb8d017a97.png

ex:

function across multiple files

1696943231859-ce835782-5595-4770-8d69-47909476343f.png

1696943259000-545526f4-07b2-4d2c-a86a-782231201ff3.png

1696943129055-59ad48e1-9574-48f4-80cf-bc480cf2203b.png

1696943272294-0e7e0924-9bfd-4b07-ab57-ec9a472c2256.png

1696943151868-eca1d4f8-da5d-4aeb-99df-cf3cf8575cf6.png

1696943337246-48565a79-f2e8-4149-a371-c4963df76d05.png

if compare.cpp is deleted

1696943861644-f3ddde08-fb0a-4e61-aff9-ac39f21b26d8.png ld is a linker error of gcc

1696943938194-c7d09e86-a4e6-4d9f-adeb-c37f3fe78cbd.png is a linker error of msvc

Pass parameter through function 

pass by value

Pass parameters through function by value(mechanism)

---------if pass a variable to a functin,it is going to pass a copy of actul variable

insider the body of funtion,it just work with the copy of actul variable

the copy of actul variable disappear out of scope of function

1696944522430-af6c02c9-7d1e-4dd4-88d7-d4f71c3f4319.png

ex:

although the varible_name is same as para_name

parameter only receive the copy of original variable

1696944228348-488ead35-6813-420c-866a-747fd8eee10e.png

1696944599486-b0f0c795-e455-44b4-9487-4091be059fc1.png

 

pass by pointer

Pass parameters through function by pointer

---------if pass a variable to a functin,it is going to pass a copy of actul pointer

insider the body of funtion,it just work with the copy of actul pointer

the copy of actul pointer disappear out of scope of function

1.operation to dereference of pointer inside function will change the value that the copied pointer point to(if no operation to the copied pointer inside function ,the copied poinet =original pointer)

so if no operation to the copied pointer inside function,operation to dereference of pointer inside function will change the value that the orginal pointer point to

(because the machanism of pointer)

2.if operation to the copied pointer inside function ,it will not change orginal pointer

 

ex: 

although the pointer_name is same as para_name

parameter only receive the copy pointer of original variable

use the copy pointer of original variable to dereference(*) the value of original variable inside function

 

1697187992059-799606c6-9a85-4c00-9fa8-e6cc7756a0e8.png

1696945634531-ac2d6af4-224d-4677-8414-ccc763b68524.png

pass by reference

Pass parameters through function by reference

----------------the operation to the alias of variable inside function wil change the variable

the change we do to reference in function are going to be visible when function retun

 

ex: 

the alias of variable is passed to parameter

the operation to the alias of variable inside function wil change the variable

because of the property of reference

 

the change we do to reference in function are going to be visible when function retun

1696945261585-4dc95332-43c9-4982-ba63-b81f818a3532.png

Get things(data) out of function

1696945712593-9dede430-ff1e-434b-8bf3-e8cc76ebf183.png

two ways to get things out of function

returning from function by value

use return in the function to pass output to the outside of function

format:

1.output variable should be initialize in the function

 

2.return output_variable;-----at the bottom of function

 

3.funtion_name(parameters) -------is the value of output variable

 

In detail:

it will copy whatever of output variable the function return and store a copy of it

in funtion_name(parameters)

but don't rely to this 

we should set output_variable same inside and outside varible to let compiler optimize to jump the process of copy when the output contain moutainless data

 

like:

return output_variable; -----at the bottlom of function

output_variable=funtion_name(parameters) -------is the value of output variable

 

ex:

two result is different in sum function and main function

1697189585827-7e68a2d9-2214-44c6-8942-8f5b89f97f02.png

 

1697190606318-f156035b-13a7-4e73-86e5-1be586758463.png

 

 

ex:

1697190831273-8c092cd4-2d2a-4557-9111-463682b2530d.png

1697191196434-f8093967-d058-40f7-a6ad-cdce8fc0262e.png

the address of output inside and outside funtion is same

it is beacuse the compiler don't execute the copy opertaion

it just reuse the address of output inside funtion and copy it to the address of output outside funtion(return by pointer)

it is a optimization

input and output parameter

parameter is something to be passed into function

it is not only input but also can be output 

use function parameter as channials to get thing out of function

 

注:output parameter should be initialize out of { } of the function

input parameter syntax of void:

varible:

1697185824914-0fcdaf13-ff82-45b0-87f3-8380fd2f3a71.png

std::vector --- dynamic array

std::string/std::vecotr can be regarded as a variable type

pointer:

reference:

output parameter syntax of void:

reference :

why?

the change we do to reference in function are going to be visible when function return

注: void funtion can't output anything

the output of void function is actually in ( ) of void function in input format

pointer:

1.operation to dereference of pointer inside function will change the value that the copied pointer point to(if no operation to the copied pointer inside function ,the copied poinet =original pointer)

so if no operation to the copied pointer inside function,operation to dereference of pointer inside function will change the value that the orginal pointer point to

2.if operation to the copied pointer inside function ,it will not change orginal pointer

1697185839221-f5ca92e6-4915-47fb-ab2a-0645cc7d14d4.png

ex:reference output

1697186279043-49be73c6-94dc-4394-81e9-325c392f6090.png

1697186300955-5b21f09c-4111-4242-8e94-c99e11830dad.png

1697186289754-d28ef8d9-ca28-44b2-b0fd-8177e4a905c4.png

1697186329099-069ba465-c78a-43c8-9f26-ba2e1a6f66b4.png

1697186527467-f1c19c87-4128-49b0-b0c6-d18b4a78f52b.png

1697186534595-f217b07c-b0bd-4215-bf28-a1401f881d34.png

ex: pointer output

1697187727080-039f6523-e5db-4a18-aad6-93fc2fa02e42.png

1697187889652-a24bfa61-5df4-4904-bf72-0be13e72f554.png

1697187906535-e6b774ae-9bd5-4102-9d43-c55c99a69389.png

1697188879372-a9229eeb-d395-43d3-81cc-f8373e706bf2.png

1697188874351-6de3d57a-8655-49ed-b25d-3490c01bc671.png

1697188908444-c5b1449a-0abe-4f9a-b798-678d7e40ed47.png

 

Function overloading

function overloading is a mechanism to have mutiple functions of same name in c

but taking different parameters 

1697197301032-89cd4699-1f56-408e-825e-b3e7b2394a87.png

1697197322391-c6cc13f0-806d-44a6-8597-138053012b20.png

how to set up function overload based on different function parameters

overloading with different parameters

can overload the functions of same name 

if the order or number or type of parameter is differert

1697198412339-65190994-d5d3-42d5-84b3-41c241c0a359.png

 

ex:

1697198658952-97a2942c-4886-48a7-a23f-619ef6a715d4.png

1697198699801-1c76ca23-4818-4769-be6d-2248a9a3ef27.png

1697198725705-a74806d0-6805-4d9e-8e01-2e4ca0267c78.png

1697198741245-e416ed6c-70e9-4b38-ae5b-4e8402274b33.png

Lambda function

1697198781100-1f6623e6-3182-450e-bdc9-a90b2ce35e34.png

get them to do things directly

 

declare lambda function

format1:

[ ]( )->return data_type{

 

};

format2:

auto variable_name [ ]( )->return data_type{

 

};

 

[ ]----- a capture list

( )------parameters to put in

>return data_type---------this lambda function is going to be forced to return that data_type (implicitly converted data_type )

even you pass it to parameters that are not data_type 

 

 

ex:

if dont give the function a name and assign the entire lambda function a variable and use it

the lambda function will not be called (excuete)

1697200403665-1c45cca6-3601-404d-b9ad-d163a9cdde37.png

1697200409903-2afa3e8e-b925-4226-b402-c9e274aee3a3.png

give the function a name and assign the entire lambda function a variable

the variable is a handle and use it to do things with lambda function

1697200664552-cca1727c-0550-45bd-a117-b8951ee6fadf.png

1697200409903-2afa3e8e-b925-4226-b402-c9e274aee3a3.png

1697201318602-09d1e466-2982-469e-85c2-55ad91a8795e.png

1697200409903-2afa3e8e-b925-4226-b402-c9e274aee3a3.png

use lamda fuction

format:

var_of_lambdafunction(the value of parameters );

 

ex:

give the function a name and assign the entire lambda function a variable

the variable is a handle and use it to do things with lambda function

1697200854655-c6b227f5-4a63-4ab7-b9a5-645b981dd0f6.png

1697200878482-1520ffd1-2410-4206-b03d-3a9d3f185926.png

1697201421886-a07e3490-1302-4d1c-bdca-7b25a74d7e53.png

1697201427621-01214913-1934-41fb-b4cf-6de343b1727f.png

1697202478756-36ab10bc-d6c9-4b54-92ff-98d6da22bed0.png

1697202485595-fd81f45a-7e43-4d36-8cbe-10183874d2b5.png

1697202393629-9cfb2444-aebf-4cee-bb93-e2457b031f11.png

1697202419391-60b2cc6a-1d17-4ebe-9321-bd5507d5ca62.png

 

directly declare and use lambda function

format1:

[ ]( parameters){

 

}( the value of parameters );

 

format2:

auto variable_name=[ ]( parameters)->return data_type{

 

}( the value of parameters );

 

three equality: 

1.[ ]( parameters)->return data_type{

 

}( the value of parameters );

2.variable_name;

3.var_of_lambdafunction(the value of parameters );var_of_lambdafunction(the value of parameters );

 

ex:

1697200999933-02e3f546-9488-4f66-87a4-d357ae3978e6.png

1697201040406-87ac4568-121b-435f-9d58-e6272debfa89.png

1697201225778-ad8ccb20-9431-4089-96bd-60cdce0770f5.png

1697201257640-99ead833-07e3-4132-a2ef-ec075cab473e.png

lambda function is called (executed),but do nonthing on the console

1697201648799-94c053bf-73bc-4a65-b2e6-d17fe073155a.png

1697201656066-2ccc945e-318d-4e85-8343-58168b912a4a.png

1697202559251-9b4b2312-e0b1-4806-b9ba-51f70ec38e89.png

1697203416985-b2dfba66-646e-4010-847a-a695d66609bc.png

1697203502397-7bd49d3b-74f5-4a01-b72b-af5a1e5536c1.png

1697203416985-b2dfba66-646e-4010-847a-a695d66609bc.png

capture list:

use things (that is declared and initilized out of the scope of lambda function ) inside the lambda function

but these thing are not the parameters in ( )

 

1697203765822-339b4298-7fc3-45a4-94b9-37f7063ed262.png

1697204345622-4a1e061d-2de2-4e04-a954-92200f3de1be.png

capeture by value:

1697204506426-5a4e01a4-360b-4e56-8ee4-e82521a941fd.png

1697204370712-f3ff8c72-2882-492e-b6db-c0a2dad8a1c4.png

1697204381709-2a6628e6-d1c8-4b2d-8b46-d417c62ebb04.png

capture by reference:

1697204428738-7873c79a-d78f-4782-b5d6-9640d089820d.png

1697204456583-1481c111-cce1-4e29-a938-6328a564f0ab.png

capture all in text

[=]----------------------------------------------capture everything by value

1697204635992-1cd5d014-b482-43d5-acb9-68a9432dc124.png

1697205010947-abe18836-a627-44a3-bce4-ac845a37091a.png

[&]----------------------------------------------capture everything by reference

1697204844993-9f96d3b3-c35c-4357-977b-8f4885dff52d.png

Function template

or template function

 

a mechanism to set up blueprint/template for function

have one point control to set up function

compiler will generate the actual function that get called

function template are an effort to solve problem like overload of function

1697205362387-a2433933-0774-4f3a-a25f-f03c015ab09c.png

 

set up a bluepoint/template for function

the compiler will use this blueprint/template to generate an actual function when call(use) this function

try out function template

declare funtion with blueprint:

syntax:

tempate <type_name T>

T function_name(T a,T b);

注:The overall syntax is a blueprint

  1. template: 这个关键字表明接下来的代码是一个模板声明,它告诉编译器后面定义的类或函数是一个模板。
  2. <typename T>: 这是模板参数列表,用尖括号 <> 括起来,里面是模板参数的声明。在这个例子中,typename T 表示声明了一个模板参数 Ttypename 是告诉编译器 T 是一个类型参数的关键字。你也可以使用 class 关键字替代 typename,效果是相同的。
  3. T: 这是模板参数的名称。它是一个占位符,表示在模板被实例化时将要替换为实际的类型。你可以使用任何有效的标识符作为模板参数的名称。

因此,template<typename T> 这行代码告诉编译器,接下来定义的类或函数是一个模板,它有一个类型参数 T。在模板被实例化时,T 将会被替换为调用时传入的具体类型。

实例化:根据实际的参数类型生成相应的函数实例

 

define function with blue point:

syntax:

tempate <type_name T>

T function_name(T a,T b){

 

}

equla to:

auto function_name(auto a,auto b){

 

}

ex:

1697205443072-c678e60f-ae36-4a25-8edd-aee551c975e7.png?x-oss-process=image%2Fformat%2Cwebp

use/call funtion template

function_name<output type>( parameters)

 

注:<output type>----force the output to be that type if it can be reasonable

 

注:

1.The compiler will use this blueprint to generate an actual function 

based on what variable_type is called(used) in this function

In detail ,if firstly got int input,it will generate int version function (that is called int template instance)

it will resued it when for double typeinput ...etc

2.function template are just blueprint., they are not real function declaration and definition

3.when call/use the function,the compiler will generate a real function declaration and definition(also 

known as template instance )

4. The real function declaration and definition(also known as template instance ) will be reused when a similar function 

5. If use funtion template ,it mean all inputs and output must be same type

or compile error(except some reasonable cases)

1697336068629-3e8b8e0a-9a9d-416a-a245-f877a1d29f75.png

aka---also known as 

1697337572063-45e1fd28-2810-44cb-bcab-c32b75395e86.png

ex:

function overload

1697335553525-fae27f15-31bf-457c-8ad0-9fb35913ef3f.png

use function template

1697335577562-ddba3668-fe1b-4666-bb33-dffb3e031093.png

insight of function template 

1697336824834-37e0e25f-ecb3-4334-b840-bf96b2b39ab9.png

1697336837000-cfbc16a4-6679-4448-836a-4fabd0ee0b5e.png

1697336879678-4e3f9437-531d-4e5d-98f7-2f0975ad841c.png

 

Template type deduction and Explicit arguments/parameter

Template type deduction

When call the function template, the template instance will be set up based on the type of input para/argument.

1710584380758-73b89cf2-e40a-4089-8d7f-e4ad57980ae6.png

----------template parameter (it is what you have in your template function declaration or defination)

1697338441129-04f4a668-695c-4cc3-b4e7-8648a1dba38d.png

---------template argument/parameter (it is what you call/use template )

Explicit arguments/parameter

when call/use the function

sytax:

function_name<varibale_type>(paras)

1697338155566-39f158b5-cbd6-4bfc-9a3c-286cd258ce58.png

  1. 模板类型推导(Template Type Deduction)
    • 模板类型推导是指编译器根据函数调用或对象创建的上下文,推断模板参数的具体类型。
    • 当我们调用模板函数或创建模板类的对象时,可以省略模板参数的类型,而让编译器根据传递给模板的参数来推断类型。
    • 例如:
cppCopy code
template<typename T>
void foo(T a) {
    // 函数体
}

int main() {
    foo(5); // 在这里,编译器会推断 T 的类型为 int
    foo("hello"); // 在这里,编译器会推断 T 的类型为 const char*
    return 0;
}
  1. 显式参数(Explicit Arguments)
    • 显式参数是指在调用模板函数或创建模板类对象时,显式指定模板参数的类型。
    • 即使编译器能够推断出模板参数的类型,我们也可以显式地指定类型。
    • 例如:
cppCopy code
template<typename T>
void foo(T a) {
    // 函数体
}

int main() {
    foo<int>(5); // 在这里,显式指定了 T 的类型为 int
    foo<const char*>("hello"); // 在这里,显式指定了 T 的类型为 const char*
    return 0;
}

模板类型推导允许编译器根据上下文推断模板参数的类型,而显式参数允许我们明确地指定模板参数的类型,即使编译器也能够推断出类型

Template parameters by reference

syntax:

template <type name T>

const T& maximum(const T& a,cont T& b);

 

ex:

1697338911924-88d2c4cd-27bf-406c-963c-b60acca00f2c.png?x-oss-process=image%2Fresize%2Cw_674%2Climit_0

1697338925697-bfba8a30-42a3-47c1-b7a4-3d503ed21739.png

confue compiler and lead to a compiler error

1697339278019-d8c2f718-a736-4bbb-b923-692dda132ec4.png

the solution:

substitute 

maximum(a, b)

with

maximum<double>(a, b) using explicit argument/parameter

Template Specialization

declare a template specialization:

template<>

variable_type function_name<output type>(variable_type parameters)

 

define a  template specialization: 

template<>

variable_type function_name<output type>(variable_type parameters){

 

};

 

1.it is like funtion but not funtion,

it is a appendix to a funtion

2.template specializtion is a feature to bypass the default mechanism of how templates work

3.template specializtion is a mechanism to tell the compiler for a template function:if pass compiler this type template funtion(this type template instance),don't do the default of replacing the argument type for the template parameters.instead use the implement that give in template specialization.

 

1697340154374-26010dfa-ae94-487b-b320-d3f044fc2136.png1697341388104-d2fc9469-169a-4f72-bf89-a53a8b835122.png

1697341380453-b6dc4e26-85c7-4421-a680-91ba1ed736c2.png

Class Template 

or template class

 

 

为什么需要模板类? 和模板函数(Function template)基本相似

是为了对 模板/函数 更通用,适用于不同的数据类型

format:

cppCopy code
template<typename T>
class MyContainer {
private:
    T data;

public:
    // 构造函数
    MyContainer(const T& value) : data(value) {}

    // 获取数据
    T get_data() const {
        return data;
    }

    // 设置数据
    void set_data(const T& value) {
        data = value;
    }
};

在使用过程中,模板类通过实例化(instance,这个词在function template中有提到)就

 

模板对象(Template Object)是通过实例化类模板(Template Class)创建的对象

 

Intro to C++20 Concepts

concept is a mechanism to used to set up constraints or restrictions on template parameters in our function template

ex:can use concepts to make our template function to be only called with integers 

and if call that template function that isn't int ,it will give a complie error

1697342463419-768d2b7b-0fde-4ad8-bcc1-1a97202e3162.png

two sides to concepts in c

#include<concepts>1697342534737-b5d6c262-46f8-4d0f-a8b2-81bdba5c3308.png

use to enforce ur template function to be only called in some particularle constraint

1697342559827-07a1ee85-a4a9-4685-816b-237aa9878d43.png

concepts in standard library

use concept when defining function

syntax1:

template <typename T>

requires std::integral<T>

T add (T a, T b){

return a + b;

}

syntax2:

template <std::integral T>

T add (T a, T b){

return a + b;

}

syntax3:

auto add (std::integral auto a,std::integral auto b)

return a + b;

syntax4:

template <typename T>

T add (T a, T b) requires std::integral<T>{

return a +b;

}

syntax5:using type traint

template <typename T>

requires std::is_integral_v<T>

T add (T a,T b){

return a + b;

}

 

C++ 中的 type traits(类型特征)是一种编程技术,它允许您在编译时查询和操作类型信息,而不是在运行时。这对于泛型编程和模板元编程非常有用,因为它可以帮助您根据类型的特性来执行不同的操作或生成不同的代码。

C++ 标准库提供了一组类型特征的模板类和函数,这些类型特征允许您检查类型的属性,例如:

  1. 是否是整数类型
  2. 是否是浮点数类型
  3. 是否是数组类型
  4. 是否是类类型
  5. 是否是指针类型
  6. 是否可以执行某个操作(例如,是否可以进行复制构造)

一些常用的 type traits 类包括 <type_traits> 头文件中的 std::is_integralstd::is_floating_pointstd::is_arraystd::is_classstd::is_pointer 等等。

以下是一个简单的示例,演示如何使用类型特征检查一个类型是否是整数类型:

 

1697361738862-5c33baa8-58ba-4764-ab03-e46580244b20.png

build own concept(custom concepts)

set up own concept 

(1)use type trait:

syntax:

template<typename T>

concept concept_name = std::type_trait_v<T>;

 

"_v" 的作用是获取类型特征的值(value),它返回一个 bool 类型的常量表达式,表示所查询的类型特征是否为真或假

 

(2)use require clause

syntax:

template<typename T>

concept concept_name = require (T a, T b){

require clauses;

}

 

require ( T a,T b) means a,b must be the type of template T(T has be constrain by concept before)or it will compile error

 

ex:

std::is_integral 是一个 C++ 标准库中的类型特征,用于判断一个类型是否是整数类型。

1697363886550-49b7ac9a-7948-46cc-b258-17f65de2033e.png

1697363886550-49b7ac9a-7948-46cc-b258-17f65de2033e.png

use own concept (custom concepts) in defining function

syntax1:

template <typename T>

requires concept_name<T>

T function_name(T a,T b){

detail of function;

}

syntax2:

template <concept_name T>

T function_nameT a ,T b) requires concept_name<T>{

detail of function;

}

syntax3:

auto function_name(concept_name auto a, concept_name auto b){

detail of function;

}

require clause:

1697370843598-65e34f23-be63-4a3f-93bc-ebff7ba73541.png

in setting up own concept

 

only check but if it don't satisfied ,no bool is produced1697371542977-d2319d78-d8b9-4bb1-a89d-4c4d0a1d7dd1.png

 

1697371565807-c6c2ce0e-4dc1-4084-8287-85fb4102c41c.png

 

 

noexceptnoexcept 是一个异常规范说明符,它表示在执行 {a + b} 表达式时,不会抛出异常。这意味着 a + b 必须是一个不会引发异常的操作

-> 在这里用于指定 std::convertible_to<int> 作为 {a + b} 表达式的期望返回类型。

std::convertible_to<int>: 这部分表示 {a + b} 的结果必须是可以隐式转换为 int 类型的。这意味着 {a + b} 的结果必须是一个可以被转换为 int 的类型

1697371575848-cadd401e-8370-4c0a-95bf-43cbcd1b3adc.png

Logical combinations of concept

1697373082198-c8d07372-92ec-48a6-a634-b9d0c3ef3f7f.png

it is conbined when using the concept

case1:

conbine two own concepts when using the concept

 

format1:

if two own concepts have set up

 

template<typename T>

T function_name  concept_name1<T> logical_operator  concept_name2<T> {

detail of function;

}

format2:

template<typename T>

T function_name  requires(T a, T b){

require clauses;

} logical_operator  requires (T a, T b){

require clauses;

}{

detail of function

}

case2:

combine two standard concept

 

template<typename T>

T function_name  requires std::concept1<T> logical_operator  requires std::concept2<T> {

detail of function;

}

ex:

1697373400358-990c66a3-68b0-4a65-8a98-175aa39b0c67.png

 

concepts and auto

used when defining function

format1:

for standard concept

 

std::concept auto function_name(std::concept auto parameters){

detail of function;

}

 

format2:

concept _name auto function_name(concept _name auto parameters){

detail of function;

}

1697375076957-7b901b59-2b70-492d-ab3b-2e6584ef7b4a.png

used when declare variable

1697375405751-acecbc16-b520-475b-b302-8de5ba87755f.png

useless

1697375418535-c7823431-1630-4edd-b443-17323cc1986a.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值