These snippets are about the syntax for defining functions. They are not about how to achieve tasks with these
functions. The tasks are just placeholders and the C++ STL already provides much better alternatives for most of
these algorithms.
// Parameters by constant reference// - We don't want to copy// - But we also don't want to change itboolhas_element(conststd::array<int,1000>&a,intelement){for(intx:a){if(x==element){returntrue;}}returnfalse;}
123456789
std::array<int,1000>v{};for(size_ti=0;i<v.size();++i){v[i]=i;}if(has_element(v,400)){std::cout<<"Array v has the element 400"<<'\n';}else{std::cout<<"Array v doesn't have the element 400"<<'\n';}
1 2 3 4 5 6 7 8 910111213141516
voidminmax(inta,intb,intc,int&minimum,int&maximum){if(a>b){minimum=b;maximum=a;}else{minimum=a;maximum=b;}if(c>maximum){maximum=c;}if(c<minimum){minimum=c;}// values of external variables are set}
1234
intmaximum;intminimum;minmax(5,3,8,minimum,maximum);std::cout<<"Minimum and maximum: "<<minimum<<", "<<maximum<<'\n';
// Pass pointers by value: https://youtu.be/xGDLkt-jBJ4?t=869// As small as the reference and no other level of indirectionvoidpointer_to_cube(std::shared_ptr<int>p){if(p){*p=*p**p**p;}}