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. EOJ Monthly 2018.4

    A. ultmaster 的小迷妹们 Time limit per test: 2.0 seconds Memory limit: 256 megabytes ultmaster 男神和他的小迷妹们准 ...

  2. IDE介绍以及Eclipse的简单使用

    一.IDE介绍 ①   什么是IDE? 1.  IDE全称是”Integrated Development Environment”,中文称为“集成开发环境”; 2.是用于提供程序开发环境的应用程序, ...

  3. Spring配置文件中使用ref local与ref bean的区别

    Spring配置文件中使用ref local与ref bean的区别.在ApplicationResources.properties文件中,使用<ref bean>与<ref lo ...

  4. Jenkins+GitHub+maven

    介绍: http://192.168.193.128:8115/jenkins/ 用户名:admin密码:admin电子邮箱:admin@admin.com root/12345678添加新用户: t ...

  5. Event based Collections

    https://sourceforge.net/p/happy-guys/wiki/Event%20based%20Collections/

  6. java面试题之HashMap和TreeMap的区别

    HashMap和TreeMap的区别 相同点: 都是以key和value的形式存储: key不可以重复: 都是线程不安全的: 不同点: HashMap的key可以为空 TreeMap的key值是有序的 ...

  7. spring中MessageSource的配置使用方法3--ResourceBundleMessageSource

    ApplicationContext接口扩展了MessageSource接口,因而提供了消息处理的功能(i18n或者国际化).与HierarchicalMessageSource一起使用,它还能够处理 ...

  8. django 报错 no such table: auth_user

    需要执行 python3 manage.py makemigrations python3 manage.py migrate 参考:http://arrayoverflow.com/question ...

  9. Junit框架使用--JUnit常用断言及注解

    从别人博客中抄过来一点东西 原文地址:http://blog.csdn.net/wangpeng047/article/details/9628449 断言是编写测试用例的核心实现方式,即期望值是多少 ...

  10. 【CF1020C】Elections(贪心)

    题意: Berland地区的腐败现象非常常见. 马上有一场选举,你事先知道了选民和政党的数量,分别为 n 和 m ,对于每一位选民,你知道他将要选举哪一个政党, 不过,每一位选民都会在接受一定数额的金 ...