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. }

string类的基本实现相关推荐

  1. C++ 笔记(22)— STL string 类(字符串赋值、访问、拼接、查找、翻转、大小写转换)

    1. 实例化和赋值 STL string #include <string> #include <iostream>int main () {using namespace s ...

  2. java string改变的影响_为什么Java的string类要设成immutable(不可变的)

    最流行的Java面试题之一就是:什么是不可变对象(immutable object),不可变对象有什么好处,在什么情况下应该用,或者更具体一些,Java的String类为什么要设成immutable类 ...

  3. C++——String类超详细介绍

    (欢迎及时指正错误!谢谢) STL的含义:标准模板库 STL的内容: 容器:数据的仓库 算法:与数据结构相关的算法.通用的算法(和数据结构无关) 注:熟悉常用的算法 sort  reverse 迭代器 ...

  4. 标准C++中的string类的用法总结

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...

  5. c++ string replace_JAVA应用程序开发之String类常用API

    [本文详细介绍了JAVA应用开发中的String类常用API,欢迎读者朋友们阅读.转发和收藏!] 1 基本概念 API ( Application Interface 应用程序接口)是类中提供的接口, ...

  6. javascript:为string类添加三个成员,实现去左,右,及所有空格

    <script language="JavaScript">    //此处为string类添加三个成员    String.prototype.Trim = func ...

  7. 字符串(string类)

    [1]String类基本函数如何实现? 示例代码如下: 1 #include<iostream> 2 #include<assert.h> 3 #include<stri ...

  8. 交换变量和String类初始化:JAVA入门基础

    本文主要介绍了变量交换.String类初始化.字符串的基本操作.变量交换详解介绍了两个变量是如何交换的,通过例子理解这个用法. 一.交换变量 1.什么是交换变量 例如用户输入a.b的值分别3,9的整数 ...

  9. 带你进入String类的易错点和底层本质分析!

    来源:https://my.oschina.net/liboware/blog/5076245 字符串拼接及创建的案例分析 案例一 String a = "test"; Strin ...

  10. string类具体用法

    string类具体用法 二话不说上代码 #include<string> #include<iostream> #include<algorithm> using ...

最新文章

  1. TinyML-TVM如何驯服TinyML
  2. 【ACM】杭电OJ 2010
  3. android 收不到短信广播,android – 短信广播接收器没有得到textmessage
  4. 微信内置浏览器无法清除缓存问题
  5. 将SmartForms转换为PDF保存到本地
  6. 软件工程教学博客 (备份)
  7. 10. VMware Tools 安裝
  8. 【华为云技术分享】云小课 | WAF反爬虫“三板斧”:轻松应对网站恶意爬虫
  9. iOS自定义下拉列表
  10. ALOS_PALSAR_12.5m分辨率DEM数据下载
  11. 基于SpringBoot实现二手交易商城
  12. SAP JCo BAPI的使用(刘欣) 2009-2-6
  13. 智能水杯设计方案_智能水杯的设计与营销
  14. SQL——PostgreSQL Driver
  15. 自控力读书笔记 第八章 传染:为什么意志力会传染?
  16. 课时11:列表:一个打了激素的数组2
  17. Z04 - 999、Flink与电商指标分析
  18. linux软件 mac地址,Linux MAC地址
  19. 乔布斯鲁宾_鲁宾·哈里斯(Ruben Harris)如何利用故事的力量闯入初创企业
  20. Ceph中一些PG相关的状态说明和基本概念说明、故障模拟

热门文章

  1. 洛谷 2719 搞笑世界杯
  2. File如何转换成MultipartFile
  3. php mongodb
  4. C++实例讲解Binder通信
  5. 数据库---T-SQL语句(一)
  6. zepto学习之路--源代码提取
  7. 使用快捷键,快到极致
  8. Linux 引导管理器 grub2 使用简介
  9. [Python]Pydev中使用中文
  10. 父类一实现serializable_我的java基础学习易错点和易忘点总结(一)