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. [python学习篇][廖雪峰][2]函数式编程

    函数名也是变量: >>> f = abs >>> f(-10) 10 然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就 ...

  2. javascript学习笔记 - 引用类型 单体内置对象

    七 单体内置对象 1.Global对象 不属于任何对象的属性和方法,都归于它.全局作用域中定义的变量.函数,都属于Global对象 1.1 URI编码  encodeURI <=>deco ...

  3. POJ-1190 生日蛋糕 NOI99

    深搜+几个剪枝. 貌似搜索顺序也挺重要的...我不知是不是因为这个然后Tle了好久... #include <cstdio> #include <iostream> #incl ...

  4. BZOJ2302 [HAOI2011]Problem c 【dp】

    题目 给n个人安排座位,先给每个人一个1~n的编号,设第i个人的编号为ai(不同人的编号可以相同),接着从第一个人开始,大家依次入座,第i个人来了以后尝试坐到ai,如果ai被占据了,就尝试ai+1,a ...

  5. 数据库操作——pymysql模块

    一 import pymysql conn=pymysql.connect( host='localhost', port=3306, user='zuo', password=', database ...

  6. d3 比例尺

    .domain([, ]) 定义域范围 .range([, ]) 值域范围 var scale = d3.scale.linear() .domain([, ]) .range([, ]); 将100 ...

  7. C/C++ XMPP/Jabber 客户端类库对比/点评 (转)

    原文转自 http://blog.csdn.net/educast/article/details/31359835 1.gloox Ans. 老牌库,推荐 gloox是一个稳定功能完整的XMPP客户 ...

  8. 過充保護警告訊息 over charging protection,Battery over voltage protection, warning message

    Definition: over charging protection.battery over voltage protection, 是一種 battery 保護機制, 避免 battery 充 ...

  9. HDU 4749: Parade Show

    看大神代码,发现上交大神很棒的一个思路 题意: 在源数字串中找出尽量多的连续子串,要求子串任意两值的大小关系与目标串相同位置的值的大小关系相同.求源串能拿出的子串的最大数量. 关键词: RK-Hash ...

  10. 使用Python计算研究生学分绩(绩点)

    最近看了CSDN上一个专栏<Python爬虫入门教程>,其中最后一篇作者写了个例子,用爬虫计算山东大学绩点,顿时想到前一阵子搞测评的时候还得拿计算器一点点算自己的平均学分绩,也想写一个自己 ...