c++模拟实现string
来源:互联网 发布:mac windows 双系统 编辑:程序博客网 时间:2024/06/06 04:56
#define _CRT_SECURE_NO_WARNINGS 1#include <iostream> using namespace std; class String { public: String(const char* str=" ") { size_t len=strlen(str);_str=new char[len+1];strcpy(_str,str);_size=len;_capacity=len; } String(const String& s) :_str(NULL) { String tmp(s._str); tmp.Swap(*this); } String& operator=(String s) { this->Swap(s); return *this; } ~String() { if (_str) { delete [] _str; _str=NULL; _size=_capacity=0; } } char* Getstr() { return _str; } const char* Getstr() const { return _str; } size_t Size() { return _size; } size_t Size() const { return _size; } size_t capacity() { return _capacity; } void Expand(size_t n) { assert(n>_capacity);_str=(char*)realloc(_str,n);_capacity=n; } void PushBack(char ch) { if (_size==_capacity) {Expand(_size*2); }_str[_size]=ch;_str[_size+1]='\0';++_size; } void Insert(size_t pos,const char* str) { assert(pos<=_size); size_t len=strlen(str); if (_size+len>_capacity) { Expand(_size+len+1); } size_t end=_size; while (end>=pos) { _str[len+end]=_str[end]; --end; } while (*str) { _str[pos]=*str; ++pos; ++str; } } void Insert(size_t pos, char ch) { assert(pos <= _size); size_t end = _size; while(end >= pos) { _str[end+1] = _str[end]; --end; } _str[pos] = ch; ++_size; } void Insert(size_t pos,const String& s,size_t subPos, size_t subLen) { assert(subPos<s.Size()); const char* str=s.Getstr()+subPos; while (subLen) { Insert(pos,*str); ++str; ++subPos; --subLen; ++pos; } } void Erase(size_t pos,size_t len) { assert(pos<_size);if (pos+len>=_size-1){_size=pos;_str[_size]='\0';}else{char* dst=_str+pos+len;char* src=_str+pos; strcpy(src,dst);_size-=len;} } int Find(char ch) { size_t i=0; for (i=0;i<_size;i++) { if (ch=_str[i]) { return 1; } else return 0; } } int Find(const char* sub) { size_t len=strlen(sub); size_t i=0; char* str1=_str; if (* str1++=*sub++) { return 1; } else return 0; } bool operator<(const String& s) { assert(s._str); } bool operator>(const String& s) { int i = 0; while(_str[i] == s._str[i]) { ++i; } if(_str[i] > s._str[i]) { return true; } return false; } bool operator>=(const String& s) { return *this==s||*this>s; } bool operator<=(const String& s) { return !(*this>s); } bool operator==(const String& s) { int i=0; while(_str[i]==s._str[i]) { ++i; } if (i==_size) { return true; } return false; } bool operator!=(const String& s) { return !(*this==s); } String SubStr(size_t pos,size_t len) { assert(pos<_size); } void Swap(String& s) { swap(_str, s._str); swap(_size, s._size); swap(_capacity, s._capacity); } private: char* _str; size_t _size; size_t _capacity; } ;
#include <iostream>
using namespace std;
#include <assert.h>
#include <string.h>#include "String.h"
void TestString(){ String s1("hello"); cout<<s1.Getstr()<<endl; String s2(s1); s2.Erase(2,2); cout<<s2.Getstr()<<endl; s1.Insert(5," world"); cout<<s1.Getstr()<<endl;}int main(){ TestString(); return 0;}
3 0
- C++:模拟实现string类
- 模拟实现string类(c++)
- 【C++】模拟实现String类
- String函数的模拟实现(c语言)
- 模拟实现C++STL中的string类
- 【c++】模拟实现string类__实现增删查改
- C语言 string及memcpy,memmove函数的模拟实现
- 模拟实现string类
- 模拟实现string库函数
- 模拟实现string类
- String的模拟实现
- c++模拟实现string
- 模拟实现string
- 模拟实现 string类
- 模拟实现string类
- 模拟实现string
- 模拟实现string类
- 模拟实现string类
- T解 POJ-3233 [矩阵快速幂][矩阵乘法][二分求解]
- CMake Tutorial(2)——Adding a Library
- 机器学习领域著名的期刊与会议
- error processing package libapache2-mod-php5 (–configure)
- CoreText(图文混排)详解+Demo
- c++模拟实现string
- Unity3D学习(4)之分离动作管理器的3D版牧师与魔鬼
- [杂题 模拟] BZOJ 1182 [Croatian2009]PLAHTE
- 《编程之法》:荷兰国旗
- Centos 7 安装 Xen
- 判断一个数是否为素数(反证法)
- WebMagic写的网络爬虫
- [ZJOI2815]灾难 建树+动态LCA+树上dp
- org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: XXX的解决方案