08 2013 档案

摘要:程序1:void myMalloc(char *s) //我想在函数中分配内存,再返回{ s=(char *) malloc(100); }void main() { char *p=NULL; myMalloc(p); //这里的p实际还是NULL,p的值没有改变,为什么? if(p) free(p); }程序2:void myMalloc(char **s) { *s=(char *) malloc(100); } void main() { char *p=NULL; myMalloc(&p); //这里的p可以得到正确的值了 if(p) free(p); }程序3: #incl 阅读全文
posted @ 2013-08-30 10:37 星语海蓝 阅读(701) 评论(0) 推荐(0) 编辑
摘要:二级指针又叫双指针。C语言中不存在引用,所以当你试图改变一个指针的值的时候必须使用二级指针。C++中可以使用引用类型来实现。下面讲解C中的二级指针的使用方法。例如我们使用指针来交换两个整型变量的值。错误代码如下:一级指针[cpp]view plaincopy#includevoidswap(int*a,int*b){int*tmp=NULL;tmp=a;a=b;b=tmp;}intmain(intargc,char**argv){inta=2;intb=3;printf("Beforeswapa=%db=%d\n",a,b);swap(&a,&b);prin 阅读全文
posted @ 2013-08-30 10:12 星语海蓝 阅读(341) 评论(0) 推荐(0) 编辑
摘要:用stl的find方法查找一个包含简单类型的vector中的元素是很简单的,例如vector strVec; find(strVec.begin(),strVec.end(),”aa”);假如vector包含一个复合类型的对象呢比如class A { public: A(const std::string str,int id) { this->str=str; this->id=id; } private: std::string str; int id; };这个时候一般的想法是写个函数遍历这个vector,然后进行比较查找。实际上在使用STL的时候,不建议使用循环遍历的查找方 阅读全文
posted @ 2013-08-28 10:45 星语海蓝 阅读(7377) 评论(0) 推荐(1) 编辑

点击右上角即可分享
微信分享提示