
C++
文章平均质量分 63
bladeLight
这个作者很懒,什么都没留下…
展开
-
拷贝构造函数
拷贝构造函数是一类特殊的构造函数,它由编译器调用来完成一些基于同一类的其他对象的创建及初始化。我们先用一个简单例子来说明。 #include using namespace std; class A{ public: A(){ cout<<"无参构造函数"<<endl; a=0; } A(int b){ cout<<"有参构造函数"<<endl; a=b; } i原创 2012-06-14 20:36:41 · 396 阅读 · 0 评论 -
通过二级指针插入节点
这是在编程珠玑的习题上看到的,P214第四题 节点的定义如下: struct node{ int val; node *next; node(int v,node *p){val=v; next=p;} };另外初始化的过程中,初始化头结点,head=new node(maxval,0)其中maxval表示一个最大值 通常的插入函数都会这样来写: void insert(int t)原创 2013-07-21 15:54:12 · 985 阅读 · 1 评论 -
const char* 的真正含义
#include #include #include using namespace std; int main(){ char greeting[]="Hello"; const char* p=greeting; //p[0]='m'; greeting[0]='m'; printf("%s\n",p); system("pause"); return 0; } 程序的输出结原创 2013-06-11 18:40:46 · 13105 阅读 · 1 评论 -
纯虚函数
#include #include #include //#include using namespace std; class Shape{ public: virtual void draw() const=0; virtual void error(const std::string& msg){} int objectID() const{} }; void Shape:原创 2013-05-31 11:14:03 · 623 阅读 · 0 评论 -
打印ASCII码到Console
ASCII码被人熟知,它的全称为American Standard Code for Information Interchange,即美国信息交换标准码。标准ASCII码的范围是0x00 - 0x7F,一共128个字符;扩展ASCII码范围是0x80 - 0xFF,一共也是128个字符。先编写程序将ASCII码表打印到Console窗口中,代码如下所示: #includestd转载 2013-05-28 21:53:52 · 918 阅读 · 0 评论 -
局部静态对象
局部静态对象指的是函数内的static对象,首先我们看下面这段代码: class A{ public: A& get(){ static A a; return a; } }; int main(){ A a; cout<<&a.get()<<endl;//测试调用多次地址是否一致 cout<<&a.get()<<endl; A b; cout<<&b.ge原创 2013-05-28 14:41:06 · 960 阅读 · 0 评论 -
类中静态变量与const常量成员的初始化
//类中,静态变量与const常量的赋值: //static 成员在类外初始化 //const 成员(及引用成员)在类的构造函数初始化列表中初始化 //static const /const static 成员可以在类中初始化(实际上是申明)也可以不初始化,同时需要在类外定义 #include "stdafx.h" #include #include using n转载 2013-04-02 10:08:40 · 1799 阅读 · 0 评论 -
如何判断new的内存是否失败?使用try catch
#include #include #include #include #include using namespace std; int main() { try { while(1) { //char *p = (char*)malloc(1000000*sizeof(char)); char *p转载 2013-03-13 14:23:40 · 3573 阅读 · 0 评论 -
关于常量const
1、C++中的const默认为内部连接,即const仅在const被定义过的文件里才是可见的,而在连接时不能被其他编译单元看到,除非使用extern显式说明为外部连接。 2、定义一个const时,必须赋一个初值给它,除非使用extern做了清楚的说明。 3、使用extern意味着使用外部连接,因此必须分配存储空间,即有几个不同的编译单元应当能够引用它,所以必须分配存储空间。 4、const可原创 2013-01-03 21:43:31 · 358 阅读 · 0 评论 -
c++中的公有继承
#include using namespace std; class A { public: int a; }; //b要想能够访问a,必须写成公有继承A class B: public A { public: int c; int b; }; int main() { B *b=new B(); b->c=3; b原创 2012-10-31 15:31:49 · 424 阅读 · 0 评论 -
分群源程序
#include #include #include #include using namespace std; struct Node{ //节点坐标 double x,y; //节点id int id; //节点状态0 1 2 3,分别表示管理者 群首 群成员 未分群节点 int state; //邻居个数 int num_of_neigh; //邻居节点列表 int n原创 2012-09-05 22:07:51 · 581 阅读 · 0 评论 -
c++中流的操作
最近遇到了一个问题,定义流ifstream fin("a.txt");操作完成后,调用fin.close();当我再用该流打开另一个文件fin.open("b.txt")的时候就不行了,解决办法是关闭之后还要clear一下。突然想到一个问题:如果a没有读完的话,fin就能继续用,但是这时候如果用来打开b.txt的话,还会从b文件的起始位置开始读吗?原创 2012-09-04 11:08:47 · 528 阅读 · 0 评论 -
交换两个数的函数int swap(int &a,int &b)
最近突然想到以前看的交换两个整数的巧妙写法的限制条件忘了,所以就再看了一遍: #include using namespace std; int a[]={1,2,3,4}; void swap(int &a,int &b){ a=a^b; b=a^b; a=a^b; } int main(){ int m=1,n=2; swap(m,n); swap(a[1],a[1])原创 2012-08-16 16:44:14 · 9737 阅读 · 0 评论 -
二叉树的层序遍历
//分层遍历 #include #include using namespace std; struct Node{ char data; int level; Node *left; Node *right; }; //写了这么长时间才发现要这样写,不这样写会出错,为什么? //就像一个函数如果只是传入值的话,结果并不改变 //比如,int a=5;void add(int a){a+原创 2012-08-18 21:50:58 · 644 阅读 · 0 评论 -
memset函数简单分析
这里我们只讨论整数的情况,因为acm中经常要用到。 我先几个简单的,大家都能说出结果的: #include using namespace std; int a[10]; int main(){ memset(a,0,sizeof(a));//结果为0 memset(a,255,sizeof(a));//结果为-1 return 0; } 我们需要知道其中的原理:对于第一个例子:0对原创 2012-08-19 16:16:00 · 846 阅读 · 0 评论 -
给定一个链表的头指针,要求只遍历一次,将单链表中的元素顺序反转过来
//使用了三个指针,每次修改pnext指向pcurrent,然后移动三个指针 #include using namespace std; struct Node{ int data; Node* next; }; int a[5]={1,2,3,4,5}; int main(){ Node *head; head=(Node *)malloc(sizeof(Node)); head原创 2012-08-17 13:54:10 · 3184 阅读 · 0 评论 -
strtok 分割字符串
#include #include #include int main(){ char ch[]="I\nam\nstudent"; char *tok=NULL; tok=strtok(ch,"\n"); while(tok){ printf("%s\n",tok); tok=strtok(NULL,"\n"); } system("pause"); return 0原创 2013-09-02 23:00:28 · 860 阅读 · 1 评论