20140729 while((*pa++=*pb++)!='\0') 合并数组代码 C++类型转换关键字 封装 多态 继承
2024-09-01 13:32:59
1、关于while((*pa++=*pb++)!='\0')和while((*pa=*pb)!='\0') {pa++;pb++;}的不同
#include<stdio.h> void main()
{
char a[]="abcde";
char b[]="gh";
char *pa=a;
char *pb=b;
while((*pa++=*pb++)!='\0')
;
printf("%s",pa);
}
执行前: a[]="abcde"; b[]="gh";
执行后:a[]="gh\0de"; b[]="gh";
pa最终指向的是d的位置。
#include<stdio.h>
void main()
{
char a[]="abcde";
char b[]="gh";
char *pa=a;
char *pb=b;
while((*pa=*pb)!='\0')
{pa++;pb++;}
printf("%s",pa);
}
执行前: a[]="abcde"; b[]="gh";
执行后:a[]="gh\0de"; b[]="gh";
pa最终指向的是\0的位置。
2、合并两个数组代码(已测试)
剑指offer49页、相关题目
#include<stdio.h>
#include<iostream>
const int length=;
using namespace std;
int *MergeArray(int *A1,int *A2,int len1,int len2)
{
if(A2==NULL)
return A1;
if(A1==NULL||len1<=||len2<=)
return NULL;
int len=len1+len2;
int indexofA1=len1-,indexofA2=len2-,indexofMerge=len-;
while(indexofA1>=&&indexofA1<indexofMerge)
//indexofA1<indexofMerge是为了防止无效赋值,即A1赋值自己。如 A1=1 2 3 4;A2=4 5 6 7的例子
{ if(A1[indexofA1]>A2[indexofA2])
{A1[indexofMerge--]=A1[indexofA1--];}
else if(A1[indexofA1]<A2[indexofA2])
{A1[indexofMerge--]=A2[indexofA2--];}
else if(A1[indexofA1]==A2[indexofA2])
{A1[indexofMerge--]=A1[indexofA1--];}
}
if(indexofA1==-)
{
for(int i=indexofA2;i>=;i--)
A1[i]=A2[i];
}
return A1;
} void display(int *arr, int len)
{
if(arr == NULL || len < )
return; for(int i = ; i < len; i++)
cout << arr[i] << " ";
cout << endl;
} int main()
{
int *arr1 = new int[length];
int *arr2 = new int[length];
int len1 = , len2 = ;
cout << "请输入数组1的数据(-1结束):" << endl;
int temp;
cin >> temp;
while(temp != -)
{
arr1[len1++] = temp;
cin >> temp;
} cout << "请输入数组2的数据(-1结束):" << endl;
cin >> temp;
while(temp != -)
{
arr2[len2++] = temp;
cin >> temp;
} MergeArray(arr1, arr2, len1, len2); display(arr1, len1 + len2);
return ;
}
2、四个C++的类型转换的关键字
//static_cast和reinterpret_cast<>()的区别
#include<iostream>
using namespace std;
void main()
{
float f=3.14;
int *i=reinterpret_cast<int *>(&f);//成功运行,float指针到int指针的转换可以通过编译
// int i=reinterpret_cast<int>(f);//失败运行,应为这是标准转换,编译器知道要用static_cast<int>
//int i=static_cast<int>(f)//隐式转换通过编译
//int *i=static_cast<int *>(&f)//不能通过编译,因为int指针类型和float类型不是标准转化,两种类型是无关类,编译出错
}
const_cast的作用:
class B{
public:
int m_iNum;
}
void foo(){
const B b1;
b1.m_iNum = 100; //comile error
B b2 = const_cast<B>(b1);
b2. m_iNum = 200; //fine
}
3、c++三个基本特征
http://blog.csdn.net/ruyue_ruyue/article/details/8211809
封装、继承,多态
封装:把数据和操作数据的方法整合到一起形成一个有机的整体(类),对数据的访问只能通过这些方法。作用:隐藏实现细节,使代码模块化
继承:使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。其继承的过程,就是从一般到特殊的过程。
多态:允许将子类类型的指针赋值给父类类型的指针,通过父类指针可以调用子类中的虚函数。
是每个类用了一个虚表,每个类的对象用了一个虚指针
最新文章
- apache配置rewrite及.htaccess文件(转载)
- Windows转到linux中,文件乱码,文件编码转换 &; 解决sqlplus连接oracle乱码
- COJ966 WZJ的数据结构(负三十四)
- 实现Web上的用户登录功能
- 新API:AttachThreadInput
- Java---注解、类加载器-加强-实现运行任意目录下class中加了@MyTest的空参方法
- JQuery实现悬浮工具条
- 安卓里面JSON处理和JAVA SE里面的JSON包
- angularjs bind与model配合双向绑定 表达式方法输出
- Java当中的IO三
- [Swift]LeetCode893. 特殊等价字符串组 | Groups of Special-Equivalent Strings
- 在网站开发时,可以设置防盗,不被复制和F12
- elasticsearch5.0.1集群一次误删除kibana索引引发的血案
- MVC源码分析 - Action/Result 过滤器(续)
- 【CSS系列】获取实时数据做进度
- React 回忆录(二)为什么使用 React?
- MVC下的DAO接口类和SERVICE接口类区别?
- java的finally简单理解
- Python日期字符串比较
- HDU 1874 SPFA/Dijkstra/Floyd