随笔分类 -  C/C++

1

C++ Primer 读书笔记 - 第十五章
摘要:1. OOP基于三个基本概念 - data abstraction,inheritance,dynamic binding. 其关键思想是多态性(polymorphism)。2. 定义为virtual的函数是基类期待派生类重新定义的,基类希望派生类继承的函数不能定义为虚函数。3. virtual关键字只能出现在类体内的声明中,不能出现在类体外的定义中。4. 派生类只能通过派生类对象访问其基类的protected成员,派生类对其基类类型对象的protected成员没有特殊访问权限。void Bulk_item::memfcn(const Bulk_item &d, const Item_ 阅读全文

posted @ 2013-06-04 02:36 NULL00 阅读(1747) 评论(0) 推荐(1)

C++ Primer 读书笔记 - 第十四章
摘要:1. An overloaded operator must have at least one operand of class or enumeration type.2. Default arguments for overloaded operators are illegal, except for operator(), the function-call operator.3. The assignment (=), subscript ([]), call (()), and member access arrow (->) operators must be defin 阅读全文

posted @ 2013-06-02 00:59 NULL00 阅读(710) 评论(0) 推荐(0)

C++ Primer 读书笔记 - 第十三章
摘要:1. Initialization和Assignment不一样。其中Initialization包括direct-initialization (如A a(...))和copy-initialization (如 A a = b;) 注意A a = b为copy-initialization, 而A a; A b; a = b;为Assignment。2. We cannot copy objects of the IO types, so we cannot use copy-initialization on objects of these types.3. As the copy .. 阅读全文

posted @ 2013-05-31 03:36 NULL00 阅读(450) 评论(0) 推荐(1)

C++ Primer 读书笔记 - 第十二章
摘要:1. 类中成员默认属性为private,private成员只对类里面的其他成员可见。2. 在类里面定义的成员函数默认为inline函数。如果只是在类中声明,那么其定义也要有inline关键字,inline关键字在声明和定义处任意出现一次就行。inline函数的定义必须出现在头文件中。3. 声明为const的成员函数不能改变调用它的对象的数据成员,const在声明和定义中都要写明。4. data abstraction是指我们只知道接口(interface),不知道内部如何实现(implementation)。5. encapsulation是指将低层次的元素组装成高层次的实体。例如一个函数,一 阅读全文

posted @ 2013-05-30 11:04 NULL00 阅读(551) 评论(0) 推荐(1)

C++ Primer 读书笔记 - 第十一章
摘要:#include <algorithm>1. find()函数2. accumulate()函数3. find_first_of()函数的两个range的类型可以不同,但是两个range内部的类型必须相同。4. fill()函数,使用时必须保证空间是足够的。 fill_n(back_inserter(vec), 10, 0);//appends 10 elements to vec5. replace()函数改变容器内部,replace_copy()函数不改变容器内部,而是自己新拷贝一份。6. sort()函数,unique()函数,unique_copy()函数,stable_s 阅读全文

posted @ 2013-05-29 07:06 NULL00 阅读(774) 评论(0) 推荐(0)

C++ Primer 读书笔记 - 第十章
摘要:1. map和set中的key是唯一的,multimap和multiset中的key可以出现多次。2. Whenever we use an associative container, its keys have not only a type, but also an associated comparsion function.3. The value_type is a pair and that we can chagne the value but not the key member of that pair.4. 用下标插入元素时,内部操作是这样的: - 找key,找不到 .. 阅读全文

posted @ 2013-05-28 09:23 NULL00 阅读(540) 评论(0) 推荐(0)

C++ Primer 读书笔记 - 第九章
摘要:1. Two constraints that element types must meet: - The element type must support assignment. - We must be able to copy objects of the element type. Soreferences, IO library types, auto_ptr typeare not permitted.2. Only vector and deque support iterator arithmetic and the use of relational operato... 阅读全文

posted @ 2013-05-28 02:41 NULL00 阅读(505) 评论(1) 推荐(0)

C++ Primer 读书笔记 - 第八章
摘要:1. IO library types do not allow copy or assignment. Only element types that support copy can be stored in vectors or other container types. We cannot have a parameter or return type that is one of the stream types. If we need to pass or return an IO object, it must be passed or returned as a poin.. 阅读全文

posted @ 2013-05-27 06:58 NULL00 阅读(575) 评论(0) 推荐(0)

C++ Primer 读书笔记 - 第七章
摘要:1. To invoke a function we use the call operator, which is a pair of parentheses. The operands to the call operator are the name of the function and a (possibly empty) comma-separated list of arguments.#include <iostream>using namespace std;string::size_type find_char(const string &s, char 阅读全文

posted @ 2013-05-26 12:11 NULL00 阅读(453) 评论(0) 推荐(0)

C++ Primer 读书笔记 - 第六章
摘要:这一章讲控制结构,和C,Java没区别。跟C不同的是,C++增加了try blocks and Exception Handling#include <iostream>#include <stdexcept>using namespace std;void foo(){ int b = 0; if (b == 0) throw runtime_error("b == 0"); int a = 4/b; cout << a << endl;}void bar() throw(int){ throw 5;}int main(){ 阅读全文

posted @ 2013-05-25 03:59 NULL00 阅读(444) 评论(0) 推荐(0)

C++ Primer 读书笔记 - 第五章
摘要:这一章的内容和C语言基础知识大同小异。1. i++ 与 ++i 的效率问题 i++的话,需要保存原来的值,然后增加 i,之后返回 i 原来的值;++i 直接增加 i,然后返回当前的 i 值,所以少做一步工作。2. Setting the pointer to 0 after the object it refers to has been deleted makes it clear that the pointer points to no object. It is legal to delete a pointer whose value is zero; doing so has .. 阅读全文

posted @ 2013-05-20 10:01 NULL00 阅读(365) 评论(0) 推荐(0)

C++ Primer 读书笔记 - 第四章
摘要:这一章更多的是在讲C语言的东西。Pointers to const Objectsconst double *ptrconst Pointersdouble *const ptr = &value //必须初始化const Pointer to a const Objectcosnt double *const ptr = &valuePointers and TypedefsTypedef string *pstringconst pstring cstr;其实const pstring cstr 等价于 string *const cstrconst int *pci_ok 阅读全文

posted @ 2013-05-20 04:11 NULL00 阅读(413) 评论(0) 推荐(1)

C++ Primer 读书笔记 - 第三章
摘要:1. std::string size()函数返回值为string::size_type,用下标时,也用string::size_type作为index的类型#include <iostream>#include <cstdio>#include <string>using namespace std;int main(){ string s = "abc"; cout << s << endl; cin >> s; cout << s << endl; string line; 阅读全文

posted @ 2013-05-19 13:01 NULL00 阅读(403) 评论(0) 推荐(0)

C++ Primer 读书笔记 - 第二章
摘要:1. 这章谈到了初始化和赋值其实是不同的,这使我想起了构造函数中的普通构造函数,拷贝构造函数,赋值构造函数,详见博文C++中的构造函数与析构函数 string这个类有default constructor,即初始化为"".2. 一个变量在一个程序中能够声明多次,但只能定义一次。3. Nonconst varialbes are extern by default. To make a const variable accessible to other files we must explicitly specify that it is extern.4. There i 阅读全文

posted @ 2013-05-19 10:27 NULL00 阅读(522) 评论(0) 推荐(0)

C++ Primer 读书笔记 - 第一章
摘要:这一章算是简介。自己写了几个程序。1. main函数的返回值int main(){ //after ./a.out, execute 'echo $?', it will output 7. return 7;}2. 对comment的理解#include <iostream>using namespace std;int main(){ cout << "/*"; cout << "*/"; cout << /* "*/" */";}3. 对输入的理解#inc 阅读全文

posted @ 2013-05-19 08:02 NULL00 阅读(752) 评论(3) 推荐(0)

变长参数表
摘要:以实现函数printf的一个最简单版本为例,介绍如何以可移植的方式编写可处理变长参数表的函数。因为我们的重点在于参数的处理,所以,函数minprintf只处理格式字符串和参数,格式转换则通过调用函数printf实现。 函数printf的正确声明形式为:int printf(char* fmt, ...) 其中,省略号表示参数表中参数的数量和类型是可变的。省略号只能出现在参数表的尾部。因为minprintf函数不需要想printf函数一样返回实际输出的字符数,因此,我们将它声明为下列形式:void minprintf(char* fmt, ...) 编写函数minprintf的关键在于如何处理. 阅读全文

posted @ 2011-05-29 23:46 NULL00 阅读(884) 评论(0) 推荐(0)

C++中的构造函数与析构函数
摘要:在C++中,当类中含有指针类型的数据时,我们往往要用到三种构造函数,即普通构造函数、拷贝构造函数、赋值构造函数,而且在析构函数中,要正确析构相应的内存。 下面以代码为实例进行演示,同时推荐大家看看林锐博士的《高质量C++/C 编程指南》,其中对编程规范和一些常见的问题进行了深度剖析,《高质量C++/C 编程指南》电子版下载地址为http://download.csdn.net/source/3091633。#include <iostream>#include <cstring>#include <cstdio>using namespace std;cla 阅读全文

posted @ 2011-03-14 17:15 NULL00 阅读(3331) 评论(0) 推荐(0)

以二进制方式输出数字在内存中的存储形式
摘要:对于二进制表示的float类型的2.5,其在内存中的表示为01000000 00100000 00000000 00000000,如果我们想打印出它在内存中是如何表示的,那么我们可以用1进行移位,与每个比特进行与运算,还是看看代码吧: 对于输入参数,在visual studio下可以点击项目->属性->配置属性->调试->命令参数进行输入,在visual c++6.0可以点击工程->设置->调试->程序变量里输入。输入2.5打印看看,其结果与我们预期相同。#include <cstdlib> #include <iostream> 阅读全文

posted @ 2008-11-27 21:30 NULL00 阅读(1219) 评论(0) 推荐(0)

C++中“#”号妙用
摘要:在写程序时,我们经常要输出,比如printf,cout,当你要输出表达式的值,并要在前面输出表达式的形式时,有时会有点麻烦!比如cout<<"a*b(c-d):"<<a*b(c-d)<<endl;有时像"a*b(c-d):"这样的表达式懒得写,或很多这样的表达式,写起来也浪费时间,我们可以用“#”来减少麻烦!貌似没说清楚,举个例子:#include <iostream> using namespace std;#define P(EX) cout<<#EX<<":" 阅读全文

posted @ 2008-11-18 12:41 NULL00 阅读(415) 评论(0) 推荐(0)

C++用substr()函数消除前后空格
摘要:最近做了个题目,遇到了要将字符串前后空格消除的细节问题。在Java中好像有一个字符串函数为trim()可以消除字符串后的空格。对于c++,查了一下,可以引用一个c++标准库Boost,可以轻松解决,但要下载,设置环境变量,因而没去弄。当然还可以用正则表达式进行匹配,但似乎都大材小用。不如就用substr()函数,而且string有find_last_not_of,find_first_not_of等等属性,已经够我们解决问题了。#include <iostream> #include <vector> #include <string> #include & 阅读全文

posted @ 2008-11-01 15:41 NULL00 阅读(5990) 评论(0) 推荐(0)

1

导航