C++要点记录范文.doc_第1页
C++要点记录范文.doc_第2页
C++要点记录范文.doc_第3页
C++要点记录范文.doc_第4页
C++要点记录范文.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C+要点记录范文 难点笔记日期xx年1月13日函数指针 1、二级函数指针模型第一种最简单的内存模型#include#include#includevoid main()int i=0;char*arrayStr=1111,aaaa,ddd,c;/首先打印for(i=0;i4;i+)printf(%sn,arrayStri);/进行排序char*tmp=NULL;/定义一个临时char*指针for(i=0;i4;i+)for(int j=i+1;j0)tmp=arrayStri;arrayStri=arrayStrj;arrayStrj=tmp;/排序后进行打印for(i=0;i4;i+)printf(%sn,arrayStri);system(pause);二级指针做函数参数主调函数分配内存空间,被调函数使用,那么,被调函数中,作为参数,需要使用二级指针,进行传递char*数组类型列1#include#include#include/二级指针做函数参数void printArray(constchar*arrayStr,constint*num)int i=0;for(i=0;i*num;i+)printf(%sn,arrayStri);void main()int num=4;char*arrayStr=1111,aaaa,ddd,c;/调用函数printArray(&arrayStr,&num);system(pause);列2#include#include#include/二级指针做函数参数void printArray(constchar*arrayStr,constint*num)int i=0;for(i=0;i*num;i+)printf(%sn,arrayStri);/排序函数void str_cmp(constchar*arrayStr,constint*num)int i=0;int j=0;char*tmp=NULL;/定义一个临时char*指针,用于存储两个指针交换中间值for(i=0;i*num;i+)for(j=i+1;j0)void main()int num=4;char*arrayStr=1111,aaaa,ddd,c;/排序之前,调用函数printArray(&arrayStr,&num);/调用排序函数str_cmp(&arrayStr,&num);/排序之后printArray(&arrayStr,&num);system(pause);内存分配图:tmp=arrayStri;arrayStri=arrayStrj;arrayStrj=tmp;第二种内存模型#define_CRT_SECURE_NO_WARNINGS#include#include#include/打印二维数组void printMyArray(charmyArray3010,int*iNum)for(int i=0;i*iNum;i+)printf(%sn,myArrayi);/排序二维数组void cmpMyArray(charmyArray3010,int*iNum)char buf30;for(int i=0;i*iNum;i+)for(int j=i+1;j0)strcpy(buf,myArrayi);strcpy(myArrayi,myArrayj);strcpy(myArrayj,buf);void main()int iNum=4;char myArray3010=fasd,asdfa,dasdfdd,fasdf;/初始化二维数组/排序之前打印printMyArray(myArray,&iNum);/进行排序cmpMyArray(myArray,&iNum);/排序之后打印printMyArray(myArray,&iNum);system(pause);第三种内存模型,自定义内存空间 1、如何看待指针做一级二级多级指针只看作是变量,主要看指针所指向的内存空间的类型。 1.自定义二级指针内存空间。 2.定义一级指针,挂到二级指针上3.注意malloc内存释放#define_CRT_SECURE_NO_WARNINGS#include#include#include/打印自定义内存数组void printMyOwnArr(char*arrayStr,intarrLen)for(int i=0;i 2、字符串拆分将”ajsldfk,ajksdfj,asdfasjdkfla,asdfaskldjfk,ajskdlf,”通过”,”进行拆分,并将拆分的字符串装入二维char数组中。 #define_CRT_SECURE_NO_WARNINGS#include#include#include/ajsldfk,ajksdfj,asdfasjdkfla,asdfaskldjfk,ajskdlf,int splitStr(constchar*str,constchars,charbuf100100,int*ncount)int ret=0;int tmpcount=0;/定义一个临时大小,保存每次截取字符串的长度char*tmpStr=str;/定义临时char*,接收str char*tmpP=str;/将与tmpStr进行长度比较,截取字符串int count=0;/用于存储第几个字符串dotmpP=strstr(tmpStr,&s);tmpcount=tmpP-tmpStr;memcpy(bufcount,tmpStr,tmpcount);bufcounttmpcount=0;/printf(%sn,bufcount);count+;tmpStr=+tmpP;while(*tmpP!=0);*ncount=count;return ret;void main()char*str=ajsldfk,ajksdfj,asdfasjdkfla,asdfaskldjfk,ajskdlf,;/将要被拆分的字符串char s=,;/分割符char buf100100;/保存分割之后的字符int ncount=0;/保存所包含的分割后有几个字符splitStr(str,s,buf,&ncount);printf(共有%d个字符串n,ncount);for(int i=0;i 2、数组内容数组概念,数组指针#define_CRT_SECURE_NO_WARNINGS#include#include#includevoid main()/a代表数组首元素的地址,不代表整个数字的地址/&a代表整个数组地址,&a和a的数据类型不一样/&a数组类型/a数组元素的类型int a10=0;typedefint(MYINT)10;/MYINI是数据类型MYINT*array=&a;int(*myArray)10=&a;/直接定义一个指向数组的指针变量myArray,此时,myArray相当于一个二级指针for(int i=0;i10;i+)(*myArray)i=i;/(*myArray)表示a,(*myArray)i表示ai二维数组的实质9X9乘法表的打印#define_CRT_SECURE_NO_WARNINGS#include#include#includevoid main()int a99=0;/初始化一个二维数组for(int i=0;i9;i+)/行与行之间循环for(int j=0;j=j)aij=(i+1)*(j+1);printf(%dt,aij);printf(n);/每循环一行,进行换一行system(pause);二维数组名称,实际上是一维(数组指针)#define_CRT_SECURE_NO_WARNINGS#include#include#includevoid main()int a35=0;/初始化一个二维数组int(*myArray)5=a;/二维数组名称的实质,就是一维数组指针,/myArray是指向数组的一个指针,printf(a:%x myArray:%x,a,myArray);system(pause);判断字符串数组中,给定字符串所在位置#define_CRT_SECURE_NO_WARNINGS#include#include#includeint get_str_location(constchar*str,constchar*key,int*location,intlistCount)int ret=0;int tmpCount=0;/定义一个临时位置变量if(str=NULL|key=NULL|location=NULL)ret=-1;return ret;for(int i=0;i 3、函数指针函数指针定义函数名取地址和不取地址都是一样的,funcP=test;funcP=&test;#define_CRT_SECURE_NO_WARNINGS#include#include#includetypedefintFunc(int);int test(inta)returna*a;/函数指针void main()Func*funcP=NULL;/使用函数类型定义一个函数指针funcP=test;/=funcP=&test;/函数名本身就是一个函数指针,可以将其直接赋值给函数指针funcP (2);/直接使用函数指针调用函数即可/也可以按照下面方式进进行定义int(*funcP2)(int)=test;system(pause);函数指针做参数日期xx年1月14日#define_CRT_SECURE_NO_WARNINGS#include#include#includevoid test_20()printf(%sn,打印了);/函数指针做参数void print_test(void(*funcP)()funcP();void main()print_test(test_20);system(pause);函数的调用和回调函数:C+中接口类实质是指针函数。 泛型编程基本定义#includeusingnamespace std;template/泛型模板定义,template和typename关键字/函数体内部的函数结构一样,则可以使用泛型定义void swap_test(T&a,T&b)T c;c=a;a=b;b=c;void main()int a=1;int b=2;swap_test(a,b);/第一种使用方法printf(a:%d-b:%dn,a,b);double c=1.0;double d=2.0;swap_test(c,d);/第二种使用方法printf(c:%d-d:%dn,c,d);system(pause);STL编程内容Vector用法基本用法,需要包含头文件#include#include#includeusingnamespace std;void print_voctor(vector&v)void main()/定义一个vector vectorv1 (5);for(int i=0;i5;i+)v1i=i+1;vectorv2 (10);v2=v1;/将v1赋值给v2print_voctor(v2);system(pause);if(&v=NULL)return;int size=v.size();/获取v的元素个数for(int i=0;i queue和stack相对,但与之相反,先存先取。 #include#includeusingnamespace std;void main()stacks;/创建一个int类型的stack(栈)对象/为stack赋值for(int i=0;i10;i+)s.push(i+1);/push函数,装入/读取stack元素while(!s.empty()/empty是否为空int tmp=s.top();/top函数,获取顶端第一个元素,先进后出s.pop();/弹出printf(%dn,tmp);system(pause);List链表和iterator迭代器List是链表结构存储器,可以插入,读取等操作。 Iterator实质就是一个指针。 #include#includeusingnamespace std;void main()list_list;/定义个list链表for(int i=0;i5;i+)_list.push_back(i+1);/通过push_back,在_list链表末尾添加一个元素list:iterator current=_list.begin();/定义个list的迭代器,相当于一个指针,当前指向第一个元素while(current!=_list.end()/当current指针不是指向_list中最后一个元素的时候进行遍历cout*current#include#include#includeusingnamespace std;void_v_print_func(int&v)/回调函数cout 1、基本操作命令记录Cd进入文件夹Rm删除文件和目录pwd获取文件路径mkdir创建文件夹cp拷贝(整个文件内容拷贝)ps显

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论