当前位置:网站首页>String Simulation Implementation
String Simulation Implementation
2022-06-11 07:41:00 【m0_ fifty-two million twelve thousand six hundred and fifty-six】
Source code :
#pragma once
#include<iostream>
#include<cassert>
#include<string>
namespace ztl
{
using namespace std;
class string
{
friend ostream& operator<<(ostream& _cout, const ztl::string& s);
friend istream& operator>>(istream& _cin, ztl::string& s);
public:
typedef char* iterator;
public:
// Constructors
string(const char* str = "")
:_size(strlen(str))
, _capacity(strlen(str))
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
// Copy structure ( Modern writing )
string(const string& s)
:_size(strlen(s._str))
, _capacity(_size)
{
_str = nullptr;
string tem(s._str);
swap(tem);
}
// Copy... Copy
string& operator=(const string& s)
{
if (this != &s)
{
string tmp(s);
swap(tmp);
}
return *this;
}
// Destructor
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
// iterator Forward iterator
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
// Capacity expansion
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];// to \0 Leave a space
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
// modify
void push_back(char c)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 15 : _capacity * 2);
}
_str[_size] = c;
++_size;
_str[_size] = '\0';
//insert(_size, c);
}
string& operator+=(char c)
{
push_back(c);
return *this;
}
void append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
const char* c_str()const
{
return _str;
}
// capacity
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool empty()const
{
if (_str[0
] != '\0')
{
return false;
}
return true;
}
void resize(size_t n, char c = '\0')
{
if (n <= _size)
{
_size = n;
_str[_size] = '\0';
}
else
{
if (n > _capacity)
{
reserve(n);
}
memset(_str + _size, c, n - _size);
_size = n;
_str[_size] = '\0';
}
}
//void reserve(size_t n)
//{
// if (n > _capacity)
// {
// char* tmp = new char[n + 1];// to \0 Leave a space
// strcpy(tmp, _str);
// delete[] _str;
// _str = tmp;
// _capacity = n;
// }
//}
// access
char& operator[](size_t index)
{
assert(index < _size);
return *(_str + index);
}
const char& operator[](size_t index)const
{
assert(index < _size);
return *(_str + index);
}
//relational operators
bool operator<(const string& s)
{
if (strcmp(_str, s._str) < 0)
return true;
return false;
}
bool operator<=(const string& s)
{
if (strcmp(_str, s._str) <= 0)
return true;
return false;
}
bool operator>(const string& s)
{
}
bool operator>=(const string& s)
{
if (strcmp(_str, s._str) >= 0)
return true;
return false;
}
bool operator==(const string& s)
{
if (strcmp(_str, s._str) == 0)
return true;
return false;
}
bool operator!=(const string& s)
{
if (strcmp(_str, s._str) != 0)
return true;
return false;
}
// return c stay string The first place in
size_t npos = -1;
size_t find(char c, size_t pos = 0) const
{
for (size_t i = 0; i < _size; ++i)
{
if (c == _str[i])
{
return i;
}
}
return npos;
}
// Returns the substring s stay string The first place in
size_t find(const char* s, size_t pos = 0) const
{
char* ptr = strstr(_str + pos, s);
if (ptr == nullptr)
{
return npos;
}
else
return ptr - _str;
}
// stay pos Insert character in position c/ character string str, And return the position of the character
string& insert(size_t pos, char c)
{
assert(pos <= _size);
if (pos == _capacity)
{
reserve(_capacity == 0 ? 15 : _capacity * 2);
}
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = c;
_size++;
return *this;
}
string& insert(size_t pos, const char* str)
{
assert(pos <= _size);
size_t len = strlen(str);
if ((pos + len) > _capacity)
{
reserve(_size + len);
}
size_t end = _size + len;
while (end > pos + len)
{
_str[end] = _str[end - len];
end--;
}
strncpy(_str + pos, str, len);
return *this;
}
// Delete pos Elements in position , And return the next position of the element
string& erase(size_t pos, size_t len)
{
assert(pos < _size);
if (len == pos || pos + len > _capacity)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
private:
char* _str;
size_t _capacity;
size_t _size;
};
ostream& operator<<(ostream& _cout, const ztl::string& s)
{
cout << s._str;
return _cout;
}
// Sure cin to string assignment
istream& operator>>(istream& _cin, ztl::string& s)
{
// If the original string has content , Just kill it first
if (s._str != NULL)
{
delete[] s._str;
s._str = NULL;
s._size = 0;
}
// Set the cache
char temp[2048] = { 0 };
cout << " Input string :" << endl;
cin >> temp;
int len = strlen(temp);
s._str = new char[len + 1];
// Copy string
for (int i = 0; i <= len; i++)
{
s._str[i] = temp[i];
}
s._size = len;
return _cin;
}
void test1()
{
ztl::string s1("hello");
cout << s1 << endl;
ztl::string s2("uuuu");
cout << s2 << endl;
s2 = s1;
cout << s2 << endl;
s1.clear();
string s3(s1);
cout << s3 << endl;
s2.push_back('a');
s2.insert(0,'a');
cout << s2 << endl;
}
};边栏推荐
- Arduino_ STM development record
- [atcoder2000] leftmost ball (dp+ combination number)
- 3年功能测试拿8K,被新来的反超,其实你在假装努力
- Seata的几种事务模式
- MFC custom string linked list
- . Net C Foundation (6): namespace - scope with name
- C language function stack frame
- C language to write a calculator calculation logic
- Analyse du contrat du modèle de taux composé
- 如何准备PMP新版大纲考试?
猜你喜欢

C language Yanghui triangle code

C memory alignment

Miscellany C language

黑群晖DSM7.0.1物理机安装教程

2022年熔化焊接与热切割考试练习题及答案

2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings

Seata的几种事务模式

QT table display data

零基础自学SQL课程 | UNION 联合查询

Summary of classic interview questions
随机推荐
Sdl-4 PCM playback
Compound RateModel合約解析
Compound ratemodel contract analysis
【AtCoder2387】+/- Rectangle
Modular linear equations (Chinese remainder theorem + general solution)
[atcoder1984] wide swap
【CodeForces908H】New Year and Boolean Bridges (FWT)
Implementation of queue (C language)
May 30-June 5, 2022 AI industry weekly (issue 100): three years
[Oracle database] mammy tutorial day03 Sorting Query
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
What is the difference between gaussdb for redis and redis?
multi-sig SC
C language judging big end and small end [consortium or pointer] big end and small end conversion
学 SQL 必须了解的10个高级概念
【AtCoder2307】Tree Game(博弈)
Qunhui ds918 creates m.2 SSD read / write cache
【HDU6357】Hills And Valleys(DP)
【IoT】智能硬件:如何获取硬件产品的wifi信号强度
Sdl-3 YUV playback