zoukankan      html  css  js  c++  java
  • C++_class_powerpoint_1.1

    Types and Declarations

    Boolean Type

    bool type – boolean , logic type
    bool literal – true, false
    int and bool

    • Nonzero->true
    • Zero ->false
    • true ->1
    • false->0

    1 <= sizeof( bool) <= sizeof(long)

    1 void f(int a,int b)
    2  {
    3     bool b1{a==b}; 
    4     bool b2 = a==b;
    5 6  }

     bool isGreater(int a, int b){return a > b;} 

    1 void f()
    2 {
    3     bool a=true,
    4            b=true;
    5     bool x=a+b;
    6     bool y=a||b;
    7 }   

    Knowing Ranges of Types

     1 // Implementation-dependent
     2 #include <limits>
     3 int main()
     4 {
     5    cout << "largest integer = " 
     6     << numeric_limits<int>::max();
     7    cout << “smallest integer = " 
     8     << numeric_limits<int>::min();
     9    return 0; 
    10 }

    Binary Literal

    int a=0b1100,b=0B0101;

    ::globalId can be used in a local scope, even its name is identical to local one.

    int x;
    void f()
    {
    int x=1; ::x=2; x=3; …
    }//::x means the x is the global one

    int x=1;

    void f(){

        int x=2;
        ::x=3;
        printf("%d ",x);

    }

    int main()
    {
        f();
        printf("%d",x);
        return 0;
    }//output 2 3

    auto

    If the type of a variable can be deducted from its initialization, keyword auto can be used instead of type name in the definition.

    1 auto i{0};
    2 vector<string> v; …
    3    for (const auto& x : v) cout << x << '
    ';
    4 for (auto i : {1, 2, 3, 4, 5, 6, 7}) 
    5        cout << i << '
    ';

    Initialization

    four syntactic styles:

    • T a1 {v};
    • T a2 = {v};
    • T a3 = v;
    • T a4(v);

    Pointers, Arrays and Structures

    Variable-size arrays

    1 #include <vector>
    2 using namespace std;
    3 void f(int i)
    4 {
    5     vector<int> v1(i);
    6     vector<int> v2{1,2,3};
    7     vector<int> v3(i,10);
    8 9 }

    Constants

    Keyword const as a modifier

    • A constant identifier must be initialized and cannot be assigned
    • Prefer to #define macro usage
    • Using symbolic constants is better than using literals in the large programs

     

    Pointers and Constants

    Prefixing a pointer with const makes an object the pointer points to, not pointer itself, a constant.

    1 const int someInt=10;
    2    const int *p = &someInt; 
    3 //Or
    4    int const  *p = &someInt; 
    5   // *p is a constant, p is a variable

    To declare a pointer itself to be a constant, use the declarator *const .

    1 int someInt=10;
    2 int *const p = &someInt; 
    3 // p is a constant

    To declare both the pointer and the object the pointer points to  be constants, use two const modifiers.

    1 const int someInt=10;
    2 const int *const p = &someInt; 
    3 //Or
    4 int const *const p = &someInt; 
    5 // *p is a constant, p is a constant too.
     1 void f4( )
     2 {  int a=1;
     3     const int c=2;
     4     const int* p1=&c; 
     5     const int* p2=&a; //non const ptr  const ptr
     6     int * p3=&c; // Error! 
     7     *p3=7;           
     8 }
     9 // a non-constant pointer can assign to a constant
    10 // pointer, but a constant pointer cannot assign to
    11 //  a non-constant pointer.

    References

    A reference is alias/nickname for an object or a function.

    The main usages:
          1. specifying arguments and return values
              for functions 
          2. for overloaded operators.
    Notation : Typename&
    References must be initialized.

    void f()
    {
        int i=1;
        int& r{i};
        int x=r;
        r=2;
    }//Both i and r are occupied the same memory

    constant references

    Constant Reference can refer to a non lvalue or other type object.
    For const T&,
       [1] implicit conversion to T if necessary
       [2] result is placed in a temporary object
       [3] reference to the temporary object
    The temporary object persists until the end of its reference's lifetime.

    References as Parameters of Functions

    The function can change the value of an object passed to it.
    This is called call-by-reference passing mechanism. It is different from call-by-value.

    1 void increment(int& a) 
    2 { a++; } //x++
    3 void f()
    4 {
    5     int x=1;
    6     increment(x); // x==2
    7 }//Parameter a is initialized with the argument x

    The return value of the function can refer to the expression of return statement.

     1 struct Pair{
     2     string name;
     3     double val;
     4 };
     5 
     6 vector<Pair> pairs;
     7 double& value(const string& s)
     8 {
     9     for (auto& x: pairs)
    10         if (s==x.name) return x.val;//original val+1;
    11     pairs.push_back({s,0});
    12     return pairs.back().val;//0->1
    13 }
    14 
    15 int main()
    16 {  
    17    for (string buf; cin>>buf;)
    18     // enter ^d in Linux at end of the input
    19           value(buf)++;  
    20    for (const auto& x: pairs)
    21        cout << x.name << ": " 
    22           << x.val  << '
    ';
    23    return 0; 
    24 }

    void*

    Pointer to any data type(not function type)
    Be able to assign, compare and cast to another pointer type
     T* -->void* is type coercion
    void*-->T* must use type cast

    1 int i=100;
    2 void* pv=&i; // type coercion
    3 int* pi1 = static_cast<int*>(pv);
    4 int* pi2 = (int*)pv;

    Structure type name

    Typename of a structure is structure tag , keyword struct is unnecessary.
    For example:
          struct address {…};
          type name is address,
          but struct address in C.

    Structure forward declaration

    The typename of a structure is available for use as pointer to structure immediately after it has been encountered and not just after the complete declaration has been seen.

     struct Link{ Link* next; … }; 

    To allow two or more types refer to each other, use structure forward declaration.

     struct List; struct Link{ List* a; … }; struct List{ Link* a; … }; 

  • 相关阅读:
    windows下操作linux虚拟机映射网络驱动器中文件提示chmod权限不足解决方案
    Centos 更改MySQL5.7数据库目录位置
    MySQL语句增加字段,修改字段名,修改类型,修改默认值
    [MySQL]MySQL数据库中如何查询分组后每组中的最后一条记录?
    ROW_NUMBER()函数使用详解
    【转】mysql 存储过程的示例
    简简单单储存过程——循环一个select结果集
    mysql存储过程demo
    mysql日期加一个天数获得新的日期
    使用SyncNavigator轻松实现数据库异地同步、断点续传、异构同步
  • 原文地址:https://www.cnblogs.com/Jiiiin/p/8606148.html
Copyright © 2011-2022 走看看