string类的基本实现

https://blog.csdn.net/qq_29503203/article/details/52265829

在面试中面试官常常会让你写出string类的基本操作,比如:构造函数,析构函数,拷贝构造等等.下面是除此之外的一些操作,希望可以帮助你更好的理解string以便以后的运用:

  1. String& operator=(const String& s);
  2. char* c_str();
  3. char& operator[](int index);
  4. void PushBack(char c);
  5. String operator+(const String& s);
  6. String& operator+=(const String& s);
  7. String& insert(int pos,const char* str);//在指定位置插入字符串
  8. bool operator==(const String& s);



为了读者方便,下面给出完整代码:

  1. #include<iostream>
  2. using namespace std;
  3. #include<cstring>
  4. #include<assert.h>
  5. class String
  6. {
  7. friend ostream& operator<<(ostream& os,const String& s);
  8. friend istream& operator>>(istream& is,String& s);
  9. public:
  10. String(const char* str=""):_sz(strlen(str))
  11. ,_capacity(strlen(str)+1)
  12. ,_str(new char[strlen(str)+1])
  13. {
  14. strcpy(_str,str);
  15. }
  16. String(const String& s):_sz(s._sz)
  17. ,_capacity(strlen(s._str)+1)
  18. ,_str(new char[strlen(_str)+1])
  19. {
  20. strcpy(_str,s._str);
  21. }
  22. ~String()
  23. {
  24. if(_str!=NULL)
  25. {
  26. delete[] _str;
  27. _str=NULL;
  28. _sz=0;
  29. _capacity=0;
  30. }
  31. }
  32. //String& operator=(const String& s)
  33. //{
  34. // if(this!=&s)
  35. // {
  36. // delete[] _str; //_str存放'\0',先将这块空间释放
  37. // _str=new char[strlen(s._str)+1]; //再为_str开辟能存放s._str的足够空间
  38. // strcpy(_str,s._str);
  39. // }
  40. // return *this;
  41. //}
  42. String& operator=(String s)
  43. {
  44. std::swap(_str,s._str);
  45. std::swap(_sz,s._sz);
  46. std::swap(_capacity,s._capacity);
  47. return *this;
  48. }
  49. char* c_str()
  50. {
  51. return _str;
  52. }
  53. char& operator[](int index)
  54. {
  55. return _str[index];
  56. }
  57. void PushBack(char c)
  58. {
  59. CheckCapacity(1);
  60. _str[_sz]=c;
  61. _sz++;
  62. _str[_sz]='\0';
  63. }
  64. String operator+(const String& s)
  65. {
  66. String tmp;
  67. tmp._str=new char[strlen(_str)+strlen(s._str)+1];
  68. strcpy(tmp._str,_str);
  69. strcat(tmp._str,s._str);
  70. return tmp;
  71. }
  72. String& operator+=(const String& s)
  73. {
  74. char* tmp=_str;
  75. _str=new char[strlen(_str)+strlen(s._str)+1];
  76. if(NULL==tmp)
  77. {
  78. exit(EXIT_FAILURE);
  79. }
  80. strcpy(_str,tmp);
  81. strcat(_str,s._str);
  82. return *this;
  83. }
  84. String& insert(int pos,const char* str)//在指定位置插入字符串
  85. {
  86. assert(pos>=_sz); //条件为真继续往下执行
  87. int len=strlen(str);
  88. CheckCapacity(_sz+len+1);
  89. int start=_sz;
  90. while(start>=pos)
  91. {
  92. _str[start+1]=_str[start];
  93. start--;
  94. }
  95. for(int i=0;i<len;i++)
  96. {
  97. _str[pos]=str[i];
  98. pos++;
  99. }
  100. return *this;
  101. }
  102. bool operator==(const String& s)
  103. {
  104. if(strcmp(_str,s._str)==0)
  105. {
  106. return true;
  107. }
  108. else
  109. return false;
  110. }
  111. private:
  112. void CheckCapacity(int count)
  113. {
  114. if(_sz+count>=_capacity)
  115. {
  116. int newcapacity=(2*_capacity>_capacity+count)
  117. ?(2*_capacity):(_capacity+count);
  118. char* tmp=new char[newcapacity];
  119. if(NULL==tmp)
  120. {
  121. exit(EXIT_FAILURE);
  122. }
  123. strcpy(tmp,_str);
  124. delete[] _str;
  125. _str=tmp;
  126. _capacity=newcapacity;
  127. }
  128. }
  129. private:
  130. char* _str;
  131. int _sz;
  132. int _capacity;
  133. };
  134. ostream& operator<<(ostream& os,const String& s)
  135. {
  136. os<<s._str<<endl;
  137. return os;
  138. }
  139. istream& operator>>(istream& is,String& s)
  140. {
  141. is>>s._str;
  142. return is;
  143. }
  144. void test1()
  145. {
  146. String s1("abcdef");
  147. String s2(s1);
  148. String s3;
  149. s3=s1;
  150. cout<<s1<<endl;
  151. cout<<s2<<endl;
  152. cout<<s3<<endl;
  153. }
  154. void test2()
  155. {
  156. String s1="hello";
  157. cout<<*(s1.c_str()+1)<<endl; //取出第二个字符
  158. cout<<strlen(s1.c_str())<<endl;
  159. cout<<s1[2]<<endl;
  160. s1[4]='a';
  161. cout<<s1<<endl;
  162. }
  163. void test3()
  164. {
  165. String s1="abcdef";
  166. s1.PushBack('k');
  167. cout<<s1<<endl;
  168. }
  169. void test4()
  170. {
  171. String s1("aacd");
  172. String s2("mmnp");
  173. String s3;
  174. s3=s1+s2;
  175. s1+=s2;
  176. cout<<s1<<endl;
  177. cout<<s3<<endl;
  178. }
  179. void test5()
  180. {
  181. String s="aaabb";
  182. s.insert(2,"cd");
  183. cout<<s.c_str()<<endl;
  184. }
  185. int main()
  186. {
  187. test5();
  188. system("pause");
  189. return 0;
  190. }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/383704.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python3常用数据结构

