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 \n";};

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

        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. 字符集

    回车 \r 换行 \n 回退 \b 警示 \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. }

 

C和C++代码精粹笔记1的更多相关文章

  1. JavaScript语言精粹笔记

    JavaScript语言精粹笔记 掌握语言的每个特性可以让你出风头,但是并不推荐,因为一部分的特性带来的麻烦可能远超本身的价值.正如书中所言,坏的材料并不能雕刻出好的作品,要成为一名更好的程序员,要取 ...

  2. JavaScript 语言精粹笔记3

    方法 毒瘤 糟粕 记录一下阅读蝴蝶书的笔记,本篇为书中最后一部分:方法.代码风格.优美的特性.毒瘤.糟粕等. 方法 这一章主要介绍了一些方法集.这里写几个我不太熟悉的方法和要点吧. array.joi ...

  3. Linux协议栈代码阅读笔记(二)网络接口的配置

    Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...

  4. [置顶] Linux协议栈代码阅读笔记(一)

    Linux协议栈代码阅读笔记(一) (基于linux-2.6.21.7) (一)用户态通过诸如下面的C库函数访问协议栈服务 int socket(int domain, int type, int p ...

  5. Linux-3.0.8 input subsystem代码阅读笔记

    先乱序记录一下阅读Linux input subsystem代码的笔记. 在input device driver的入口代码部分,需要分配并初始化input device结构,内核提供的API是inp ...

  6. [置顶] Linux协议栈代码阅读笔记(二)网络接口的配置

    Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...

  7. 《linux 内核全然剖析》 fork.c 代码分析笔记

    fork.c 代码分析笔记 verifiy_area long last_pid=0; //全局变量,用来记录眼下最大的pid数值 void verify_area(void * addr,int s ...

  8. 《linux 内核全然剖析》sched.c sched.h 代码分析笔记

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011368821/article/details/25129835 sched.c sched.h ...

  9. 使用Git和Github来管理自己的代码和笔记

    一.Github注册 1.先注册github.com的账号,官方网站: https://github.com/ 2.登录 3.创建仓库,仓库分公开的和私有的,公开的是免费的,私有的是收费的.我现在创建 ...

随机推荐

  1. picPick使用研究

    WhiteBoard白板功能很强大. 可以直接在网页上进行圈画,然后截图. ImageEditor是一个很好用的画图功能,比windows画图的箭头好看.

  2. PHPSTORM杂技

    PHPSTORM技巧 去掉右上角弹出浏览器条 settings->web browsers->show browser popup in the editor前的钩去掉 让class fu ...

  3. 【转】PhpStorm 提交代码到远程GitHub仓库

    转载地址:http://my.oschina.net/lujianing/blog/180728 1.下载github for window http://windows.github.com/ 2. ...

  4. Java接口抽象类

    抽象类中的方法可以实现,接口中的方法只能声明,不能实现.抽象类的成员变量可以为各种类型,接口的变量只能为public static final.抽象类可以有静态方法和静态代码块,接口不能有.一个类只能 ...

  5. Apache2 FastCGI C Demo

    安装依赖 sudo apt-get install libapache2-mod-fastcgi a2enmod fastcgi sudo apt-get install libfcgi-dev li ...

  6. pytorch conditional GAN 调试笔记

    推荐的几个开源实现 znxlwm 使用InfoGAN的结构,卷积反卷积 eriklindernoren 把mnist转成1维,label用了embedding wiseodd 直接从tensorflo ...

  7. 欧拉回路 & 欧拉路径

    欧拉路径 & 欧拉回路 概念 欧拉路径: 如果图 G 种的一条路径包括所有的边,且仅通过一次的路径. 欧拉回路: 能回到起点的欧拉路径. 混合图: 既有无向边又有无向边的图. 判定 无向图 一 ...

  8. linux JDK安装(一)

    1.先卸载服务器自带的jdk软件包# java -version #查看服务器是否安装过jdkjava version "1.6.0_17"OpenJDK Runtime Envi ...

  9. springboot获取getBean方法以及ApplicationContext空指针问题解决

    创建获取ApplicationContext工具类: package com.performancetest.common.utils; import org.springframework.bean ...

  10. 解决c#所有单线程单元(STA)线程都应使用泵式等待基元(如 CoWaitForMultipleHandles),并在运行时间很长的操作过程中定期发送消息。 转载

    最近做一个后来程序,启动了事务后有一段操作业务,当运行一段时间后,出现这个异常 CLR 无法从 COM 上下文 0x1b1c38 转换为 COM 上下文 0x1b1da8,这种状态已持续 60 秒.拥 ...