1 《编程语言》
>
1. C++ 准备
- 《Effective C++》适合面试之前突击,罗列了常用问题以及解决方案;
- 《C++ Primer》全面了解,不露死角;
- 《Inside C++ Object Model》深入了解C++对象内部,如对象sizeof, 虚函数调用机制;
- 《The C++ Programming Language》全面深入了解C++.
2. 多维度考察基本功
- 以复制运算符函数为例:
- 返回值是否为该类型的引用;
- 是否传入参数的类型声明为常量引用;
- 是否判传入的参数和当前的实例(*this)是不是同一个实例,如果是同一个是不能直接进行赋值操作,应该直接返回;
- 是否考虑异常安全:赋值运算符函数内部抛出异常,不能破坏原本实例的状态;
2《数据结构》
链表和树是面试中出现频率最高的数据结构,由于操作链表和指针需要大量的指针,代码鲁棒性是需要特别注意的,否则容易出现程序崩溃的问题。栈是一个与递归密切相关的数据结构,队列也与广度优先遍历算法紧密相关。
a. 数组:占据一块连续的内存并按照顺序存储数据。创建数组时,要首先制定数组的容量大小,然后根据容量大小分配内存。即使只在数组中存储一个数字,也需要为所有数据预先分配内存。连续性可以根据下标在O(1)时间去读/写,因此效率非常高,也可以实现简单的哈希表。动态数组如C++ STL中的vector,
b, 字符串:C/C++的字符串以’\0‘作为结尾。为了节省内存,C/C++把常量字符串放到单独的一个内存区域。当几个指针赋值给相同的常量字符串时,他们实际上指向相同的内存地址,但用常量字符串初始化数组时,情况却不一样,会重新分配地址,类似copy。
c. 链表:《未完待续。。。》
《算例分析》
/*<Case 1: Check the result of the program after running>
//Analyze the result of below code
class A
{
private:
{
int value;
}
public:
{
A(int n){ value = n;}
//**传参为A的一个实例,由于是传值参数,实际就是把形参复制到实参,
//从而调用复制构造函数,进而形成永无止境的递归调用复制构造函数
//造成栈溢出。C++不允许复制构造函数传值参数。
A(A other){value = A.value;}
void Print(){std::cout<<value<<std::endl;}
}
int _tmain(int argc, _TCHAR* argv[])
{
A a = 10;
A b = a;//here will crash
b.Print();
return 0;
}
/* Case 2: Design a good Singleton mode */
//Singleton mode
public sealed Class Singleton1
{
//define as private to protect from other instances
private Singletion1()
{
}
private static Singleton1 instance = null;
public static Singleton1 Instance()
{
get
{
if(instance == null)
{
instance = new Singleton1();
}
return instance;
}
public sealed Class Singleton2
{
private Singleton2()
{
}
private static Singleton2 instance = null;
private static readonly object synobj = new object();
public static Singleton2 Instance()
{
get
{
lock(synobj)
{
if(instance == null)
{
instance = new Singleton2();
}
}
return instance;
}
}
}
public sealed class Singleton3
{
private Singleton3()
{
}
private static object syncobj = new object();
private static Singleton3 instance = null;
public static Singleton3 Instance
{
get
{
if(instance == null)
{
lock(syncobj)
{
if(instance == null)
{
instance = new Singleton3();
}
}
}
return instance;
}
}
}
public sealed class Singleton4
{
private Singleton4()
{
}
private static Singleton4 instance = new Singleton4();
public static Singleton4 Instance
{
get
{
return instance;
}
}
/*Case 3: Search whether the number is existed in the 2D matrix*/
bool Find(int* matrix, int rows, int columns, int numbder)
{
if(matrix == null || rows ==0 || columns == 0)
{
return false;
}
bool Found = false;
int row = 0;
int column = columns -1;
while(row < rows && column > = 0)
{
int aValue = matrix[row*columns + column];
if(aValue == number)
{
found = true;
break;
}
if(aValue < number)
{
--column;
}
else
{
++row;
}
};
return found;
}