20140903 dynamic_cast和static的区别 链表的冒泡排序和插入排序
1、四个类型转换
http://www.cnblogs.com/chio/archive/2007/07/18/822389.html
static_cast和dynamic_cast的区别
- dynamic_cast的向下转化必须要有虚函数,
- 对于向上转化(子类->父类),两者是相同的。dynamic_cast<Base *>(&Derived)和static_cast<Base *>(&Derived)
- 对于向下转化(父类指针->子类指针),如果父类指针指向的是子类对象,那么两者相同;如果父类指针指向的是父类对象,那么static转换会不安全,dynamic_cast会返回NULL;
#include<iostream>
using namespace std;
class Base
{
public:
int m_num;
void fBase()
{
cout<<"Base is called"<<endl;
}
/*virtual void fBase1()
{ }*/
}; class Derived :public Base
{
public:
char *m_szName[100];
void fDeviced()
{
cout<<"Derived is called"<<endl;
}
}; int main()
{
Base B;
Derived D;
Base *pb=&B;
Base *pb1=&D;
/*对于上行转化,dynamic_cast和static_cast是一样的,因为指针pb只会访问基类的内容*/
pb=dynamic_cast<Base *>(&D);
pb=static_cast<Base *>(&D);
//*以下两句话的效果是一样的,因为pb1本身指向的是派生类对象,所以转化后,pd1和pd2的访问不会越界*/
pd1=static_cast<Derived *>(pb1);
pd2=dynamic_cast<Derived *>(pb1);
//以下两句话的效果是不一样的,因为pb本身指向的是基类对象,所以转化后,pd1成功,但是通过pd1可以访问派生类的成员m_szName,这是不安全的
//而dynamic_cast的会使pd2变成0,即不可以访问类的成员
Derived *pd1=static_cast<Derived *>(pb);
Derived *pd2=dynamic_cast<Derived *>(pb);
}
2、链表冒泡排序算法
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(listnode)
struct listnode
{
int data;
struct listnode *next;
}; listnode *Create()
{
listnode *head=(listnode *)malloc(LEN);
head->data=0;
head->next=NULL;
listnode *rear=head;
int x=0;
int flag=1;
while(flag)
{
scanf("%d",&x);
if(x!=0)
{
listnode *s=(listnode *)malloc(LEN);
s->data=x;
rear->next=s;
rear=s;
}
else
flag=0;
}
rear->next=NULL;
return head;
}
void bubblesort(listnode * head)
{
listnode * end, * p , * q;//end用来记录排好序的最后一个元素地址,p,q分别为前驱,后继
int temp;
p=head->next;
q=p->next;
end=NULL;
while(end!=head->next)
//如果head所指结点的next成员为end,循环结束
{
p=head->next;//p结点从链表头结点开始
q=p->next;//q指向p所指结点的下一个结点
while(p->next!=end)
//当p->next的值为end时,表示到链尾
{
if(p->data>q->data)//按照数据域从小到大排序
{
temp=p->data;
p->data=q->data;
q->data=temp;
}
p=q;
q=q->next;
}
end=p;//使end指向每次排序的q所指的结点即尾结点
}
} void print(listnode *head)
{
listnode *p=head->next;
while(p!=NULL){
printf("%d -> ",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
listnode *head=Create();
bubblesort(head);
print(head);
}
3、链表的插入排序(推荐方法)
http://blog.csdn.net/chengtao_xd/article/details/8711451
4、字符串反转的优化
1、交换的地方
2、利用指针比较快
20140903 dynamic_cast和static的区别 链表的冒泡排序和插入排序的更多相关文章
- php self与static的区别
self vs static 用一个demo来直接说明self与static的区别.self示例: <?phpclass Vehicle { protected static $name ...
- 转:C++ 匿名namespace的作用以及它与static的区别
匿名namespace的作用以及它与static的区别 一.匿名namespace的作用在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做为函数名或者全局变量名,则 ...
- C# 总结const、 readonly、 static三者区别:
总结const. readonly. static三者区别: (有人问我,看似简单,我也没能立刻回答出来,总结一下,分享一下.) const:静态常量,也称编译时常量(compile-time con ...
- c++ dynamic_cast 和 static_cast 的区别
今天在看王道宝典的时候看到dynamic_cast ,一直都没用过,也不了解,今天来总结一下. dynamic_cast 和 static_cast 都可以用来强制转换指针类型,但不同的是dynami ...
- PHP中new self()和new static()的区别
1.new static()是在PHP5.3版本中引入的新特性. 2.无论是new static()还是new self(),都是new了一个新的对象. 3.这两个方法new出来的对象有什么区别呢,说 ...
- PHP中new self()和new static()的区别探究
1.new static()是在PHP5.3版本中引入的新特性. 2.无论是new static()还是new self(),都是new了一个新的对象. 3.这两个方法new出来的对象有什么区别呢,说 ...
- Java中主类中定义方法加static和不加static的区别
Java中主类中定义方法加static和不加static的区别(前者可以省略类名直接在主方法调用(类名.方法),后者必须先实例化后用实例调用) 知识点:1.Getter and Setter 的应用 ...
- php面向对象编程self和static的区别
在php的面向对象编程中,总会遇到 class test{ public static function test(){ self::func(); static::func(); } public ...
- C++ 匿名namespace的作用以及与static的区别
匿名namespace的作用以及它与static的区别 一.匿名namespace的作用 在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做 为函数名或者全局变量名 ...
随机推荐
- HBase 中加盐之后的表如何读取:Spark 篇
在 <HBase 中加盐之后的表如何读取:协处理器篇> 文章中介绍了使用协处理器来查询加盐之后的表,本文将介绍第二种方法来实现相同的功能. 我们知道,HBase 为我们提供了 hbase- ...
- MySQL 同一字段匹配多个值
delete from api_log WHERE curl LIKE '%.css' or curl LIKE '%.js' or curl LIKE '%.JPG'; 删除字段curl以 js,c ...
- java并发编程之美-阅读记录5
java并发包中的并发List 5.1CopeOnWriteArrayList 并发包中的并发List只有CopyOnWriteArrayList,该类是一个线程安全的arraylist,对其进行的修 ...
- css 图片有间隔多个Img标签之间的间隙
今天写css时发现,图片加起来刚好是900px的三张图片,不能在一个900px宽容器放下,因为图片之间有间隔,我猜是浏览器把两个img标签之间的空格当成了空白节点. 在网上找到了几个不错的解决方法: ...
- IE的debug工具对程序进行debug跟踪JS代码
2015/8/31 (其他的:显示zjfy_app_sys_ip.html,只需关闭启用保护模式) 显示ie的debug,F12-->Ctrl + P 扩展:第一步,在程序中设置断点,如图所示左 ...
- 卸载 Bash On Ubuntu On Windows
1.打开cmd,输入lxrun /uninstall /full,然后根据提示输入y即可开始卸载. 已失效
- iview table绑定双击事件
<Table <Table ref="table" highlight-row :columns="columns" :data="new ...
- python print 连续输出变量加字符串
a=1 b=2 print(a,'+',b,'=',a+b) 输出:1+2=3
- python2和python3中TestSuite().addTest的区别
Python2中unittest.TestSuite().addTest()的参数是这样的:unittest.TestSuite().addTest(TestFun("test_nam&qu ...
- C++ 浅析移位运算
按位左移(<<): 按二进制形式把所有的数字向左移动对应的位数,高位移出(舍弃),低位的空位补零 按位右移(>>): 按二进制形式把所有的数字向右移动对应位移位数,低位移出(舍 ...