Python3中有三种组合数据类型&#xff0c;分别为&#xff1a; 序列类型&#xff1a;字符串&#xff08;str&#xff09;、元组&#xff08;tuple&#xff09;、列表&#xff08;list&#xff09;集合类型&#xff1a;集合&#xff08;set&#xff09;映射类型&#xff1a;字典…

Linux C++ 回射服务器

http://blog.csdn.net/qq_25425023/article/details/53914820回射服务器就是服务端将客户端的数据发送回去。我实现的回射服务器返回增加了时间。服务端代码&#xff0c;可以很容易看懂&#xff1a;[cpp] view plaincopy#include <sys/socket.h> #include <stdio.h&g…

TCP第四次挥手为什么要等待2MSL

当客户端进入TIME-WAIT状态的时候(也就是第四次挥手的时候)&#xff0c;必须经过时间计数器设置的时间2MSL(最长报文段寿命)后&#xff0c;才能进入关闭状态&#xff0c;这时为什么呢&#xff1f;&#xff1f;&#xff1f; 这最主要是因为两个理由&#xff1a; 1、为了保证客户…

计算机网络【一】概述+OSI参考模型

网络概述 局域网:覆盖范围小(100m以内)&#xff0c;自己花钱买设备&#xff0c;带宽固定(10M,100M,1000M)&#xff0c;自己维护&#xff08;接入层交换机直接连接电脑、汇聚层交换机直接连接接入层交换机&#xff09; 广域网:距离远&#xff0c;花钱买服务&#xff0c;租带宽&…

单链表逆序的多种方式

https://www.cnblogs.com/eniac12/p/4860642.htmltemplate<class T> void List<T>::Inverse() {if(first NULL) return;LinkNode<T> *p, *prev, *latter; p first->link;   // 当前结点prev NULL;   // 前一结点l…

Linux系统【四】进程间通信-管道

进程间通信&#xff08;IPC Interprocess Communication&#xff09; 进程和进程之间的通信只能通过内核&#xff0c;在内核中提供一块缓冲区进行通信。内核提供的这种机制叫做IPC 在进程间完成数据传输需要借助操作系统提供的特殊方法&#xff0c;如&#xff1a;文件&#xf…

单链表各种操作详解

#include "stdio.h" #include "stdlib.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 20 /* 存储空间初始分配量 */typedef int Status;/* Status是函数的类型,其值是函数结果状态代码&#xff0c;如OK等 */ typedef int…

Linux系统【五】进程间通信-共享内存mmap

mmap函数 #include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);参数&#xff1a; void *addr建立映射区的首地址&#xff0c;由Linux内核指定&#xff0c;所以我们直接传递NULL。也就是说虽然这是一个参宿但是并不…

socket编程 -- epoll模型服务端/客户端通信的实现

