数据结构

数据结构

在8086汇编下,逻辑地址和物理地址是怎么样转换的?

答案:通用寄存器给出的地址,是段内偏移地址,相应段寄存器地址*10H + 通用寄存器内地址,就得到了真正要访问的地址

比较c++中4种类型转换方式

一、C 风格(C-style)强制转型如下:

(T) expression // cast expression to be of type T 
函数风格(Function-style)强制转型使用这样的语法:
T(expression) // cast expression to be of type T 
这两种形式之间没有本质上的不同,它纯粹就是一个把括号放在哪的问题。我把这两种形式称为旧风格(old-style)的强制转型。 

二、 C++的四种强制转型形式:

  C++ 同时提供了四种新的强制转型形式(通常称为新风格的或 C++ 风格的强制转型):
  const_cast(expression)
  dynamic_cast(expression)
  reinterpret_cast(expression)
  static_cast(expression)

  每一种适用于特定的目的:

·dynamic_cast 主要用于执行“安全的向下转型(safe downcasting)”,也就是说,要确定一个对象是否是一个继承体系中的一个特定类型。它是唯一不能用旧风格语法执行的强制转型,也是唯一可能有重大运行时代价的强制转型。

·static_cast 可以被用于强制隐型转换(例如,non-const 对象转型为 const 对象,int 转型为 double,等等),它还可以用于很多这样的转换的反向转换(例如,void* 指针转型为有类型指针,基类指针转型为派生类指针),但是它不能将一个 const 对象转型为 non-const 对象(只有 const_cast 能做到),它最接近于C-style的转换。

·const_cast 一般用于强制消除对象的常量性。它是唯一能做到这一点的 C++ 风格的强制转型。

·reinterpret_cast 是特意用于底层的强制转型,导致实现依赖(implementation-dependent)(就是说,不可移植)的结果,例如,将一个指针转型为一个整数。这样的强制转型在底层代码以外应该极为罕见。
  
旧风格的强制转型依然合法,但是新的形式更可取。首先,在代码中它们更容易识别(无论是人还是像 grep 这样的工具都是如此),这样就简化了在代码中寻找类型系统被破坏的地方的过程。第二,更精确地指定每一个强制转型的目的,使得编译器诊断使用错误成为可能。例如,如果你试图使用一个 const_cast 以外的新风格强制转型来消除常量性,你的代码将无法编译。

dynamic_cast .vs. static_cast

class B { ... };
class D : public B { ... };

void f(B* pb)
{
   D* pd1 = dynamic_cast<D*>(pb);
   D* pd2 = static_cast<D*>(pb);
}

If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0. 

If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.

即dynamic_cast可用于继承体系中的向下转型,即将基类指针转换为派生类指针,比static_cast更严格更安全。dynamic_cast在执行效率上比static_cast要差一些,但static_cast在更宽上范围内可以完成映射,这种不加限制的映射伴随着不安全性.static_cast覆盖的变换类型除类层次的静态导航以外,还包括无映射变换,窄化变换(这种变换会导致对象切片,丢失信息),用VOID*的强制变换,隐式类型变换等

static_cast .vs. reinterpret_cast

reinterpret_cast是为了映射到一个完全不同类型的意思,这个关键词在我们需要把类型映射回原有类型时用到它.我们映射到的类型仅仅是为了故弄玄虚和其他目的,这是所有映射中最危险的.(这句话是C++编程思想中的原话)

static_cast 和 reinterpret_cast 操作符修改了操作数类型. 它们不是互逆的; static_cast 在编译时使用类型信息执行转换, 在转换执行必要的检测(诸如指针越界计算, 类型检查). 其操作数相对是安全的. 另一方面, reinterpret_cast 仅仅是重新解释了给出的对象的比特模型而没有进行二进制转换, 例子如下:

int n=9; double d=static_cast < double > (n); 

上面的例子中, 我们将一个变量从 int 转换到 double. 这些类型的二进制表达式是不同的. 要将整数 9 转换到 双精度整数 9, static_cast 需要正确地为双精度整数 d 补足比特位. 其结果为 9.0. 而reinterpret_cast 的行为却不同: 

int n=9;
double d=reinterpret_cast<double & > (n); 

这次, 结果有所不同. 在进行计算以后, d 包含无用值. 这是因为 reinterpret_cast 仅仅是复制 n 的比特位到 d, 没有进行必要的分析.

总结:
一共四种cast。1、static_cast,支持子类指针到父类指针的转换,并根据实际情况调整指针的值,反过来也支持,但会给出编译警告,它作用最类似C风格的“强制转换”,一般来说可认为它是安全的;2、dynamic_cast,支持父类指针到子类指针的转换,并根据实际情况调整指针的值,和static_cast不同,反过来它就不支持了,会导致编译错误,这种转换是最安全的转换;3、reinterpret_cast,支持任何转换,但仅仅是如它的名字所描述的那样“重解释”而已,不会对指针的值进行任何调整,用它完全可以做到“指鹿为马”,但很明显,它是最不安全的转换,使用它的时候,你得头脑清醒,知道自己在干什么;4、const_cast,这个转换能剥离一个对象的const属性,也就是说允许你对常量进行修改。

