
C++
blueboy82006
孤独漂泊在程序海洋中的小浦原
展开
-
C++中重载的一个特例
大家都知道,具有相同名字而形参表不同的函数称为重载函数。这里只是要注意一个特殊的情况:const形参与非const形参,对于重载而言无本质区别,看下面的例子:int fun(int a){ return a;}int fun(const int a){ return a;} 编译时报错:error C2084: function int __cdecl原创 2010-02-07 21:07:00 · 1102 阅读 · 2 评论 -
bit_map
unsigned char flags[1000];int getv(int idx){ int i=idx>>3; int j=idx&0x7; //return ((flags[i]&(0x1>j); return ((flags[i]>>j)&0x01);}int setv(int idx){ int i=idx>>3; int原创 2012-02-22 13:20:04 · 615 阅读 · 0 评论 -
ConvertBST2DoubleList
void Convert(Node * r,Node * & first,Node * & last){ Node *firstL,*lastL,*firstR,*lastR; if(r==NULL) return; Convert(r->left,firstL,lastL); Convert(r->right,firstR,lastR);原创 2012-02-22 13:44:17 · 478 阅读 · 0 评论 -
find_seq
int cnt(0);int howmany(0);void find_seq(int A1, int n, int sum){ howmany++; int tmp=(A1+(n-1)/2.0)*n; if(tmp > sum) return; else if(tmp==sum) { cout<<"the seque原创 2012-02-15 14:01:07 · 617 阅读 · 0 评论 -
find_mid
int find_mid( int *a, int *b, int length){ if (length == 1)//这里应该根据中位数的定义取大小 return a[0]<b[0]?a[0]:b[0]; int i = length/2; if (a[i] == b[i]) return a[i]; else if (a[i原创 2012-02-15 14:04:34 · 583 阅读 · 0 评论 -
CreatBT
BTNode *CreatBT(char *post,char *in,int n,int m){ BTNode *s; char *p,*q,*maxp; int maxpost,maxin ,k; if(n<=0) return NULL; maxpost=-1; for(p=in;p<in+n;p++) for(q=post;q<post+m;q++) if(*p==原创 2012-02-15 14:31:38 · 640 阅读 · 0 评论 -
abc
C++语言: static int dictExpand(dict*ht,unsigned long size) { dict n; /* the new hashtable */ unsigned long realsize= _dictNextPower(size),i; /* the size is invalid if it原创 2012-02-16 11:22:33 · 582 阅读 · 0 评论 -
insertNew
void insertNew(BTNode *tree, BTNode *new_node){ int llcnt(0),lrcnt(0),rrcnt(0); BTNode *lch,*rch,*p,*lb; if(tree->lchild==NULL)//insert tree->lchild=new_node; else if(tree->rc原创 2012-02-15 15:46:44 · 649 阅读 · 0 评论 -
CreateBTfromPre
BTNode *CreateBTfromPre(char *pre,char *in,int n,int m){ BTNode *s; char *p,*q,*minp; int minpre,minin ,k; if(n<=0) return NULL; minpre=m+1; for(p=in;p<in+n;p++) for(q原创 2012-02-15 16:52:59 · 731 阅读 · 0 评论 -
findMaxDistance
int BTNodeDepth(BTNode *b){ int lchilddep,rchilddep; if(b==NULL) return 0; else { lchilddep=BTNodeDepth(b->lchild); rchilddep=BTNodeDepth(b->rchild); r原创 2012-02-17 20:16:27 · 750 阅读 · 0 评论 -
void CBTInsert(BTNode *tree, BTNode *new_node)
void CBTInsert(BTNode *tree, BTNode *new_node){ int llcnt(0),lrcnt(0),rrcnt(0); BTNode *lch,*rch,*p,*lb; if(tree->lchild==NULL)//insert tree->lchild=new_node; else if(tree->rc原创 2012-05-01 22:24:51 · 1301 阅读 · 0 评论 -
_strtok
char* _strtok(char *s, const char *dm){ static char *last; char *tok; const char* pdm=dm; if(s == NULL) s = last; if(s == NULL) return NULL; tok = s; while原创 2012-05-29 15:54:01 · 570 阅读 · 0 评论 -
adsort
//ShellSort 希尔排序void ShellSort(int Data[],int n){ int i,j,gap; int temp; gap=n/2; while(gap>0) { for(i=gap;i<n;i++) { temp=Data[i]; j=i-gap; while(j>=0 && temp<D原创 2012-05-13 10:54:46 · 620 阅读 · 0 评论 -
今天明白了一点模板函数的事情
下面这个代码出错: //test.htemplatevoid fun(T x); //test.cpp#include "test.h"templatevoid fun(T x){ std::cout<<x<<std::endl;} //main.cpp#include#include "test.h"int ma原创 2010-05-17 07:16:00 · 596 阅读 · 0 评论 -
More Effective C++ 读书笔记 之 不以多态方式处理数组
最关键的问题是派生类和基类的大小可能不一致。这样两种类型的数组的元素间距(也就是元素大小)就不同。另外,C++标准中规定:通过基类指针删除一个由派生对象构成的数组,其结果未定义。 结论:由于多态和指针算术不能混合运用,而数组对象几乎总是涉及指针的算术运算,所以数组和多态不应混合使用。原创 2010-05-17 08:27:00 · 594 阅读 · 0 评论 -
说说C++类中的static成员
说到这个问题有一句经典的描述类中static成员的话:static成员是类的组成部分但不是任何对象的组成部分,static成员独立于类的任何对象而存在,由类的全体对象共享 static数据成员: 每个static成员是与类关联的对象,并不与该类的对象相关联;static数据成员必须在类定义体的外部定义且正好一次;一个特原创 2010-02-07 21:39:00 · 473 阅读 · 0 评论 -
C++类中指针成员的管理
在C++中尽管使用标准库能够大大减少程序中对指针的需要,但有时仍不得不使用指针,当在类中使用指针数据成员时,这个类就具有指针的所有缺陷。为此我们看看下面这个特意编写的类:#includeusing namespace std;class PtrClass{public: PtrClass(int *p,int i):ptr(p),val(i){} int *ge原创 2010-02-12 17:11:00 · 1646 阅读 · 0 评论 -
通过例子回顾C++基本知识(一)
引自C++Primer第四版中的类的例子,稍作修改并啰嗦了一些注释,用来回顾C++面向对象的基本知识。 #include#includeusing namespace std;//vc++6.0的问题,友员要前向声明class Sales_item;istream& operator>>(istream&,Sales_item&);ostream& op原创 2010-02-08 19:15:00 · 715 阅读 · 0 评论 -
通过例子回顾C++基本知识(二)
C++中派生与继承的基本知识: #include#includeusing namespace std;class Item_base{//没有任何打折策略的基类public: Item_base(const string &book="",double sales_price=0.0): isbn(book),price(sales_price){}原创 2010-02-08 19:42:00 · 608 阅读 · 0 评论 -
如何去除VC6.0中使用STL时的warning C4786
由于VC的bug在使用STL中的map时常常要出现很多C4786的warning,实在烦人~我们可以手动关掉这一类的warning: 只需在程序开头加上 #pragma warning(disable: 4786) 即可。原创 2010-02-16 21:36:00 · 2353 阅读 · 0 评论 -
fstream 和 中文路径 [转]
转载自:http://jackinelee.blog.hexun.com/20954695_d.htmlfstream 和中文路径 有时候用ifstream或ofstream打开带有中文路径的文件会失败。解决办法:1、使用C语言的函数设置为中文运行环境setlocale(LC_ALL,"Chinese-simplified");2、使用STL函数设置为系统语言环境std::lo转载 2010-02-16 22:50:00 · 1315 阅读 · 0 评论 -
写个简单的makefile
有如下三个文件,我们以此来写个基本的makefile test.h#ifndef TEST_H_H#define TEST_H_H#include#include#includeclass Test{public: void vector_insert(std::string&); void vector_print();private: s原创 2010-02-20 12:25:00 · 790 阅读 · 0 评论 -
用模板函数解决未知类型未知维数的数组的输出
如何用模板函数解决未知类型未知维数的数组的输出?这个问题困扰了我好长时间,刚在论坛发贴才明白了原由,正好有时间写出来大家看下。下面这个写法可以正常运行但必须事先确定数组的维数,代码还有待完善 //通过模板函数输出未知类型、未知大小的数组#include#includeusing namespace std;template void print_array(原创 2010-02-18 19:13:00 · 1404 阅读 · 0 评论 -
C++中IO流的基本操作
#include#include#include#includeusing namespace std;int main(){ cout<<"hello flush"<<flush; cout<<"hello ends"<<ends; cout<<"hello endl"<<endl; /* //test cin.clear() && c原创 2010-02-26 16:10:00 · 1147 阅读 · 1 评论 -
C++中数据的精度和格式控制
//精度和格式控制#include#includeusing namespace std;int main(){ float n; cin>>setiosflags(ios::fixed)>>setprecision(2)>>n; cout<<setiosflags(ios::fixed)<<setprecision(2)<<n<<endl; float i=1原创 2010-02-26 16:12:00 · 1693 阅读 · 0 评论 -
More Effective C++ 读书笔记 之 区别pointer和reference
reference不能是空,必须有初值。通常在reference和point都适用时,reference有更好的效率(因为reference不需要测试是否为NULL)。当需要指向某个东西而且决不会改指其它东西,或是当实现一个操作符而其语法需求无法由pointer达成时(如operator[]),就应该选择references;任何其他时候,采用pointer.原创 2010-05-11 22:04:00 · 688 阅读 · 0 评论 -
More Effective C++ 读书笔记 之 C++转型操作符
取代旧式C的:static_cast改变常量性的:const_cast用于继承体系的:dynamic_cast还有一个:reinterpret_cast,与编译平台有关,常用于转换函数指针类型。原创 2010-05-17 08:19:00 · 504 阅读 · 0 评论 -
IsBalanced
bool IsBalanced(BinaryTreeNode* pRoot, int* pDepth){ if(pRoot == NULL) { *pDepth = 0; return true; } int left, right; if(IsBalanced(pRoot->m_pLeft, &left)原创 2012-05-21 10:57:40 · 778 阅读 · 0 评论