目录
1.string容器
string容器是C++标准模板库提供的专门用来存储操作字符串的容器。下边介绍了string容器的一些基本操作,均参考于C++官方文档。
2.构造函数和析构函数的相关操作
3.迭代器
3.容量相关
4.元素访问相关
5.元素遍历相关
6.字符串操作
6.1 operator+=
6.2 append
6.3 push_back
6.4 insert && erase
6.5swap
7.string模拟实现
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<assert.h>
using namespace std;
namespace str {
class string {
public:
typedef char* iterator;
typedef char* reverse_iterator;
//构造函数
string(const char* a="") {
if (a == nullptr) {
assert(false);
}
_size = strlen(a);
_str = new char[_size + 1];
strcpy(_str, a);
_capcity = _size;
}
string(size_t n,char s) {
_str = new char[n+1];
memset(_str,s,n);
*(_str + n) = '\0';
_size = n;
_capcity = n;
}
string(const string& s)
:_str(nullptr)
{
/*
浅拷贝,两个对象地址相同,不安全
_size = s._size;
_capcity = s._capcity;
_str=s._str;
*/
///解决方式:深拷贝//
string tmp(s._str);
this->swap(tmp);
}
~string() {
if (_str) {
delete[] _str;
_str = nullptr;
_size = 0;
_capcity = 0;
}
}
//赋值运算符重载
string& operator=(string s) {
swap(s);
return *this;
}
//迭代器
iterator begin() {
return this->_str;
}
iterator end() {
return _str + _size;
}
reverse_iterator rbegin() {
return _str+_size;
}
reverse_iterator rend() {
return _str;
}
//capcity相关
size_t size()const {
return _size;
}
size_t length()const {
return _size;
}
size_t capcity()const {
return _capcity;
}
bool empty()const {
if (_size == 0) {
return true;
}
return false;
}
void resize(size_t newsize,char s) {
size_t oldsize = size();
if (newsize < 0) {
assert(0);
}
if (newsize <= oldsize) {
_str[newsize] = '\0';
}
else {
if ( newsize > _capcity) {
reserve(newsize);
}
append(newsize - oldsize, s);
}
_size = newsize;
}
void reserve(size_t newsize) {
if (newsize > _capcity) {
char* tem = new char[newsize+1];
strcpy(tem,_str);
delete[] _str;
_str=tem;
_capcity = newsize;
}
}
//访问相关
//modify相关
string& append(size_t n,char a) {
if (n + _size > _capcity) {
reserve(n + _size);
}
memset(_str+_size, a, n);
_str[_size + n] = '\0';
_size = n + _size;
return *this;
}
string& append(const char* str) {
size_t len=strlen(str);
if (len + _size > _capcity) {
reserve(len + _size);
}
strcat(_str,str);
_size = len + _size;
return *this;
}
//Element acess
char& operator[](size_t index) {
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index)const {
assert(index < _size);
return _str[index];
}
/modify
void push_back(char s) {
append(1,s);
}
string& operator+=(const string& s) {
append(s._str);
return *this;
}
string& operator+=(const char* str) {
append(str);
return *this;
}
//insert erase swap
string& insert(size_t pos,const string& s) {
size_t len = strlen(s._str);
char* p=s._str;
if (len + _size > _capcity) {
reserve(len+_size);
}
for (int i = _size+len; i>=pos; i--) {
_str[i+len] = _str[i];
}
while (*p != '\0') {
_str[pos] = *p;
p++;
}
return *this;
}
string& earse(size_t pos,size_t n) {
assert(pos<_size);
while (_str[pos] != '\0') {
_str[pos] = _str[pos + 1];
}
return *this;
}
///string operator
size_t find(char ch, size_t pos = 0) {
for (size_t i = pos; i < _size; i++) {
if (ch==_str[i]) {
return i;
}
}
return npos;
}
size_t rfind(char ch, size_t pos = npos) {
if (pos == npos) {
pos = _size - 1;
}
for (int i = _size-1; i >=0; i--) {
if (ch == _str[i]) {
return i;
}
}
return npos;
}
string substr(size_t pos = 0, size_t n =npos) {
if (n == npos) {
n = _size - pos;
}
char* tmp = new char[n + 1];
strncpy(tmp, _str + pos, n);
tmp[n] = '\0';
string strret(tmp);
delete[] tmp;
return strret;
}
void swap(string& s) {
std::swap(_str,s._str);
std::swap(_size, s._size);
std::swap(_capcity, s._capcity);
}
friend ostream& operator<<(ostream& _cout, const string& s)
{
_cout << s._str;
return _cout;
}
private:
char* _str;
size_t _size;
size_t _capcity;
static size_t npos;
};
size_t string::npos = -1;
void TestString1()
{
str::string s1;
str::string s2("hello");
str::string s3(s2);
str::string s4(10, 'A');
cout << s4.size() << std::endl;
cout << s2 << endl;
for (auto e : s3)
cout << e;
cout << endl;
auto it = s4.begin();
while (it != s4.end())
{
cout << *it;
++it;
}
cout << endl;
}
void TestString2()
{
str::string s("hello");
s.resize(10, '!');
std::cout << s << std::endl;
s.resize(7, '!');
}
void TestString3()
{
str::string s("abc.cpp");
str::string ret = s.substr(s.rfind('.') + 1);
cout << ret << endl;
}
}
int main() {
str::TestString1();
str::TestString2();
str::TestString3();
return 0;
}