随笔分类 - c++特性
摘要:c++11 auto 关键字 自动判断变量的类型 查看代码 #include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #inclu
阅读全文
摘要:今天做一道逆波兰表达式,没有想到输入数据可能有负数,调了好久好久 -_- .... 有几个可以用的函数 pow (db x, db y) 返回 x^y atof (ch * x) 把 x 返回为一个浮点数 如果x不全是数字,则返回最开始的部分 atof ("123dsss")=123 atof("h
阅读全文
摘要:STL: Standard Template Library 容器:可容纳各种数据类型的通用数据结构, 是类模板迭代器:可用于依次存取容器中元素,类似于指针算法:用来操作容器中的元素的函数模板 1顺序容器:vector, deque,list2 关联容器:set, multiset, map, mu
阅读全文
摘要:c++/oop 类模板 定义出一批相似的类, 可以定义类模板, 然后由类模板生成不同的类 例:可变长整型数组 写法: template <类型参数表(class 类型参数1,class 类型参数2...)> class 模板名{ .... }; 类模板中的成员函数在外面定义时: template <
阅读全文
摘要:c++/oop 模板 函数模板 举例:swap template <class c1,class c2 , ...> 返回值类型 模板名(形参表){ } template<class T> void swap (T & x, T & y){ T tmp=x;x=y;y=tmp; } 编译器在编译的时
阅读全文
摘要:c++/oop 文件操作 #include <fstream> 文件的打开和关闭: void open (const char * szFileName,int mode) 第一个参数是指向文件名的指针,第二个是文件打开模式标记 模式标记 ios::in (ifstream/fstream)打开文件
阅读全文
摘要:#include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <iomanip> #include <string>
阅读全文
摘要:string 用法 string 变量名 string city = "Beijing" 一个 string 对象的大小是固定的,与字符串的长短无关,只保存地址和一些其他信息。 string as[] = {"asd","dfg","ertw"}; cout<<as[1]<<endl; string
阅读全文
摘要:c++/oop 继承与派生 继承:在定义一个新的类 B 时,如果该类与某个已有的类 A 相似 (指的是 B 拥有 A 的全部特点), 那么就可以把 A 作为一个基类,而把 B 作为基类的一个派生类 (也称子类)。 逻辑:B对象也是一个A对象 例 : 查看代码 #include <cstdlib> #
阅读全文
摘要:可变整型数组 查看代码 #include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> using namespace std; cla
阅读全文
摘要:c++/oop 运算符重载 重载为普通函数 #include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> using namespac
阅读全文
摘要:对象指针 指向一个对象 写法 p->sum() 和 (*p).sum() 是等价的,前者更加直观些 #include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> #in
阅读全文
摘要:C++/oop 指针 按理说指针不是c++特有的东西,只是我恰好发现自己不太会,赶紧补一下 int *p 定义指针 p 指向 int 型变量 以下写法均可 int* p / int * p p = &a (& 取地址) 指针本身的类型是 unsigned long int *p 取内容 指针保存着变
阅读全文
摘要:面向对象 简单的说就是写很多类 每个类有自己的数据和函数,叫做“成员”。 类定义出来的变量,也称为类的实例,就是“对象”。 和struct 好像差不多 对象之间有 '=' 关系,其他的需要定义 private: 私有成员,只能在成员函数内访问public: 公有成员,可以在任何地方访问protect
阅读全文
摘要:c++/oop 引用 就相当于是原变量 int & a = b 相当于是别名 一个神奇的写法:引用作为函数返回值 1 int n = 4; 2 int & SetValue() { 3 return n; 4 } 5 int main() { 6 SetValue() = 40; 7 cout <<
阅读全文