1. 程式人生 > >C++ 模擬String類增刪查改

C++ 模擬String類增刪查改

String是C++中的重要型別,程式設計師在C++面試中經常會遇到關於String的細節問題,甚至要求當場實現這個類。只是由於時間關係,可能只要求實現建構函式、解構函式、拷貝建構函式等關鍵部分。

String的實現涉及很多C++的基礎知識、記憶體控制及異常處理等問題,仔細研究起來非常複雜,本文主要做一個簡單介紹和講解模擬實現String類的增刪查改。

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
using namespace std;
class String
{
public:
    String(char
* str = "") { _size = strlen(str); _capacity = _size; _str = new char[strlen(str)+1]; strcpy(_str,str); } void Swap(String& s) { swap(_str,s._str); swap(_size,s._size); swap(_capacity,s._capacity); } String(const String& s) :_
str(NULL) ,_size(0) ,_capacity(0) { String tmp(s._str); Swap(tmp); } String& operator=(String s) { Swap(s); return *this; } ~String() { if (_str) { delete[] _str; } } char* Getstr() { return
_str; } size_t Size() { return _size; } size_t Capacity() { return _capacity; } void Expand(size_t n) { if (n > _capacity) { _str = (char*)realloc(_str,n+1); assert(_str); _capacity = n; } } void PushBack(char ch) { if (_size == _capacity) { Expand(_capacity * 2); } _str[_size] = ch; ++_size; _str[_size] = '\0'; } void PushBack(const char* str) { size_t len = strlen(str); if (len + _size > _capacity) { Expand(len + _size); } strcpy(_str + _size,str); } void PopBack() { assert(_size); _str[_size-1] = _str[_size]; _size--; } void Insert(int pos,char ch) { if (_size==_capacity) { Expand(_capacity*2); } int end = _size; while (end >= (int)pos) { _str[end + 1] = _str[end]; --end; } _str[pos] = ch; ++_size; } void Insert(size_t pos, const char* str) { size_t len = strlen(str); if (_size+len>_capacity) { Expand(_size+len); } int end = _size; while (end >= (int)pos) { _str[end + len] = _str[end]; --end; } while (*str) { _str[pos++] = *str++; } _size += len; } void Erase(size_t pos, size_t count) { if (pos+count >= _size) { _str[pos] = '\0'; _size = pos; } else { strcpy(_str + pos, _str + pos + count); _size -= count; } } size_t Find(char ch) const { size_t i = 0; for (i = 0;i<_size;i++) { if (_str[i]==ch) { return i; } } return -1; } size_t Find(const char* str) const { assert(str); const char* srcStr = _str; const char* subStr = str; size_t srcIndex = 0; size_t subIndex = 0; size_t subLen = strlen(subStr); while (srcIndex < _size) { size_t matchIndex = srcIndex; while (srcStr[matchIndex] == subStr[subIndex]) { ++matchIndex; ++subIndex; if (subIndex == subLen) { return srcIndex; } } subIndex = 0; srcIndex++; } return -1; } char& operator[](size_t pos) { assert(pos<_size); return _str[pos]; } bool operator<(const String& s) const { size_t i = 0; for (i = 0;i < _size && i < s._size;i++) { if (_str[i] < s._str[i]) { return true; } else if (_str[i]>s._str[i]) { return false; } } if (i==_size) { return true; } else { return false; } } bool operator<=(const String& s) const { return (*this<s)|| (*this==s); } bool operator>(const String& s) const { return !(*this<=s); } bool operator>=(const String& s) const { return (*this>s)||(*this==s); } bool operator==(const String& s) const { size_t i = 0; for (i = 0;i < _size && i < s._size;i++) { if (_str[i] != s._str[i]) { return false; } } if (i == _size&&i == s._size) { return true; } else { return false; } } bool operator!=(const String& s)const { return !(*this==s); } private: size_t _size; size_t _capacity; char* _str; };