https://blog.csdn.net/y396397735/article/details/50680359 本例实现如下功能&#xff1a; 支持多客户端与一个服务端进行通信&#xff0c;客户端给服务端发送字符串数据&#xff0c;服务端将字符串中小写转为大写后发送回客户端&#xff0c;客户端打印输出经转换后的字符串。…

Python3 面向对象程序设计

类的定义 Python使用class关键字来定义类 class Car:def infor(self):print("This is a car") car Car() car.infor()内置方法isinstance()来测试一个对象是否为某个类的实例 self参数 类的 所有实例方法都有一个默认的self参数&#xff0c;并且必须是方法的第一…

计算机网络【二】物理层基础知识

计算机网络的性能 速率&#xff1a;连接在计算机网络上的主机在数字信道上传送数据位数的速率&#xff0c;也成为data rate 或bit rate&#xff0c;单位是b/s,kb/s,Mb/s,Gb/s。 我们平时所讲的宽带的速度是以字为单位的&#xff0c;但是实际中应用一般显示的是字节 &#xff0…

Linux网络编程——tcp并发服务器(多进程)

https://blog.csdn.net/lianghe_work/article/details/46503895一、tcp并发服务器概述一个好的服务器,一般都是并发服务器&#xff08;同一时刻可以响应多个客户端的请求&#xff09;。并发服务器设计技术一般有&#xff1a;多进程服务器、多线程服务器、I/O复用服务器等。二、…

求序列第K大算法总结

参考博客&#xff1a;传送门 在上面的博客中介绍了求序列第K大的几种算法&#xff0c;感觉收益良多&#xff0c;其中最精巧的还是利用快速排序的思想O(n)查询的算法。仔细学习以后我将其中的几个实现了一下。 解法 1&#xff1a; 将乱序数组从大到小进行排序然后取出前K大&a…

Linux网络编程——tcp并发服务器(多线程)

https://blog.csdn.net/lianghe_work/article/details/46504243tcp多线程并发服务器多线程服务器是对多进程服务器的改进&#xff0c;由于多进程服务器在创建进程时要消耗较大的系统资源&#xff0c;所以用线程来取代进程&#xff0c;这样服务处理程序可以较快的创建。据统计&a…

计算机网络【三】物理层数据通信

物理层传输媒介 导向传输媒体&#xff0c;比如光纤和铜线 双绞线&#xff08;屏蔽双绞线STP 五屏蔽双绞线UTP&#xff09;电线扭曲在一起可以降低互相之间的电磁干扰 同轴电缆 (50欧姆的基带同轴电缆&#xff0c;75欧姆的宽带同轴电缆) 10M和100M网络只使用了四根线&#xf…

02_算法分析

02_算法分析 0.1 算法的时间复杂度分析0.1.1 函数渐近增长概念&#xff1a;输入规模n>2时&#xff0c;算法A1的渐近增长小于算法B1 的渐近增长随着输入规模的增大&#xff0c;算法的常数操作可以忽略不计测试二&#xff1a;随着输入规模的增大&#xff0c;与最高次项相乘的常…

Linux网络编程——I/O复用之select详解

https://blog.csdn.net/lianghe_work/article/details/46506143一、I/O复用概述I/O复用概念&#xff1a;解决进程或线程阻塞到某个 I/O 系统调用而出现的技术&#xff0c;使进程不阻塞于某个特定的 I/O 系统调I/O复用使用的场合&#xff1a;1.当客户处理多个描述符&#xff08;…

Linux多进程拷贝文件

学习了mmap以后&#xff0c;实现一个简单的小程序&#xff0c;进行多个进程对一个文件进行拷贝。 Linux mmap共享内存学习可以参考我的另一篇博客&#xff1a;传送门 实现思想 我们可以将原来的文件利用mmap分成多个段分别进行传输。 实现代码 #include<stdio.h> #…

斐波那契查找(Fibonacci Search)和折半查找

两个查找算法都是针对有序数组进行查找&#xff0c;不同点在于分界点的取值不同。 算法介绍 折半查找很简单&#xff0c;每次与当前区间的中点进行比较&#xff0c;然后决定查找前一部分还是后一部分。 Fibonacci查找利用了Fibonacci序列每一项等于前两项和的特点进行划分&a…

Linux网络编程——tcp并发服务器(I/O复用之select)

https://blog.csdn.net/lianghe_work/article/details/46519633与多线程、多进程相比&#xff0c;I/O复用最大的优势是系统开销小&#xff0c;系统不需要建立新的进程或者线程&#xff0c;也不必维护这些线程和进程。代码示例&#xff1a;#include <stdio.h> #include &l…