careercup-C和C++ 13.8】的更多相关文章

17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other nodes. The data structure BiNode could be used to represent both a binary tree (where nodel is the left node and node2 is the right node) or a doubly link…
18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangle of letters such that every row forms a word (reading left to right) and every column forms a word (reading top to bottom). The words need not be cho…
13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最直接的方法是先读入所有的数据,统计文本的总行数,然后再遍历一遍打印出最后K行.这个方法需要读两遍文件,我们想使用一种更简便的方法,只需要读取一遍文本就可以打印出最后K行,这里我们使用一个循环数组Circular Array,原理是我们维护一个大小为K的字符串数组,当数组存满后,新进来的数据从开头开始…
13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the number of inputs is small, which data structure options can be used instead of a hash table? 这道题让我们比较哈希表和STL中的map数据结构,在遇到这道题之前,我一直以为map就是c++中的哈希表呢,原来是不同的啊-…
13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table.当类中定义了虚函数时,一个虚表格就建立了用来保存该类的虚函数的地址.此时编译器Compiler也会在该类中增加一个虚指针vptr(Virtual Pointer),用来指向虚表格.当一个虚函数在派生类中没有被重写时,派生类中的虚表格中仍然存的是基类的虚函数的地址.当虚函数被调用时,就要到虚表格中取…
13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问深拷贝和浅拷贝的区别.浅拷贝复制对象中所有的成员值到另一个对象中,而深拷贝不仅复制这些,还复制所有的指针对象,参见下面代码: struct Test { char *ptr; }; void shallow_copy(Test &src, Test &dest) { dest.ptr = sr…
13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义,volatile有易变的易挥发的意思,在C/C++里,表示告知编译器某个变量可能会由程序外部改变,比如操作系统,硬件或者其他的线程.由于变量会由无法预期的改变,所有编译器每次都需要从内存中读取变量值.我们可以如下定义一个整型变量为volatile: int volatile x; volatile int x…
13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面这段代码: class Foo { public: void f(); }; class Bar: public Foo { public: void f(); }; Foo *p = new Bar(); p->f(); 调用p->f()会调用基类中的f(),这是因为f()不是虚函数.为了调用派…
13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete copy of the passed in data structure. The Node data structure contains two pointers to other Nodes. 在这道题让我们通过一个节点指针来复制整个数据结构,节点类Node中包含两个节点指针,我们需要用哈希表来…
13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates, that simulates a pointer while also providing automatic garbage collection. It automatically counts the number of references to a SmartPointer<T*>…