随笔分类 -  C++ Primer

摘要:1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 using namespace std... 阅读全文
posted @ 2014-08-10 09:20 CrazyCode. 阅读(482) 评论(0) 推荐(0)
摘要:string类型的输入操作符: 1.读取并忽略开头所有的空白字符(如空格,换行符,制表符). 2.读取字符直至再次遇到空白字符,读取终止。 用getline读取整行文本 getline.接受两个参数:一个输入流对象和一个string对象. int main() { string line; while(getline(cin,line)) co... 阅读全文
posted @ 2013-10-28 19:16 CrazyCode. 阅读(137) 评论(0) 推荐(0)
摘要:int a =2,b=3; int &c= a; c指向a以后就永久的指向a了 cout<<c<<endl; c=b;//这里并不是让c指向了b.然后让a=b; cout<<a<<endl; 阅读全文
posted @ 2013-10-15 13:29 CrazyCode. 阅读(126) 评论(0) 推荐(0)
摘要:c++中,模板是泛型编程的基础.模板是创建类或函数的蓝图或公式。函数模板是一个独立于类型的函数,可作为一种方式,产生函数的特定类型版本。模板定义以关键字template开始,后接模板形参表,模板形参表是用尖括号括住的1个或多个模板形参的列表,形参之间以逗号分隔.#include "stdafx.h"#include #includeusing namespace std;template int compare(const T &v1,const T &v2){if(v1inlineT min(const T&,const T&);模板类型形 阅读全文
posted @ 2013-10-11 01:05 CrazyCode. 阅读(273) 评论(0) 推荐(0)
摘要:// leran.cpp : 定义控制台应用程序的入口点。#include "stdafx.h"#include#include #include#includeusing namespace std;class GT_cls{public:GT_cls(size_t val = 0):bound(val){}bool operator()(const string &s){return s.size()>=bound;}private:std::string::size_type bound;};int _tmain(int argc, _TCHAR* ar 阅读全文
posted @ 2013-10-09 13:10 CrazyCode. 阅读(233) 评论(0) 推荐(0)
摘要:在函数形参表后面写上 =0 以指定纯虚函数:class Disc_item:public Item_base{public: double net_price(std::size_t) const = 0;}将函数定义为纯虚能够说明,该函数为后代类型提供了可以覆盖的接口。但是这个类中的版本绝不会调用。重要的是,用户将不能穿件Disc_item类型的对象。试图创建抽象基类的对象将发生编译时错误.Disc_item discounted;//error:can't define a Disc_item object;Bulk_item bulk;//Bulk_item继承自Disc_ite 阅读全文
posted @ 2013-10-09 13:09 CrazyCode. 阅读(201) 评论(0) 推荐(0)
摘要:派生类构造函数 派生类的构造函数受继承关系的影响,每个派生类构造函数除了初始化自己的数据成员之外,还要初始化基类,也会运行基类的构造函数 。#include "stdafx.h"#include using namespace std;class Ctest{public:Ctest(int a=1,int b=2,int c=3){cout discount_policy();itemP -> discount_policy();//错误...因为在Item_base里面没有这个方法discount_policy();设计派生类的时候,只要可能,最好避免与基类成员的 阅读全文
posted @ 2013-10-09 13:07 CrazyCode. 阅读(249) 评论(0) 推荐(0)
摘要:protected成员可以认为protected访问标号是private 和public 的混合:1.像private成员一样,protected成员不能被类的用户访问.2.像public成员一样,protected成员可被该类的派生类访问.此外,protected还有另一个重要性质:派生类只能通过派生类对象访问其基类的protected成员,派生类对其基类类型对象的protected成员没有特殊访问权限.少量的理解:#include "stdafx.h"#includeusing namespace std;class Ctest{public:Ctest(int a=1 阅读全文
posted @ 2013-10-07 10:08 CrazyCode. 阅读(568) 评论(0) 推荐(0)
摘要:// container.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include#include#include#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ string a="in.txt"; string b="out.txt"; ifstream infile(a.c_str()); if(infile) { cout>ingood; cout<<ingood< 阅读全文
posted @ 2013-09-28 21:08 CrazyCode. 阅读(135) 评论(0) 推荐(0)
摘要:逗号操作符是一组由逗号分隔的表达式,这些表达死从左向右计算.逗号表达式的结果是其最后边表达式的值。如果最后边的操作数是左值,则逗号表达式的值也是左值。此类表达式通常用于for循环:int cnt=iec.size();for(vector::size_type ix=0;ix!=ivec.size();++ix,--cnt)ivec[ix]=cnt;上述的for语句在循环表达式中使ix自增1而cnt自减1。每次循环均要修改ix和cnt的值。当检验ix的条件判断成立时,程序将下一个元素重新设置为cnt的当前值.以上摘自书146面..我的理解int a(){return 1,2,3;}那么其他地方 阅读全文
posted @ 2013-09-28 20:34 CrazyCode. 阅读(182) 评论(0) 推荐(0)
摘要:#include using namespace std; void abc(int a,int b,int c) { cout<<a<<b<<c<<endl; } int main() { int i = 10; abc(i,i++,i++); return 0; }12 11 10 阅读全文
posted @ 2013-09-28 19:24 CrazyCode. 阅读(124) 评论(0) 推荐(0)
摘要:set和multiset容器的能力set和multiset容器的内部结构通常由平衡二叉树(balancedbinarytree)来实现。当元素放入容器中时,会按照一定的排序法则自动排序,默认是按照less排序规则来排序。这种自动排序的特性加速了元素查找的过程,但是也带来了一个问题:不可以直接修改set或multiset容器中的元素值,因为这样做就可能违反了元素自动排序的规则。如果你希望修改一个元素的值,必须先删除原有的元素,再插入新的元素。2set和multiset容器的操作ConstructorandDestructorsetc:创建一个空的set或multiset容器setc(op): 阅读全文
posted @ 2013-09-28 00:36 CrazyCode. 阅读(233) 评论(0) 推荐(0)
摘要:// container.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){typedef string *pstring;string a = "abc";//const pstring good = &a; 这一句和下一句的效果是一致的string *const good=&a;string b = "abcs";//good = b;re 阅读全文
posted @ 2013-09-24 03:32 CrazyCode. 阅读(180) 评论(0) 推荐(0)
摘要:Exmpl.h/////////#pragma once#includeclass Exmpl{public://默认构造函数Exmpl(){std::cout#include#include"Exmpl.h"#include"Employee.h"using namespace std;void fun(Exmpl obj){}void fun2(Exmpl &obj){}Exmpl fun3(){Exmpl obj;return obj;}int _tmain(int argc, _TCHAR* argv[]){//Exmpl a;//这里调 阅读全文
posted @ 2013-09-24 03:27 CrazyCode. 阅读(192) 评论(0) 推荐(0)
摘要:// container.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){char ca1[]={'1','2','3'};cout<<sizeof(ca1)/sizeof(char)<<endl;//输出3char ca2[]="123";cout<<sizeof(ca2)/s 阅读全文
posted @ 2013-09-24 03:25 CrazyCode. 阅读(170) 评论(0) 推荐(0)
摘要:char *words[]={"abbc","plump","buck","mulligan"}这里其实是定义了一个指针数组.顺序容器的元素排列次序与元素值无关,而是由元素添加到容器中的次序决定的.顺序容器:vector list deque顺序容器适配器 stack 后进先出LIFO栈 queue 先进先出FIFO队列. priority_queue有优先级管理的队列.将一个容器复制给另一个容器时,类型必须匹配:容器类型和元素类型都必须相同.接受容器大小做形参的构造函数只适用于顺序容器,而关联容器不支持这种初始化 阅读全文
posted @ 2013-09-02 03:10 CrazyCode. 阅读(216) 评论(0) 推荐(0)
摘要:用数组的长度去除以数组里面数据类型的长度.就能求出数组里面的元素的个数.int ia[]={0,1,1,2,3,5,8,13,21,55,89};int count = sizeof(ia)/ sizeof(ia[0]); 阅读全文
posted @ 2013-09-02 02:40 CrazyCode. 阅读(283) 评论(0) 推荐(0)
摘要:函数不能返回另一个函数或者内置数组类型,但可以返回指向函数的指针,或者指向数组元素的指针的指针.207面:形参的长度会引起误解.当编译器检查数组形参关联的实参时,它只会检查实参是不是指针,指针的类型和数组元素的类型是否匹配,而不会检查数组的长度.交换a和b的值有两个方法1.通过引用:#include using namespace std;void swapa(int &v1,int &v2){ int tmp = v2; v2 = v1; v1 = tmp;}int main(){int a=6,b=7; swapa(a,b);coutusing namespace std; 阅读全文
posted @ 2013-08-19 13:24 CrazyCode. 阅读(200) 评论(0) 推荐(0)
摘要:cout#include //要用到格式控制符void main() {double amount = 22.0/7;cout <<amount <<endl;cout <<setprecision(0) <<amount <<endl <<setprecision(1) <<amount <<endl <<setprecision(2) <<amount <<endl <<setprecision(3) <<amount << 阅读全文
posted @ 2013-08-17 20:10 CrazyCode. 阅读(1313) 评论(0) 推荐(0)
摘要:简洁即是美:*iter++这样的符号:由于后自增操作符的优先级高于解引用操作,因此*iter++等效于*(iter++).子表达式iter++使iter加1,然后返回iter原值的副本作为该表达式的结果.注意一些简洁的用法:详细解释:*dest++ = *source++;1)指针dest加1 (2)指针source加1(3)将source原来所指向的对象赋给dest原来所指向的对象.#include #include using namespace std;int main(){ int arr1[]={1,2,3,4,5,8,9}; int *source = arr1; size_... 阅读全文
posted @ 2013-08-14 19:01 CrazyCode. 阅读(158) 评论(1) 推荐(0)