链表的插入、追加、删除操作

在链表类中
void insert(int n,int value)
{
}

c++ 二叉树的前序、中序和后序遍历[递归和非递归版本]

//想一想
//顺序表(array)主要操作为初始化,查询表长, 插入一个数据(给定数据和位置,如何实现),追加一个数据,删除一个数据(给定位置,如何实现),查找节点
//链表结构

注意template类!!!可以存放任何类的模板类;

如何打印当前源文件的文件名以及源文件的当前行号?(

宏__FILE__,__LINE__)

const与define的区别

const的应用场景:定义常量、修饰函数参数和修饰函数返回值三个作用
被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。

1)const常量有数据类型,而宏常量没有。编译器可以对前者进行类型检查,对后者只进行字符替换,没有类型安全检查
,并在字符替换时可能会产生意料不到的错误。

2)有些集成化的调试工具可以对const常量进行调试,但是不能对宏常量进行调试

简述数组和指针的区别?

数据要么在静态存储区被创建,要么在栈上被创建,指针可以随时指向任意类型的内存块。

1)修改内容上的差别

#include <iostream>
using namespace std;
int main()
{
    char *p = "hello";
    char q[] = "world";
    q[1] = '2';
    cout<<q<<endl;
    cout<<p<<endl;
    p[1] = 'c';
    cout<<p<<endl;
    return 0;   
}

编译器会有一个waring,但是并没有报错,在动态运行的时候发现有错误。因为hello是常量,不能对常量进行修改;

2)用运算符sizeof可以计算出数组的容量,sizeof指针,得到的是一个指针变量的字节数,而不是p所指向的内存变量。
注意当数组当做函数的参数传递时,该数组自动退化为同类型的指针。
指针是4个字节

类成员函数的重载、覆盖和隐藏区别?

(重载和重写有什么区别?)
重写就是覆盖,override,重载是overload,知道隐藏有什么用呢????》》》》

a 成员函数被重载的特征:1——相同的范围(一个类中);(2)函数名字相同;(3)参数不同;(4)virtual关键字可有可无
b 覆盖是指派生类函数覆盖基类函数,特征是:1——不同的范围(分别位于派生类和基类) 2)函数签名相同 3——基类函数必须有virtual关键字
c 隐藏是指派生类的函数屏蔽了与其同名的基类函数。

c的规则如下:1——如果派生类的函数与基类的函数同名,但是参数不同。此时,无论不管有无virtual关键字,基类的函数将被隐藏(注意别和重载混淆)2——
如果派生类的函数与基类相同,而且参数相同,但是基类函数没有virtual关键字,此时,基类的函数被隐藏。(注意别和覆盖混淆)

main主函数执行完毕后,是否可能再执行一段代码?举例说明

可以,用_onexit()并没有看到效果啊!!!

#include <stdlib.h>

//功能:Processes the specified function at exit.
//格式:int atexit( void ( __cdecl *func )( void ) );
//描述:The atexit function is passed the address of a function (func) to be
// called when the program terminates normally. Successive calls to atexit 
//create a register of functions that are executed in LIFO (last-in-first-out) 
//order. The functions passed to atexit cannot take parameters. atexit and 
//_onexit use the heap to hold the register of functions. Thus, the number of
// functions that can be registered is limited only by heap memory.
//int atexit (void (*function)(void));


#include <stdio.h>

void fun1(void);

void fun2(void);

void fun3(void);

void fun4(void);

int main()
{

    atexit(fun1);

    atexit(fun2);

    atexit(fun3);

    atexit(fun4);

    printf("this is executed first.\n");
    return 0;
}

void fun1() 
{
    printf("next.\n");
}

void fun2()
{    
    printf("executed ");    
}

void fun3()
{    
    printf("is ");    
}

void fun4()
{    
    printf("this ");    
}


看到没,fn1()、fn2()、fn3()、fn4()就是在main()函数运行结束后调用的。

这里最关键的是使用了 atexit() 函数,讲 atexit() 函数之前先讲一下 exit() 函数。

exit()函数用于在程序运行的过程中随时结束程序,其原型为:
    void exit(int state);
exit的参数state是返回给操作系统或当前程序的调用程序,返回0表示程序正常结束,非0表示程序非正常结束。main函数结束时也会隐式地调用exit()函数。exit()函数运行时首先会执行由atexit()函数登记的函数,然后会做一些自身的清理工作,同时刷新所有输出流、关闭所有打开的流并且关闭通过标准I/O函数tmpfile()创建的临时文件。 

atexit() 用于注册终止函数(即main执行结束后调用的函数),其原型为:
    int atexit(void (*function)(void));
