[C++]字符串代码
最编程
2024-10-03 07:21:50
...
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
namespace ns1
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
// 默认构造
string(const char* str = "")
{
_size = _capacity = strlen(str);
_str = new char[_size + 1];
// 拷贝数据
strcpy(_str, str);
}
// 析构
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
// 拷贝构造
string(const string& s)
{
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
// 容量
size_t size() const
{
return _size;
}
size_t capacity() const
{
return _capacity;
}
bool empty() const
{
return _size == 0;
}
void clear()
{
_size = 0;
_str[_size] = '\0';
}
void reserve(size_t n = 0)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void resize(size_t n, char c = '\0')
{
// 删除
if (n <= _size)
{
_str[n] = '\0';
_size = n;
}
else // 填充
{
// 需不需要扩容,交给 reserve 去判断
reserve(n);
for (int i = _size; i < n; i++)
_str[i] = c;
_str[n] = '\0';
_size = n;
}
}
// 遍历
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
iterator begin()
{
return _str;
}
const_iterator begin() const
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator end() const
{
return _str + _size;
}
// 修改
void push_back(char c)
{
检查扩容
//if (_size == _capacity)
// reserve(_capacity == 0 ? 4 : 2 * _capacity);
//_str[_size] = c;
//++_size;
//_str[_size] = '\0';
insert(_size, c);
}
void append(const char* str)
{
/*size_t len = strlen(str);
if (_size + len > _capacity)
reserve(_size + len);
strcpy(_str + _size, str);
_size += len;*/
insert(_size, str);
}
string& operator+=(char c)
{
push_back(c);
return *this;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
void insert(size_t pos, char c)
{
assert(pos <= _size); // 支持尾插
// 检查扩容
if (_size == _capacity)
reserve(_capacity == 0 ? 4 : 2 * _capacity);
// 挪动
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = c;
++_size;
}
void insert(size_t pos, const char* str)
{
// 检查容量
size_t len = strlen(str);
if (_size + 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);
_size += len;
}
void erase(size_t pos = 0, size_t len = npos)
{
assert(pos < _size);
if (len == npos || len > _size - pos) // pos + len可能会溢出
{
// 从 pos 开始全部删除
_str[pos] = '\0';
_size = pos;
}
else
{
// 部分删除
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
size_t find(char c, size_t pos = 0) const
{
assert(pos < _size);
for (int i = pos; i < _size; i++)
if (_str[i] == c)
return i;
return npos;
}
size_t find(const char* s, size_t pos = 0) const
{
const char* ret = strstr(_str + pos, s);
if (ret)
return ret - _str; // 返回下标
return npos;
}
string substr(size_t pos = 0, size_t len = npos) const
{
string tmp;
// 全部截取
if (len == npos || len > _size - pos)
{
for (int i = pos; i < _size; i++)
tmp += _str[i];
}
else
{
// 部分截取
for (int i = pos; i < pos + len; i++)
tmp += _str[i];
}
return tmp;
}
const char* c_str() const
{
return _str;
}
private:
char* _str = nullptr;
size_t _size = 0;
size_t _capacity = 0;
public:
static int npos;
};
int string::npos = -1;
// 全局函数
void swap(string& s1, string& s2)
{
s1.swap(s2);
}
bool operator==(const string& s1, const string& s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return ret == 0;
}
bool operator<(const string& s1, const string& s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return ret < 0;
}
bool operator<=(const string& s1, const string& s2)
{
return s1 == s2 || s1 < s2;
}
bool operator>(const string& s1, const string& s2)
{
return !(s1 <= s2);
}
bool operator>=(const string& s1, const string& s2)
{
return !(s1 < s2);
}
bool operator!=(const string& s1, const string& s2)
{
return !(s1 == s2);
}
ostream& operator<<(ostream& os, const string& str)
{
for (auto e : str)
os << e;
return os;
}
istream& operator>> (istream& is, string& str)
{
str.clear();
char ch;
ch = is.get();
char buff[128];
int i = 0;
while (ch != ' ' && ch != '\n')
{
// 字符放到buff
buff[i++] = ch;
// buff 满了,转移到string
if (i == 127)
{
buff[i] = '\0';
i = 0;
str += buff;
}
ch = is.get();
}
// 清理buff
if (i > 0)
{
buff[i] = '\0';
str += buff;
}
return is;
}
istream& getline(istream& is, string& str)
{
str.clear();
char ch;
ch = is.get();
char buff[128];
int i = 0;
while (ch != '\n')
{
// 字符放到buff
buff[i++] = ch;
// buff 满了,转移到string
if (i == 127)
{
buff[i] = '\0';
i = 0;
str += buff;
}
ch = is.get();
}
// 清理buff
if (i > 0)
{
buff[i] = '\0';
str += buff;
}
return is;
}
}
{
// 字符放到buff
buff[i++] = ch;
// buff 满了,转移到string
if (i == 127)
{
buff[i] = '\0';
i = 0;
str += buff;
}
ch = is.get();
}
// 清理buff
if (i > 0)
{
buff[i] = '\0';
str += buff;
}
return is;
}
istream& getline(istream& is, string& str)
{
str.clear();
char ch;
ch = is.get();
char buff[128];
int i = 0;
while (ch != '\n')
{
// 字符放到buff
buff[i++] = ch;
// buff 满了,转移到string
if (i == 127)
{
buff[i] = '\0';
i = 0;
str += buff;
}
ch = is.get();
}
// 清理buff
if (i > 0)
{
buff[i] = '\0';
str += buff;
}
return is;
}
}
推荐阅读
-
c 和 c++ 一样吗?
-
ROS C++:使用 ros::AsyncSpinner 多线程处理 ROS 消息
-
C++ priority_queue 优先级队列构造大根堆和小根堆
-
C++ 语言特性 15 - 基于范围的 For 循环
-
华为 OD 机测试 - 间隔交织问题 - 贪婪算法(Python/JS/C/C++ 2024 年 E 卷 200 分) - IX、C++ 算法源代码
-
计算机毕业设计 基于深度学习的短视频内容理解与推荐系统的设计与实现 Python+Django+Vue 前后端分离,附源代码 讲座 文档
-
旅程 6 NV12 理论和代码详情
-
C++] 模拟红黑树的实现
-
C++ - 输入一个 2*3 矩阵,将该矩阵向左旋转 90 度并输出。(要求:使用指针完成)。
-
UE 行为树编辑器图解说明 - 行为树编辑器代码分析