string类的基本实现
https://blog.csdn.net/qq_29503203/article/details/52265829
在面试中面试官常常会让你写出string类的基本操作,比如:构造函数,析构函数,拷贝构造等等.下面是除此之外的一些操作,希望可以帮助你更好的理解string以便以后的运用:
- String& operator=(const String& s);
- char* c_str();
- char& operator[](int index);
- void PushBack(char c);
- String operator+(const String& s);
- String& operator+=(const String& s);
- String& insert(int pos,const char* str);//在指定位置插入字符串
- bool operator==(const String& s);
为了读者方便,下面给出完整代码:
- #include<iostream>
- using namespace std;
- #include<cstring>
- #include<assert.h>
- class String
- {
- friend ostream& operator<<(ostream& os,const String& s);
- friend istream& operator>>(istream& is,String& s);
- public:
- String(const char* str=""):_sz(strlen(str))
- ,_capacity(strlen(str)+1)
- ,_str(new char[strlen(str)+1])
- {
- strcpy(_str,str);
- }
- String(const String& s):_sz(s._sz)
- ,_capacity(strlen(s._str)+1)
- ,_str(new char[strlen(_str)+1])
- {
- strcpy(_str,s._str);
- }
- ~String()
- {
- if(_str!=NULL)
- {
- delete[] _str;
- _str=NULL;
- _sz=0;
- _capacity=0;
- }
- }
- //String& operator=(const String& s)
- //{
- // if(this!=&s)
- // {
- // delete[] _str; //_str存放'\0',先将这块空间释放
- // _str=new char[strlen(s._str)+1]; //再为_str开辟能存放s._str的足够空间
- // strcpy(_str,s._str);
- // }
- // return *this;
- //}
- String& operator=(String s)
- {
- std::swap(_str,s._str);
- std::swap(_sz,s._sz);
- std::swap(_capacity,s._capacity);
- return *this;
- }
- char* c_str()
- {
- return _str;
- }
- char& operator[](int index)
- {
- return _str[index];
- }
- void PushBack(char c)
- {
- CheckCapacity(1);
- _str[_sz]=c;
- _sz++;
- _str[_sz]='\0';
- }
- String operator+(const String& s)
- {
- String tmp;
- tmp._str=new char[strlen(_str)+strlen(s._str)+1];
- strcpy(tmp._str,_str);
- strcat(tmp._str,s._str);
- return tmp;
- }
- String& operator+=(const String& s)
- {
- char* tmp=_str;
- _str=new char[strlen(_str)+strlen(s._str)+1];
- if(NULL==tmp)
- {
- exit(EXIT_FAILURE);
- }
- strcpy(_str,tmp);
- strcat(_str,s._str);
- return *this;
- }
- String& insert(int pos,const char* str)//在指定位置插入字符串
- {
- assert(pos>=_sz); //条件为真继续往下执行
- int len=strlen(str);
- CheckCapacity(_sz+len+1);
- int start=_sz;
- while(start>=pos)
- {
- _str[start+1]=_str[start];
- start--;
- }
- for(int i=0;i<len;i++)
- {
- _str[pos]=str[i];
- pos++;
- }
- return *this;
- }
- bool operator==(const String& s)
- {
- if(strcmp(_str,s._str)==0)
- {
- return true;
- }
- else
- return false;
- }
- private:
- void CheckCapacity(int count)
- {
- if(_sz+count>=_capacity)
- {
- int newcapacity=(2*_capacity>_capacity+count)
- ?(2*_capacity):(_capacity+count);
- char* tmp=new char[newcapacity];
- if(NULL==tmp)
- {
- exit(EXIT_FAILURE);
- }
- strcpy(tmp,_str);
- delete[] _str;
- _str=tmp;
- _capacity=newcapacity;
- }
- }
- private:
- char* _str;
- int _sz;
- int _capacity;
- };
- ostream& operator<<(ostream& os,const String& s)
- {
- os<<s._str<<endl;
- return os;
- }
- istream& operator>>(istream& is,String& s)
- {
- is>>s._str;
- return is;
- }
- void test1()
- {
- String s1("abcdef");
- String s2(s1);
- String s3;
- s3=s1;
- cout<<s1<<endl;
- cout<<s2<<endl;
- cout<<s3<<endl;
- }
- void test2()
- {
- String s1="hello";
- cout<<*(s1.c_str()+1)<<endl; //取出第二个字符
- cout<<strlen(s1.c_str())<<endl;
- cout<<s1[2]<<endl;
- s1[4]='a';
- cout<<s1<<endl;
- }
- void test3()
- {
- String s1="abcdef";
- s1.PushBack('k');
- cout<<s1<<endl;
- }
- void test4()
- {
- String s1("aacd");
- String s2("mmnp");
- String s3;
- s3=s1+s2;
- s1+=s2;
- cout<<s1<<endl;
- cout<<s3<<endl;
- }
- void test5()
- {
- String s="aaabb";
- s.insert(2,"cd");
- cout<<s.c_str()<<endl;
- }
- int main()
- {
- test5();
- system("pause");
- return 0;
- }
string类的基本实现相关推荐
- C++ 笔记(22)— STL string 类(字符串赋值、访问、拼接、查找、翻转、大小写转换)
1. 实例化和赋值 STL string #include <string> #include <iostream>int main () {using namespace s ...
- java string改变的影响_为什么Java的string类要设成immutable(不可变的)
最流行的Java面试题之一就是:什么是不可变对象(immutable object),不可变对象有什么好处,在什么情况下应该用,或者更具体一些,Java的String类为什么要设成immutable类 ...
- C++——String类超详细介绍
(欢迎及时指正错误!谢谢) STL的含义:标准模板库 STL的内容: 容器:数据的仓库 算法:与数据结构相关的算法.通用的算法(和数据结构无关) 注:熟悉常用的算法 sort reverse 迭代器 ...
- 标准C++中的string类的用法总结
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
- c++ string replace_JAVA应用程序开发之String类常用API
[本文详细介绍了JAVA应用开发中的String类常用API,欢迎读者朋友们阅读.转发和收藏!] 1 基本概念 API ( Application Interface 应用程序接口)是类中提供的接口, ...
- javascript:为string类添加三个成员,实现去左,右,及所有空格
<script language="JavaScript"> //此处为string类添加三个成员 String.prototype.Trim = func ...
- 字符串(string类)
[1]String类基本函数如何实现? 示例代码如下: 1 #include<iostream> 2 #include<assert.h> 3 #include<stri ...
- 交换变量和String类初始化:JAVA入门基础
本文主要介绍了变量交换.String类初始化.字符串的基本操作.变量交换详解介绍了两个变量是如何交换的,通过例子理解这个用法. 一.交换变量 1.什么是交换变量 例如用户输入a.b的值分别3,9的整数 ...
- 带你进入String类的易错点和底层本质分析!
来源:https://my.oschina.net/liboware/blog/5076245 字符串拼接及创建的案例分析 案例一 String a = "test"; Strin ...
- string类具体用法
string类具体用法 二话不说上代码 #include<string> #include<iostream> #include<algorithm> using ...
最新文章
- TinyML-TVM如何驯服TinyML
- 【ACM】杭电OJ 2010
- android 收不到短信广播,android – 短信广播接收器没有得到textmessage
- 微信内置浏览器无法清除缓存问题
- 将SmartForms转换为PDF保存到本地
- 软件工程教学博客 (备份)
- 10. VMware Tools 安裝
- 【华为云技术分享】云小课 | WAF反爬虫“三板斧”:轻松应对网站恶意爬虫
- iOS自定义下拉列表
- ALOS_PALSAR_12.5m分辨率DEM数据下载
- 基于SpringBoot实现二手交易商城
- SAP JCo BAPI的使用(刘欣) 2009-2-6
- 智能水杯设计方案_智能水杯的设计与营销
- SQL——PostgreSQL Driver
- 自控力读书笔记 第八章	 传染:为什么意志力会传染?
- 课时11:列表:一个打了激素的数组2
- Z04 - 999、Flink与电商指标分析
- linux软件 mac地址,Linux MAC地址
- 乔布斯鲁宾_鲁宾·哈里斯(Ruben Harris)如何利用故事的力量闯入初创企业
- Ceph中一些PG相关的状态说明和基本概念说明、故障模拟