很多时候我们需要在程序退出的时候做一些诸如释放资源的操作,但程序退出的方式有很多种,比如main()函数运行结束、在程序的某个地方用exit()结束程序、用户通过Ctrl+C或Ctrl+break操作来终止程序等等,因此需要有一种与程序退出方式无关的方法来进行程序退出时的必要处理。方法就是用atexit()函数来注册程序正常终止时要被调用的函数。

atexit()函数的参数是一个函数指针,函数指针指向一个没有参数也没有返回值的函数。

在一个程序中最多可以用atexit()注册32个处理函数,这些处理函数的调用顺序与其注册的顺序相反,也即最先注册的最后调用,最后注册的最先调用。同一个函数如若登记多次,则也会被调用多次。

正如上面程序中所示,这些函数都是在main结束以后才被调用的。atexit()只是注册他们,使得他们在main结束以后被调用,看名字就可以看出来。

http://c.biancheng.net/cpp/html/2804.html

如何判断一段程序是由C编译程序还是C++编译的?

#include <iostream>
using namespace std;

int main()
{
    #ifdef __cplusplus
    cout<<"C++"<<endl;
    #else
    cout<<"C"<<endl;
    #endif
    return 0;   
}

据说,上次那个问题,eof文件,最终最后一个数会读两次?

http://blog.youkuaiyun.com/ixidof/article/details/4782486
http://www.cnblogs.com/wanghao111/archive/2009/05/27/1491138.html

从文件中读入数据,然后排序,写入另外一个文件中去;

数据结构

链表题

单链表,给出一个链表的头结点head,写出一个函数把这个链表逆序,空间复杂度为o(1),时间复杂度为on

#include <iostream>
using namespace std;
class Node{
    public:
        int data;
        Node *next;
        Node(int d)
        {
            data = d;
            next = NULL;    
        }
};
Node * reverse(Node *head)
{
    if(head == NULL || head->next == NULL)
        return head;
    Node *p1 = head;
    Node *p2 = head->next;
    Node *p3 = p2->next;
    p1->next = NULL;
    while(p3 != NULL)
    {
        p2->next = p1;
        p1 = p2;
        p2 = p3;
        p3 = p2->next;
    }
    p2->next = p1;
    head = p2;
    return head; 
}
int main()
{
    #ifdef __cplusplus
    cout<<"C++"<<endl;
    #else
    cout<<"C"<<endl;
    #endif
    Node *head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);
    head->next->next->next->next->next = new Node(6);
    head = reverse(head);
    while(head != NULL)
    {
        cout<<head->data<<endl;
        head = head->next;
    }
    return 0;   
}

已知两个链表head1和head2各自有序,请把它们合并为一个链表依然有序。(保留所有结点,即便大小相同);

#include <iostream>
using namespace std;
class Node{
    public:
        int data;
        Node *next;
        Node(int d)
        {
            data = d;
            next = NULL;    
        }
};
Node * reverse(Node *head)
{
    if(head == NULL || head->next == NULL)
        return head;
    Node *p1 = head;
    Node *p2 = head->next;
    Node *p3 = p2->next;
    p1->next = NULL;
    while(p3 != NULL)
    {
        p2->next = p1;
        p1 = p2;
        p2 = p3;
        p3 = p2->next;
    }
    p2->next = p1;
    head = p2;
    return head; 
}
Node *merge(Node *head, Node * head2)
{
    if(head == NULL)
        return head2;
    else if (head2 == NULL)
        return head;
    Node *head3 = new Node(0);
    Node *tempHead = head3;
    while (head != NULL && head2 != NULL)
    {
        if(head->data <= head2->data)
        {
            head3->next = new Node(head->data);
            head = head->next;
            head3 = head3->next;
        }
        else
        {
            head3->next = new Node(head2->data);
            head2 = head2->next;
            head3 = head3->next;
        }
    }
    while(head != NULL)
    {
        head3->next = new Node(head->data);
        head = head->next;
        head3 = head3->next;
    }
    while(head2 != NULL)
    {
        head3->next = new Node(head2->data);
        head2 = head2->next;
        head3 = head3->next;
    }
    head3 = tempHead->next;
    return head3;

}
int main()
{
    #ifdef __cplusplus
    cout<<"C++"<<endl;
    #else
    cout<<"C"<<endl;
    #endif
    Node *head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);
    head->next->next->next->next->next = new Node(6);


    Node *head2 = new Node(1);
    head2->next = new Node(2);
    head2->next->next = new Node(3);
    head2->next->next->next = new Node(4);
    head2->next->next->next->next = new Node(5);
    head2->next->next->next->next->next = new Node(6);
    //head2 = reverse(head2);

    Node *head3 = merge(head,head2); //写出空间复杂度为1的程序 ,这里的空间复杂度为on 

    while(head3 != NULL)
    {
        cout<<head3->data<<endl;
        head3 = head3->next;
    }
    return 0;   
}

写出空间复杂度为1的程序…

写一个在一个字符串(长度为n)中寻找一个子串(长度为m)出现的第一个位置的函数;KMP算法

多重继承的内存分配问题

class A: public B, public C
如果该类中没有虚函数和虚继承,则简单一点,如果有呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值