有点意思的C/C++问题及解答:11-15

问题11:下面这个函数希望完成什么任务?

  1. int func(int x)   
  2. {   
  3.     int countx = 0;   
  4.     while(x)   
  5.     {   
  6.         countx ++;   
  7.         x = x&(x-1);   
  8.     }   
  9.     return countx;   
  10. }   

解答:这个函数是求一个整数的二进制表示中含1的个数。假定x = 9999,该函数返回8。这是道微软的面试题,在《编程之美》一书中给出了若干种方法,用来求二进制数中1的个数。

问题12:以下三条输出语句分别输出什么?

  1. char str1[] = "abc";   
  2. char str2[] = "abc";   
  3. const char str3[] = "abc";   
  4. const char str4[] = "abc";   
  5. const char* str5 = "abc";   
  6. const char* str6 = "abc";   
  7. cout << boolalpha << ( str1==str2 ) << endl; // 输出什么  
  8. cout << boolalpha << ( str3==str4 ) << endl; // 输出什么  
  9. cout << boolalpha << ( str5==str6 ) << endl; // 输出什么   
解答:输出分别为false false true。str1 和str2 都是字符数组,每个都有其自己的存储区,它们的值则是各 存储区首地址,不等;str3 和str4 同上,只是按const 语义,它们所指向的数据区不能修改。str5 和str6 并非数组而是字符指针,并不分配存储区,其后的“abc”以常量形式存于静态数据区,而它们自己仅是指向该区首地址的指针,相等。 如果去掉定义str5、str6时加的const修饰,输出还是相等的。

问题13: 已知String类定义如下,请实现这些函数。

  1. class String   
  2. {   
  3. public:   
  4.     String(const char *str = NULL); //默认构造函数   
  5.     String(const String &another);  //拷贝构造函数   
  6.     ~String();                      //析构函数   
  7.     String & operator =(const String &rhs); //赋值操作符   
  8. private:   
  9.     char *m_data;                   //用于保存字符串   
  10. };   

解答:实现中有几点要注意:(1)默认构造函数中,判断str是否为空。(2)析构函数中应使用delete [ ] 运算符。(3)赋值操作符应判断是否是给自身赋值。代码如下:

  1. String::String(const char *str)  
  2. {  
  3.     if(str == NULL)  
  4.     {  
  5.         m_data = new char[1];  
  6.         m_data[0] = '\0';  
  7.     }  
  8.     else  
  9.     {  
  10.         m_data = new char[strlen(str) + 1]; //需要加1,strlen返回的字符串长度并不包含'\0'  
  11.         strcpy(m_data,str);  
  12.     }  
  13. }  
  14. String::String(const String &another)  
  15. {  
  16.     m_data = new char[strlen(another.m_data) + 1];  
  17.     strcpy(m_data,another.m_data);  
  18. }  
  19. String& String::operator=(const String &rhs)  
  20. {  
  21.     if(this != &rhs) //防止给自身赋值  
  22.     {  
  23.         delete [] m_data;  
  24.         m_data = new char[strlen(rhs.m_data) + 1];  
  25.         strcpy(m_data,rhs.m_data);  
  26.     }  
  27.     return *this;  
  28. }  
  29. String::~String()  
  30. {  
  31.     delete [] m_data; //注意是delete []  
  32. }  
问题14:写一个在一个字符串(T)中寻找一个子串(P)第一个位置的函数。

解答:用KMP算法即可。基本策略是预处理子串 P ,获得其中与模式匹配有关的子字符串关系规律,从而当发生匹配失败时,可以确定继续与 T 当前位置匹配的 P 的新位置,这样就不需要在 T 中回溯了。时间复杂度为O(T+P)。

  1. int KMP(char *T, char *P)  
  2. {  
  3.     int lenT = strlen(T);  
  4.     int lenP = strlen(P);  
  5.     int i = 0, j = 0;  
  6.     int *next = new int[lenP + 1];  //最后一个元素用不到  
  7.     Fail(next, P);                  //计算失败函数  
  8.     while(i < lenT && j < lenP)  
  9.     {  
  10.         if(j == -1 || T[i] == P[j])  
  11.         {  
  12.             i++; j++;  
  13.         }  
  14.         else  
  15.             j = next[j];   //P的下一个比较位置  
  16.     }  
  17.     delete [] next;  
  18.     if(j < lenP || j == 0)  
  19.         return -1;  
  20.     else   
  21.         return i - lenP;  
  22. }  
  23.   
  24. //仍是一个模式匹配的过程,只不过目标串和模式串是同一串P,所以两个代码的程序很相似  
  25. void Fail(int *next, char *P)  
  26. {  
  27.     int lenP = strlen(P);  
  28.     int i = -1,j = 0;  
  29.     next[0] = -1;  
  30.     while(j < lenP)  
  31.     {  
  32.         if(i == -1 || P[i] == P[j])  
  33.         {  
  34.             i++; j++;  
  35.             next[j] = i;  
  36.         }  
  37.         else  
  38.             i = next[i];  
  39.     }  
  40. }  
问题15:为什么一个空类的sizeof为1而不是0?

解答:空类同样可以被实例化,每个实例在内存中都有一个独一无二的地址,为了达到这个目的,编译器往往会给一个空类隐含的加一个字节,这样空类在实例化后在内存得到了独一无二的地址。

猜你喜欢

转载自blog.csdn.net/nk_wang/article/details/44066837