随笔分类 -  c++/c

摘要:原理先看个例子,存储字符串abc、ab、abm、abcde、pm可以利用以下方式存储 上边就是Trie树的基本原理:利用字串的公共前缀来节省存储空间,最大限度的减少无谓的字串比较。应用 Trie树又称单词查找树,典型的应用是用于统计,排序和保存大量的字符串(不仅用于字符串),所以经常被搜索引擎系统用于文本词频的统计。设计 trie,又称前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定。一个节点的所有子孙都有相同的前缀,也就是这个节点对应的字符串,而根节点对应空字符串。一般情况下,不是所有的节点... 阅读全文
posted @ 2014-03-15 15:58 jihite 阅读(12909) 评论(0) 推荐(2)
摘要:非类型模版类型模版类模版函数模版一。非类型模版#include using namespace std;templateclass Index{ public: int operator[](char vchar) { return vchar % Size; }};int main(){ Index index; cout using namespace std;templateclass trie{ public: trie(Type val_1, Type val_2) : num_1(val_1), num_2(val... 阅读全文
posted @ 2014-03-14 23:31 jihite 阅读(956) 评论(0) 推荐(0)
摘要:代码#include using namespace std;templateclass Index{ public: int operator[](char vchar) { return vchar % Size; }};int main(){ Index index; cout 声明此参数 阅读全文
posted @ 2014-03-14 22:28 jihite 阅读(260) 评论(0) 推荐(0)
摘要:一开始C++定义为:C with Classes。 如今的C++已经是一个多重范型编程语言,可以把C++视为有四个次语言组成的联邦语言。C。C++任然以C为基础。区块、语句、预处理、内置语言类型、数组、指针等。搞笑编程守则映射出C语言的局限:没有模版、没有异常、没有重载。Object-Orited C++。这部分是C with Classes诉求的:class、封装、集成、多态、virtual函数等。Template C++。模版威力强大,带来了崭新的泛型编程,即TMP(模版元编程)。STL。STL是template程序库。它对容器、迭代器、算法以及函数对象的规则有极佳的紧密配合与协... 阅读全文
posted @ 2014-03-09 23:54 jihite 阅读(490) 评论(0) 推荐(0)
摘要:开源中国:http://my.oschina.net/lotte1699/blog/142538网页快照:http://www.piaocafe.com/295977937/1393815670376264 阅读全文
posted @ 2014-03-03 11:04 jihite 阅读(461) 评论(0) 推荐(0)
摘要:Linux 环境下当GCC版本比较高时,编译代码可能出现的问题 问题是这样产生的,先看这个函数原型: 再看这个函数调用: 把这两个东西组合起来,用最新的g++编译一下就会得到标题中的警告。 为什么呢?原来char *背后的含义是:给我个字符串,我要修改它。 而理论上,我们传给函数的字面常量是没法被修 阅读全文
posted @ 2014-03-01 18:23 jihite 阅读(48305) 评论(2) 推荐(8)
摘要:c/c++常需要获得最大值,最小值,通常这两个数与平台和操作系统有关,因此可移植的办法就是推荐使用库函数提供的常量定义1. 利用语言自定义类似的常量定义在limits.h和float.h头文件中。在头文件中,整数的最值通常是这样的名字:INT_MAX, INT_MIN,直接使用即可。2. 自定义变量int MAX_INT = ((unsigned)(-1))>>1;int MIN_INT = ~MAX_INT;解释int占4个字节。-1是有符号数,默认用补码表示,二进制表示为32个1,如果强制解释为无符号数,那么(unsigned)(-1)=(11111111 1111111111 阅读全文
posted @ 2014-01-22 16:32 jihite 阅读(1183) 评论(3) 推荐(0)
摘要:1. struct 和 class 唯一的区别:默认的成员保护级别和默认的派生保护级别不同(前者为public,后者为private)。2. int *p = new int[23]; delete []p; 阅读全文
posted @ 2014-01-07 22:52 jihite 阅读(282) 评论(0) 推荐(0)
摘要:C++的函数调用默认不使用动态绑定。要触发动态绑定,必须满足两个条件:只有指定为虚函数的成员函数才能进行动态绑定必须通过基类类型的引用或指针进行函数调用因为每个派生类对象中都拥有基类部分,所以可以使用基类类型的指针或引用来引用派生类对象示例#include #include using namespace std;struct base{ base(string str = "Base") : basename(str) {} virtual void print() { cout print(), pd->print()" print(); pd-> 阅读全文
posted @ 2014-01-07 21:46 jihite 阅读(5426) 评论(0) 推荐(1)
摘要:1. 类内的访问控制在基类中,public和private具有普通的含义:用户(即基类的对象)可以访问public成员(包括函数、数据),而不能访问private成员。private只能被基类的成员和友员访问。(注:基类的对象无权访问private成员)派生类对基类的public和private具有相同的含义:它可以访问public成员,不可以访问private成员。派生类对象亦如此。为了使派生类(注:是类, 不是类对象)可以基类的成员,但禁止基类的对象访问,定义了protected控制。基类protected成员,派生类可以访问,但基类以及派生类的对象是不行的。示例#include #inc 阅读全文
posted @ 2014-01-06 22:40 jihite 阅读(618) 评论(0) 推荐(0)
摘要:目录输入和输出操作符算术操作符和关系操作符下标操作符自加、自减操作符成员访问操作符1 输入和输出操作符1.1 输出操作符1.1.1 示例#include #include using namespace std;class A{ friend ostream& operator#include using namespace std;class A{ friend ostream& operator>(istream& in, A& a); public: A(const string &s = "", int v = 0) : 阅读全文
posted @ 2014-01-05 13:27 jihite 阅读(1724) 评论(0) 推荐(1)
摘要:c++编程提倡使用标准库,一个原因是标准库大胆减少对指针的使用。但是许多程序是离不开指针的。包含指针的类需要特别注意复制控制,原因是复制指针时只复制指针中的地址,而不复制指针所指向的对象。这样当把一个对象复制给另一个对象后,当改变一个对象后,另一个对象也会收到牵连。另外一个对象释放掉后,其指针已经被释放掉。而另一个对象还不知道,其实该对象中的指针已经成为悬垂指针。这样再操作就会出现错误。1. 定义智能指针类原理:定义一个计数的类,所有复制的都是指向这一个类,每复制一次,该类加1一次;每析构一次,该类减1一次。当次数为0时,释放掉动态申请的空间。图例:1)定义一个对象2)复制一个对象#inclu 阅读全文
posted @ 2014-01-03 11:04 jihite 阅读(1380) 评论(0) 推荐(0)
摘要:反思两个问题 1. 带默认参数的函数,为何声明、定义不能同时有参数? 2. 带默认参数的函数, 为何带默认参数的参数靠后站?上程序#include #include using namespace std;class A{ public: A(const string &a = "hello, nihao!", int b = 67); private: string s; int sb;};A::A(const string &a, int b) : s(a), sb(b){ cout #include #include #... 阅读全文
posted @ 2013-12-28 23:42 jihite 阅读(12927) 评论(1) 推荐(0)
摘要:上代码#include #include using namespace std;class A { public: A(const string &book = "ab") : s(book) {} int same_s(const A &a) const { return s == a.s; } private: string s;};int main(int argc ,char **argv){ A c("aaa"); string m = "aaa"; cout #include using na... 阅读全文
posted @ 2013-12-18 08:37 jihite 阅读(984) 评论(0) 推荐(0)
摘要:1. 类成员为const类型2. 类成员为引用类型#include using namespace std;class A{ public: A(int &v) : i(v), p(v), j(v) {} void print_val() { cout using namespace std;class Base{ public: Base(int a) : val(a) {} private: int val;};class A{ public: A(int v) : p(v), b(v) {} ... 阅读全文
posted @ 2013-12-17 09:07 jihite 阅读(15478) 评论(1) 推荐(6)
摘要:上代码#include using namespace std;class A{ public: A(int v): j(v + 2), i(j) {} void print_val() { cout using namespace std;class A{ public: A(int v): i(v), j(v + 2) {} void print_val() { cout << "hello:" << i << " " << j << endl;} private: int i; int . 阅读全文
posted @ 2013-12-16 22:56 jihite 阅读(715) 评论(0) 推荐(0)
摘要:假设文件内容为1. hello1 hello2 hello3 hello42. dsfjdosi3. skfskj ksdfls输出每个单词代码#include #include #include #include #include using namespace std;int main(){ string word; ifstream infile("text"); if(!infile) { cout > word) cout >传到word(间隔福为Tab, Space, Enter)输出每一行代码#include #include... 阅读全文
posted @ 2013-12-09 21:31 jihite 阅读(3756) 评论(0) 推荐(1)
摘要:本质 '\0'就是8位的00000000,因为字符类型中并没有对应的这个字符,所以这么写。'\0'就是 字符串结束标志。 '\0'是转义字符,意思是告诉编译器,这不是字符0,而是空字符。空字符\0对应的二进制为00000000,而数字0为00110000 原来,在C语言中没有专门的字符串变量,通常用 阅读全文
posted @ 2013-12-09 08:39 jihite 阅读(24143) 评论(2) 推荐(2)
摘要:源自c++primer 4th, 248页代码#include #include #include using namespace std;int main(){ int ival; while(cin >> ival, !cin.eof()) { cout ::max(), '\n'); continue; } } }几个地方1. 逗号表达式首先计算每一个操作数,然后返回最右边的操作数最为整个操作的结果,因此while(cin >> ival, !cin.eof())看重的只是!cin.eof(),而对前边的cin>>... 阅读全文
posted @ 2013-12-08 17:42 jihite 阅读(3503) 评论(2) 推荐(3)
摘要:缘起#include #include #include using namespace std; int main(){ ofstream fs; //输出文件流 (正确) ostringstream os; //输出string流(正确) ostream o; //输出普通流 (错误)}解惑头文件iostream定义了三个类iostream, istream, ostream(三个普通流), 都存在这样的问题:不可以直接定义无参数的对象。原因可以从编译器提示看出 类中无参数的构造函数定义为protected,因此不可直接... 阅读全文
posted @ 2013-12-06 23:14 jihite 阅读(1108) 评论(0) 推荐(0)