C和C++代码精粹笔记1
CH1 更好的C
- 运算符重载规定,必须有一个为用户自定义类型
- 一些输出没注意到的函数:
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;
- 打印地址
Printf("%p",s);
Cout<<"address: "<<&s;
CH2 指针
- 区别 const的位置
Const char *p; 说明 *p是常量,不能改变。
Char* const p; 说明,p是常量,不能改变。
依据看 * 的位置。
- 初始化二维指针
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;
- 函数指针
指向非成员函数的指针:
#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;
}
指向成员函数的指针数组
- #include <Windows.h>
- #include <iostream>
- using
namespace std; - class Object
- {
- public:
- void retreve(){cout << "Object::retreve";}
- void insert(){cout << "Object::insert";}
- void update(){cout << "Object::update";}
- void process(int choice);
- private:
- typedef void(Object::*Omf)();//起别名起的好
- static Omf farray[3];// 成员函数指针数组
- };
- Object::Omf Object::farray[3] = {
- &Object::insert,
- &Object::retreve,
- &Object::update
- };
- void Object::process(int choice)
- {
- if(choice >=0 && choice <=2)
- {
- (this->*farray[choice])();
- cout << endl;
- }
- }
- int main()
- {
- Object o;
- for(;;)
- {
- int choice;
- cin >> choice;
- if(choice >=0 && choice<=2)
- o.process(choice);
- else
- break;
- }
- return 0;
- }
- 封装与不完全类型
采用与源类相似的类实现,就是重新定义一个与源类相似的类,然后重新包装一下。
CH3 预处理器
- 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环境下被编译
- 字符集
回车 \r 换行 \n 回退 \b 警示 \a
CH4 C标准库之一 : 面向合格的程序员
1、<ctype.h>
常见的函数: is系列。Eg : isupper(); islower(); isspcace() 等。
char a = 'A';
int b = isalpha(a);
实例:
- #include <Windows.h>
- #include <iostream>
- #include<ctype.h>
- #include <stdlib.h>
- #include<stdio.h>
- #include <string.h>
- using
namespace std; - long atox(char *s)
- {
- while(isspace(*s))//干掉开头的空格,只要有一个非零的数即跳出
- s++;
- long sum;
- for(sum = 0L; isxdigit(*s); ++s)
- {
- int digit;
- if(isdigit(*s))
- digit = *s - '0';
- else
- digit = toupper(*s) - 'A' + 10;
- sum = sum*16L + digit;
- }
- return sum;
- }
- long atox(char*s)
- {
- char xdigs[] = {0123456789ABCDEF};
- long sum;
- while(isspace(*s))
- s++;
- for(sum = 0; isxdigit(*s); ++s)
- {
- int digit = strchr(xdigs,toupper(*s)) - xdigs;//找到指针的位置,即数字的位置(0-15取值) 找出字符串xdigs中第一次出现字串 (*s) 的位置。
- sum = sum*16L + digit;
- }
- }
- long atox(char *s)
- {
- long n = 0;
- sscanf(s,"%lx",&n); // 参考代码:http://www.91linux.com/html/article/program/cpp/20081130/14121.html
- return n;
- }
- int main()
- {
- char* s = "123 abc";
- //方法一
- long t = atox(s);
- //方法二
- char** ss = NULL;
- long tt = strtol(s,ss,16);
- cout << tt << endl;
- return 0;
- }
C和C++代码精粹笔记1的更多相关文章
- JavaScript语言精粹笔记
JavaScript语言精粹笔记 掌握语言的每个特性可以让你出风头,但是并不推荐,因为一部分的特性带来的麻烦可能远超本身的价值.正如书中所言,坏的材料并不能雕刻出好的作品,要成为一名更好的程序员,要取 ...
- JavaScript 语言精粹笔记3
方法 毒瘤 糟粕 记录一下阅读蝴蝶书的笔记,本篇为书中最后一部分:方法.代码风格.优美的特性.毒瘤.糟粕等. 方法 这一章主要介绍了一些方法集.这里写几个我不太熟悉的方法和要点吧. array.joi ...
- Linux协议栈代码阅读笔记(二)网络接口的配置
Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...
- [置顶] Linux协议栈代码阅读笔记(一)
Linux协议栈代码阅读笔记(一) (基于linux-2.6.21.7) (一)用户态通过诸如下面的C库函数访问协议栈服务 int socket(int domain, int type, int p ...
- Linux-3.0.8 input subsystem代码阅读笔记
先乱序记录一下阅读Linux input subsystem代码的笔记. 在input device driver的入口代码部分,需要分配并初始化input device结构,内核提供的API是inp ...
- [置顶] Linux协议栈代码阅读笔记(二)网络接口的配置
Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...
- 《linux 内核全然剖析》 fork.c 代码分析笔记
fork.c 代码分析笔记 verifiy_area long last_pid=0; //全局变量,用来记录眼下最大的pid数值 void verify_area(void * addr,int s ...
- 《linux 内核全然剖析》sched.c sched.h 代码分析笔记
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011368821/article/details/25129835 sched.c sched.h ...
- 使用Git和Github来管理自己的代码和笔记
一.Github注册 1.先注册github.com的账号,官方网站: https://github.com/ 2.登录 3.创建仓库,仓库分公开的和私有的,公开的是免费的,私有的是收费的.我现在创建 ...
随机推荐
- [python学习篇][廖雪峰][2]函数式编程
函数名也是变量: >>> f = abs >>> f(-10) 10 然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就 ...
- javascript学习笔记 - 引用类型 单体内置对象
七 单体内置对象 1.Global对象 不属于任何对象的属性和方法,都归于它.全局作用域中定义的变量.函数,都属于Global对象 1.1 URI编码 encodeURI <=>deco ...
- POJ-1190 生日蛋糕 NOI99
深搜+几个剪枝. 貌似搜索顺序也挺重要的...我不知是不是因为这个然后Tle了好久... #include <cstdio> #include <iostream> #incl ...
- BZOJ2302 [HAOI2011]Problem c 【dp】
题目 给n个人安排座位,先给每个人一个1~n的编号,设第i个人的编号为ai(不同人的编号可以相同),接着从第一个人开始,大家依次入座,第i个人来了以后尝试坐到ai,如果ai被占据了,就尝试ai+1,a ...
- 数据库操作——pymysql模块
一 import pymysql conn=pymysql.connect( host='localhost', port=3306, user='zuo', password=', database ...
- d3 比例尺
.domain([, ]) 定义域范围 .range([, ]) 值域范围 var scale = d3.scale.linear() .domain([, ]) .range([, ]); 将100 ...
- C/C++ XMPP/Jabber 客户端类库对比/点评 (转)
原文转自 http://blog.csdn.net/educast/article/details/31359835 1.gloox Ans. 老牌库,推荐 gloox是一个稳定功能完整的XMPP客户 ...
- 過充保護警告訊息 over charging protection,Battery over voltage protection, warning message
Definition: over charging protection.battery over voltage protection, 是一種 battery 保護機制, 避免 battery 充 ...
- HDU 4749: Parade Show
看大神代码,发现上交大神很棒的一个思路 题意: 在源数字串中找出尽量多的连续子串,要求子串任意两值的大小关系与目标串相同位置的值的大小关系相同.求源串能拿出的子串的最大数量. 关键词: RK-Hash ...
- 使用Python计算研究生学分绩(绩点)
最近看了CSDN上一个专栏<Python爬虫入门教程>,其中最后一篇作者写了个例子,用爬虫计算山东大学绩点,顿时想到前一阵子搞测评的时候还得拿计算器一点点算自己的平均学分绩,也想写一个自己 ...