好文档 - 专业文书写作范文服务资料分享网站

C++面试宝典2012版

天下 分享 时间: 加入收藏 我要投稿 点赞

head = head2 ;

head->next = MergeRecursive(head1,head2->next); }

return head ; ----------

41. 分析一下这段程序的输出 (Autodesk) class B { public: B() {

cout<<\} ~B() {

cout<<\}

B(int i):data(i) //B(int) works as a converter ( int -> instance of B) {

cout<<\} private: int data; };

B Play( B b) { return b ; }

(1) results:

int main(int argc, char* argv[]) constructed by parameter 5 { destructed B(5)形参析构

B t1 = Play(5); B t2 = Play(t1); destructed t1形参析构

return 0; destructed t2 注意顺序! } destructed t1 (2) results:

int main(int argc, char* argv[]) constructed by parameter 5 { destructed B(5)形参析构

B t1 = Play(5); B t2 = Play(10); constructed by parameter 10 return 0; destructed B(10)形参析构 } destructed t2 注意顺序! destructed t1

43.写一个函数找出一个整数数组中,二大的数 (microsoft)

答案:

const int MINNUMBER = -32767 ; int find_sec_max( int data[] , int count) {

int maxnumber = data[0] ; int sec_max = MINNUMBER ; for ( int i = 1 ; i < count ; i++) {

if ( data[i] > maxnumber ) {

sec_max = maxnumber ; maxnumber = data[i] ; } else {

if ( data[i] > sec_max ) sec_max = data[i] ; } }

第return sec_max ; }

44.写一个在一个字符串(n)中寻找一个子串(m)第一个位置的函数。

KMP算法效率最好,时间复杂度是O(n+m),

46.多重继承的内存分配问题:

比如有class A : public class B, public class C {} 那么A的内存结构大致是怎么样的? 这个是compiler-dependent的, 不同的实现其细节可能不同。如果不考虑有虚函数、虚继承的话就相当简单;否则的话,相当复杂。可以参考《深入探索C++对象模型

47.如何判断一个单链表是有环的?(注意不能用标志位,最多只能用两个额外指针)

struct node { char val; node* next;}

bool check(const node* head) {} //return false : 无环;true: 有环一种O(n)的办法就是(搞两个指针,一个每次递增一步,一个每次递增两步,如果有环的话两者必然重合,反之亦然):

bool check(const node* head) {

if(head==NULL) return false; node *low=head, *fast=head->next; while(fast!=NULL && fast->next!=NULL) {

low=low->next; fast=fast->next->next; if(low==fast) return true; }

return false; }

48.指针找错题

分析这些面试题,本身包含很强的趣味性;而作为一名研发人员,通过对这些面试题的深入剖析则可进一步增强自身的内功。 2.找错题 试题1: 以下是引用片段: void test1() //数组越界 {

char string[10];

char* str1 = \ strcpy( string, str1 ); }

试题2: 以下是引用片段: void test2() {

char string[10], str1[10]; int i;

for(i=0; i<10; i++) { str1= 'a'; }

strcpy( string, str1 ); }

试题3: 以下是引用片段: void test3(char* str1) {

char string[10]; if( strlen( str1 ) <= 10 ) {

strcpy( string, str1 ); } } 解答:

试题1字符串str1需要11个字节才能存放下(包括末尾的‘\\0‘),而string只有10个字节的空间,strcpy会导致数组越界;对试题2,如果面试者指出字符数组str1不能在数组内结束可以给3分;如果面试者指出strcpy(string,str1)调用使得从 str1内存起复制到string内存起所复制的字节数具有不确定性可以给7分,在此基础上指出库函数strcpy工作方式的给10分;

对试题3,if(strlen(str1) <= 10)应改为if(strlen(str1) <10),因为strlen的结果未统计‘\\0‘所占用的1个字节。剖析:考查对基本功的掌握 (1)字符串以‘\\0‘结尾; (2)对数组越界把握的敏感度; (3)库函数strcpy的工作方式,

49.如果编写一个标准strcpy函数

总分值为10,下面给出几个不同得分的答案:2分 以下是引用片段: void strcpy( char *strDest, char *strSrc ) {

while( (*strDest++ = * strSrc++) != ?\\0‘ ); }

4分 以下是引用片段:

void strcpy( char *strDest, const char *strSrc ) //将源字符串加const,表明其为输入参数,加2分 {

while( (*strDest++ = * strSrc++) != ?\\0‘ ); }

7分 以下是引用片段:

void strcpy(char *strDest, const char *strSrc) {

//对源地址和目的地址加非0断言,加3分 assert( (strDest != NULL) &&(strSrc != NULL) ); while( (*strDest++ = * strSrc++) != ?\\0‘ ); }

10分 以下是引用片段:

//为了实现链式操作,将目的地址返回,加3分! char * strcpy( char *strDest, const char *strSrc ) {

C++面试宝典2012版

head=head2;head->next=MergeRecursive(head1,head2->next);}returnhead;----------41.分析一下这段程序的输出(Autodesk)classB{public:B(){cout<<\}~B(){cout<
推荐度:
点击下载文档文档为doc格式
1eqhz0wwyj4c2db0065w
领取福利

微信扫码领取福利

微信扫码分享