Basic review of c ++ (I) and basic review
1 C ++ program
1.1 the miminal cpp
int main(){} // the minimal C++ program
Thought 1: Why does the minimum program have no return statement?
1.2 Hello World
#include <iostream>int main(){ std::cout << "Hello, World!\n";}
Question 2: What is the difference between \ n and std: endl?
1.3 functions
#include <iostream>using namespace std;double square(double x){ return x*x;}void print_square(double x){ cout << "the square of " << x << " is " << square(x) << '\n";}int main(){ print_square(1.2);}
Think 3: is it necessary for square to write a template function?
2 Types, Variables and Arithmetic
2.1 definition
ADeclarationIs a statement that introduces a name into the program. It specifies a type for the named entity:
-TypeDefines a set of possible values and a set of operations (for an object ).
-ObjectIs some memory that holds a value of some type.
-ValueIs a set of bits interpreted according to a type.
-VariableIs a named object.
2.2 sizeof (type)
bool // Boolean, possible values are true and falsechar // character, for example, 'a', ' z', and '9'int // integer, for example, 1, 42, and 1066double // double-precision floating-point number, for example, 3.14 and 299793.0
2.3 initialization
double d1 = 2.3;double d2 {2.3};complex<double> z = 1; // a complex number with double-precision floating-point scalars complex<double> z2 {d1,d2};complex<double> z3 = {1,2}; // the = is optional with {...}
vector<int> v {1,2,3,4,5,6}; // a vector of ints
"=" Is traditional and dates back to C, but if in doubt, use the general {}-list form
C ++ 11 auto
auto b = true; // a boolauto ch = 'x'; // a charauto i = 123; // an intauto d = 1.2; // a doubleauto z = sqrt(y); // z has the type of whatever sqrt(y) returns
We use auto where we don't hav e a specific reason to mention the type explicitly. ''specific reasons ''include:
-The definition is in a large scope where we wantMake the type clearly visible to readersOf our code.
-We want to beExplicit about a variable's range or precision(E.g., double rather than float ).
3 Constants
3.1 const and constexpr
Const: Meaning roughly ''' I promise not to change this value ''. this is used primarily to specify interfaces, so that data can be passed to functions without fear of it being modified. the compiler enforces the promise made by const.
const int dmv = 17; // dmv is a named constantint var = 17; // var is not a constantconstexpr double max1 = 1.4∗square(dmv); // OK if square(17) is a constant expressionconstexpr double max2 = 1.4∗square(var); // error : var is not a constant expressionconst double max3 = 1.4∗square(var); // OK, may be evaluated at run time
Constexpr: Meaning roughly ''to be evaluated at compile time ''. this is used primarily to specify constants, to allow placement of data in memory where it is unlikely to be upted, and for performance.
double sum(const vector<double>&); // sum will not modify its argumentvector<double> v {1.2, 3.4, 4.5}; // v is not a constantconst double s1 = sum(v); // OK: evaluated at run timeconstexpr double s2 = sum(v); // error : sum(v) not constant expression
3.2 constexpr function
For a function to be usable in a constant expression, that is, in an expression that will be evaluated by the compiler, it must be definedConstexpr
constexpr double square(double x) { return x∗x; }
To beConstexpr, A function must be rather simple: just a return-statement computing a value.
AConstexprFunction can be used for non-constant arguments, but when that is done the result is not a constant expression.
4 Loops
4.1. if
bool accept(){ cout << "Do you want to proceed (y or n)?\n"; // write question char answer = 0; cin >> answer; // read answer if(answer == 'y') return true; return false;}
4.2 switch
bool accept2(){ cout << "Do you want to proceed (y or n)?\n"; // write question char answer = 0; cin >> answer; // read answer switch(answer) { case 'y': return true; case 'n': return false; default: cout << "I will take that for a no.\n"; return false; }}
4.3 while
bool accept3(){ int tries = 1; while(tries < 4){ cout << "Do you want to proceed (y or n)?\n"; // write question char answer = 0; cin >> answer; // read answer switch(answer) { case 'y': return true; case 'n': return false; default: cout << "Sorry, I don't understand that.\n"; ++tries; // increment } } cout << "I'll take that for a no.\n"; return false;}
5 Pointers and Arrays
5.1 [] and *
[] Means ''array of ''and other means ''pointer .''
char v[6]; // array of 6 characterschar∗ p; // pointer to character
Prefix unary identifier means ''contents of ''and prefix unary & means ''address .''
char∗ p = &v[3]; // p points to v’s four th elementchar x = ∗p; // *p is the object that p points to
T a[n]; // T[n]: array of n TsT∗ p; // T*: pointer to TT& r; // T&: reference to TT f(A); // T(A): function taking an argument of type A returning a result of type T
5.2 copy and print
Copy elements from one array to another
void copy_fct(){ int v1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int v2[10]; for(auto i=0; i!=10; ++i) // copy elements v2[i] = v1[i];}
For every element of v, from the first to the last, place a copy in x and print it.
void print(){ int v[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for(auto x : v) // range-for-statement std::cout << x << '\n'; for(auto x : {21, 32, 43, 54, 65}) std::cout << x << '\n';}
5.3 increment and count
void increment(){ int v[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for(auto &x : v) ++x;}
The suffix & means ''reference to. ''' It's similar to a pointer, doesn t that no need to use a prefix when to access the value referred to by the reference
// count the number of occurrences of x in p[]// p is assumed to point to a zero-ter minated array of char (or to nothing)int count_x(char* p, char x){ if(p==nullptr) return 0; int count = 0; for(; *p!=0; ++p) if(*p == x) ++count;
return count;}
For more information about nullptr, see nullptr in Blog C ++ 11.