zoukankan      html  css  js  c++  java
  • C和C++代码精粹笔记1

    CH1 更好的C

    1. 运算符重载规定,必须有一个为用户自定义类型
    2. 一些输出没注意到的函数:

      float x = 123.456, y = 12345;

      //cout.precision(2); //显示两个有效值,其他是科学计数法

      //cout << x << " " << y << endl;

       

      //cout.setf(ios::showpoint);//显示末尾的0

      //cout << x << " " << y << endl;

       

      cout.setf(ios::showpos);//显示符号

      cout << x << " " << y << endl;

    3. 打印地址

      Printf("%p",s);

      Cout<<"address: "<<&s;

     

    CH2 指针

    1. 区别 const的位置

      Const char *p; 说明 *p是常量,不能改变。

      Char* const p; 说明,p是常量,不能改变。

    依据看 * 的位置。

    1. 初始化二维指针

      int **a;

      a = new int*[3];//行

          for(int i = 0; i < 4; ++i)

          {

              a[i] = new int[4];//列

          }

      delete[] a;

       

       

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

          int (*p)[4] = a;

       

          //p[i] == *(p+i);

          //p[i][j] == *(p[i]+j) == *(*(p+i)+j);

       

       

          size_t totals = sizeof(a);//整个字节数

          size_t totalcols = sizeof(a[0]);//整个列字节数

          size_t rows = sizeof(a)/sizeof(a[0]);

          size_t cols = sizeof(a[0])/sizeof(a[0][0]);

          for(int i = 0; i < rows; ++i)

          {

              for(int j = 0; j < cols; ++j)

                  cout << p[i][j] << " ";

              cout <<endl;

          }

          cout << rows << " " << cols << endl;

    2. 函数指针

      指向非成员函数的指针:

      #include <Windows.h>

      #include <iostream>

       

      using namespace std;

       

      int (*fp)(int,int);//全局指针变量

      void (*farray[])(void); //可以定义在主函数外部,也可以在内部定义。 函数指针数组

      int Max(int a, int b)

      {

          if(a > b)

              return a;

          else

           return b;

      }

      int main()

      {

          fp = Max;

          int a = fp(1,2);

          cout << a << endl;

      return 0;

      }

       

      指向成员函数的指针:

      #include <Windows.h>

      #include <iostream>

       

      using namespace std;

       

      class C

      {

      public:

          void f(){cout << "C::f ";};

          void g(){cout << "C::g ";};

          int Max(int a, int b)

      {

          if(a > b)

              return a;

          else

           return b;

      }

      };

       

      int main()

      {

      C c;

      void (C::*pmf)() = &C::f; //定义指向成员函数的指针

      (c.*pmf)();//指向成员函数的指针,与非成员函数的指针相比,语法上有点小变化,多了 对象.*

      pmf = &C::g;

      (c.*pmf)();

       

      int (C::*fp)(int,int) = &C::Max;

      int aa = (c.*fp)(3,4);

      cout << aa << endl;

      return 0;

      }

    指向成员函数的指针数组

    1. #include <Windows.h>
    2. #include <iostream>
    3.  
    4. using namespace std;
    5.  
    6. class Object
    7. {
    8. public:
    9.    void retreve(){cout << "Object::retreve";}
    10.    void insert(){cout << "Object::insert";}
    11.    void update(){cout << "Object::update";}
    12.    void process(int choice);
    13. private:
    14.    typedef void(Object::*Omf)();//起别名起的好
    15.    static Omf farray[3];// 成员函数指针数组
    16.  
    17. };
    18. Object::Omf Object::farray[3] = {
    19.   &Object::insert,
    20.   &Object::retreve,
    21.   &Object::update
    22. };
    23. void Object::process(int choice)
    24. {
    25.    if(choice >=0 && choice <=2)
    26.    {
    27.       (this->*farray[choice])();
    28.       cout << endl;
    29.    }
    30. }
    31. int main()
    32. {
    33.    Object o;
    34.    for(;;)
    35.    {
    36.       int choice;
    37.       cin >> choice;
    38.       if(choice >=0 && choice<=2)
    39.          o.process(choice);
    40.       else
    41.           break;
    42.    }
    43.    return 0;
    44. }
    1. 封装与不完全类型

      采用与源类相似的类实现,就是重新定义一个与源类相似的类,然后重新包装一下。

     

    CH3 预处理器

    1. debug模式下可以运行 release下运行不了,用宏定义实现

      #define DEBUG 1

       

      int main()

      {

      int i = 2;

      #if DEBUG

      cout << "debugmod" << endl;

      #endif

      return 0;

      }

       

      下面这句话也可以实现上面的功能

       

    #ifdef _DEBUG

    cout << "debugmod" << endl;

    #endif // _DEBUG

     

     

    要想将新的C++代码与旧的C代码混合编程,需要加下面的语句

    extern "C" void f(); //f()在C环境下被编译

    1. 字符集

      回车 换行 回退  警示 a

       

       

       

    CH4 C标准库之一 : 面向合格的程序员

    1、<ctype.h>

    常见的函数: is系列。Eg : isupper(); islower(); isspcace() 等。

    char a = 'A';

    int b = isalpha(a);

    实例:

    1. #include <Windows.h>
    2. #include <iostream>
    3. #include<ctype.h>
    4. #include <stdlib.h>
    5. #include<stdio.h>
    6. #include <string.h>
    7. using namespace std;
    8.  
    9. long atox(char *s)
    10. {
    11.    while(isspace(*s))//干掉开头的空格,只要有一个非零的数即跳出
    12.       s++;
    13.    long sum;
    14.    for(sum = 0L; isxdigit(*s); ++s)
    15.    {
    16.       int digit;
    17.       if(isdigit(*s))
    18.          digit = *s - '0';
    19.       else
    20.          digit = toupper(*s) - 'A' + 10;
    21.       sum = sum*16L + digit;
    22.    }
    23.    return sum;
    24. }
    25.  
    26. long atox(char*s)
    27. {
    28.    char xdigs[] = {0123456789ABCDEF};
    29.    long sum;
    30.    while(isspace(*s))
    31.       s++;
    32.    for(sum = 0; isxdigit(*s); ++s)
    33.    {
    34.       int digit = strchr(xdigs,toupper(*s)) - xdigs;//找到指针的位置,即数字的位置(0-15取值) 找出字符串xdigs中第一次出现字串 (*s) 的位置。
    35.       sum = sum*16L + digit;
    36.    }
    37. }
    38. long atox(char *s)
    39. {
    40.    long n = 0;
      1.    sscanf(s,"%lx",&n); // 参考代码:http://www.91linux.com/html/article/program/cpp/20081130/14121.html
    41.    return n;
    42. }
    43. int main()
    44. {
    45.    char* s = "123 abc";
    46.    //方法一
    47.    long t = atox(s);
    48.    //方法二
    49.    char** ss = NULL;
    50.    long tt = strtol(s,ss,16);
    51.    cout << tt << endl;
    52.    return 0;
    53. }

     

  • 相关阅读:
    ActiveMQ简单介绍+简单实例
    分布式开放消息系统(RocketMQ)的原理与实践
    RocketMQ实战(一)
    十分钟入门RocketMQ
    android Lib
    JDK1.8 Lambda
    JDK1.8聚合操作
    JNI
    Gradle插件
    Weex命令
  • 原文地址:https://www.cnblogs.com/zhuxuekui/p/4199636.html
Copyright © 2011-2022 走看看