C++面试题(二)

来源:互联网 发布:视频监控客户端软件 编辑:程序博客网 时间:2024/04/30 00:26

string函数 
编写标准库函数strcpy

char *strcpy(char *strDest, const char *strSrc);//strDest为目标,strSrc为源{     assert((strDest!=NULL) && (strSrc !=NULL));      //如果两个为空则不用复制,直接中止     char *address = strDest;       //用address指向strDest开始地址     while( (*strDest++ = * strSrc++) != ‘\0’ ) //复制,直到源串结束;        NULL ; //空操作     return address ;    //返回strDest开始地址                            }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

编写标准库函数strlen

int strlen( const char *str ) //输入参数const  {   assert( strt != NULL ); //断言字符串地址非0   int len;   while( (*str++) != ' 0' )   {    len++;   }   return len;  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

已知String类定义如下

class String  {  public:  String(const char *str = NULL); // 通用构造函数  String(const String &another); // 拷贝构造函数  ~ String(); // 析构函数  String & operater =(const String &rhs); // 赋值函数  private:  char *m_data; // 用于保存字符串  }; 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
//普通构造函数  String::String(const char *str)   {      if(str==NULL)       {          m_data = new char[1]; // 得分点:对空字符串自动申请存放结束标志'\0'的空    //加分点:对m_data加NULL 判断    *m_data = '\0';       }    else   {    int length = strlen(str);     m_data = new char[length+1]; // 若能加 NULL 判断则更好     strcpy(m_data, str);    }  }  // String的析构函数  String::~String(void)   {   delete [] m_data; // 或delete m_data;  }  //拷贝构造函数  String::String(const String &other)    // 得分点:输入参数为const型  {    int length = strlen(other.m_data);    m_data = new char[length+1];     //加分点:对m_data加NULL 判断   strcpy(m_data, other.m_data);   }  //赋值函数  String & String::operate =(const String &other) // 得分点:输入参数为const型  {    if(this == &other)   //得分点:检查自赋值    return *this;    delete [] m_data;     //得分点:释放原有的内存资源   int length = strlen( other.m_data );    m_data = new char[length+1];  //加分点:对m_data加NULL 判断   strcpy( m_data, other.m_data );    return *this;         //得分点:返回本对象的引用  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

写一个函数,完成内存之间的拷贝

// 功能:由src所指内存区域复制count个字节到dest所指内存区域。  // 说明:src和dest所指内存区域可以重叠,但复制后dest内容会被更改。函数返回指向dest的指针  void *memmove(void *dest , const void *src , size_t count)  {      assert( (dest != NULL) && (src != NULL));     //安全检查      assert( count > 0 );      char *psrc = (char *) src;      char *pdest = (char *) dest;      //检查是否有重叠问题      if( pdest < psrc )      {          //正向拷贝          while( count-- )              *pdest++ = *psrc++;      }      else if( psrc < pdest )      {          //反向拷贝          psrc = psrc + count - 1;          pdest = pdest + count - 1;          while( count-- )              *pdest-- = *psrc--;      }      return dest;  }  // 功能:由src指向地址为起始地址的连续n个字节的数据复制到以dest指向地址为起始地址的空间内。  // 说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针  void *memmcpy(void *dest , const void *src , size_t count)  {      assert( (dest != NULL) && (src != NULL));     //安全检查      assert( count > 0 );      char *psrc = (char *) src;      char *pdest = (char *) dest;      while( count-- )          *pdest++ = *psrc++;      return dest;  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

什么是拷贝构造函数? 
  它是单个参数的构造函数,其参数是与它同属一类的对象的(常)引用;类定义中,如果未提供自己的拷贝构造函数,C++提供一个默认拷贝构造函数,该默认拷贝构造函数完成一个成员到一个成员的拷贝

什么是深浅拷贝? 
  浅拷贝是创建了一个对象用一个现成的对象初始化它的时候只是复制了成员(简单赋值)而没有拷贝分配给成员的资源(如给其指针变量成员分配了动态内存); 深拷贝是当一个对象创建时,如果分配了资源,就需要定义自己的拷贝构造函数,使之不但拷贝成员也拷贝分配给它的资源。

0 0
原创粉丝点击