首页 > 代码库 > C++ 之 基础回顾(一)

C++ 之 基础回顾(一)

1  C++ program

  技术分享

1.1  the miminal cpp

int main(){}    // the minimal C++ program

1.2  Hello World

#include <iostream>int main(){    std::cout << "Hello, World!\n";}

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);}

 

2  Types, Variables and Arithmetic

2.1  definition

  A declaration is a statement that introduces a name into the program. It specifies a type for the named entity: 

  - A type defines a set of possible values and a set of operations (for an object).

  - An object is some memory that holds a value of some type.

  - A value is a set of bits interpreted according to a type.

  - A variable is a named object.

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.2  sizeof(char)

  技术分享

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 want to make the type clearly visible to readers of our code.

  - We want to be explicit 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 corrupted, 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 defined constexpr

constexpr double square(double x) { return x∗x; }

  To be constexpr , a function must be rather simple: just a return -statement computing a value.

  A constexpr function 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 means ‘‘pointer to.’’

char v[6];   // array of 6 characterschar∗ p;     // pointer to character

  prefix unary means ‘‘contents of’’ and prefix unary & means ‘‘address of.’’

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, except that no need to use a prefix 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;}

  其中,nullptr 参见博文 C++11 之 nullptr

 

C++ 之 基础回顾(一)