1-历次面试题汇总

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

基础题

1、static能否与const或virtual共同修饰函数

static不能与const一起修饰函数,C++编译器在实现const的成员函数的时候为了确保该函数不能修改类的实例的状态,会在函数中添加一个隐式的参数const this*。但当一个成员为static的时候,该函数是没有this指针的。也就是说此时const的用法和static是冲突的。

static也不能与virtual一起修饰函数,Virtual functions are invoked when you have a pointer/reference to an instance of a class. Static functions aren't tied to a particular instance, they're tied to a class. C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.

注意:

用const修饰函数,是说该函数不能改变成员的值,因此者这个函数的this指针是const的,但是如果我用static修饰的话,这个时候这个函数是没有this指针的,因此两个不能同时修饰函数。

2、桶排序

大致思路是将数据进行划分,在各自划分的桶中做排序,然后再按照桶号从小到大输出。

#include

#include

#include

usingnamespace std;

void bucket_sort(vector&num)

{

int mini = INT_MAX, maxi = INT_MIN;

for(int i=0; i

{

if(num[i] < mini)

mini = num[i];

if(num[i] > maxi)

maxi = num[i];

}

int nBasket = 5;

int space = (maxi-mini+1)/nBasket;

vector *basket = new vector[nBasket];

for(int i=0; i

{

int k = (num[i]-mini)/space;

basket[k].push_back(num[i]);

}

int pos = 0;

for(int i=0; i

{

sort(basket[i].begin(), basket[i].end());

for(int j=0; j

num[pos++] = basket[i][j];

}

delete[] basket;

}

int main()

{

vector num;

for(int i=0; i<100; i++)

num.push_back(100-i);

bucket_sort(num);

for(int i=0; i

cout << num[i] <<" ";

}

3、内存只有1G,是否可以new 4G内存空间

Memory pages aren't actually mapped to your program until you use them. All malloc does is reserve a range of the virtual address space. No physical RAM is mapped to those virtual pages until you try to read or write them.

Even when you allocate global or stack ("automatic") memory, there's no mapping of physical pages until you touch them.

Finally, sizeof() is evaluated at compile time, when the compiler has no idea what the OS will do later. So it will just tell you the expected size of the object.

You'll find that things will behave very differently if you try to memset the memory to 0 in each of your cases. Also, you might want to try calloc, which zeroes its memory.

32位意味着4G的寻址空间,linux把它分为两部分:最高的1G(虚拟地址从0xC0000000到0xffffffff)用做内核本身,成为“系统空间”,而较低的3G字节(从0x00000000到0xbffffff)用作各进程的“用户空间”。这样,理论上每个进程可以使用的用户空间都是3G。当然,实际的空间大小收到物理存储器大小的限制。虽然各个进程拥有其自己的3G用户空间,系统空间却由所有的进程共享。从具体进程的角度看,则每个进程都拥有4G的虚拟空间,较低的3G为自己的用户空间,最高的1G 为所有进程以及内核共享的系统空间。

可是经自己测试:

相关文档
最新文档