目录
15find的模拟实现,从pos位置找一个字符,返回下标位置
模拟实现string先定义头文件zai
#include<iostream>
#include<stdlib.h>
#include<assert.h>
using namespace std;
再定义一个自己的域,域里写string,目的是为了和#include<string>中的string区分开
namespace ss
{
class string
{
};
}不用加;
1构造初始化带参
string(const char* str)
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
memcpy(_str, str, _size + 1);//要加一因为不会拷贝\0.
}
2无参的构造,有一个\0
string()
:_size(0)
, _capacity(0)
, _str(new char[1])
{
_str[0] = '\0';
}
3析构
//析构
~string()
{
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
4打印的方式(带不带const)
const char* c_str()const
{
return _str;
}
const char* c_str()
{
return _str;
}
5长度的计算
size_t size()const
{
return _size;
}
6[]的操作符重载(带不带const)
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];//空间不销毁可以用&返回
}
const char& operator[](size_t pos)const
{
assert(pos < _size);
return _str[pos];//空间不销毁可以用&返回
}
7尾插的模拟实现
//尾插的模拟实现
void push_back(const char ch)
{
if (_size == _capacity)
{
//2倍扩容,还要防止为空的情况
reserve(_capacity == 0 ? 4 : (_capacity * 2));
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
8插入字符串
void append(const char* ch)
{
int len = strlen(ch);
if (_size + len > _capacity)
{
//2倍扩容,还要防止为空的情况
reserve(_size + len);
}
memcpy(_str + _size, ch, len + 1);
}
9+=的操作符重载
字符
string& operator+=(char ch)//字符
{
push_back(ch);
return *this;
}
字符串(常量字符串不可修改)
string& operator+=(const char* ch)//字符串
{
append(ch);
return *this;
}
10迭代器的模拟实现
typedef char* iterator;
typedef const char* const_iterator;
带不带const的迭代器
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
11insert的模拟实现(插入字符)
void insert(int pos, int n, char ch)
{
assert(pos < _size);
int end = _size;
if (n + _size > _capacity)
{
reserve(n + _size);
}
//挪动数据,先把pos位置后的往后移动,也要把pos位置的字符往后移动n个,_size!=pos正好可以
while (_size >= pos)
{
_str[_size + n] = _str[_size];//记住是+n因为是加了n个字符
_size--;
}
//给值
for (int i = 0; i < n; i++)
{
_str[pos++] = ch;
}
_size = end;
_size += n;
}
12insert的模拟实现,pos位置插入字符串
void insert(int pos, const char* ch)
{
assert(pos < _size);
int end = _size;
int len = strlen(ch);
if (len + _size > _capacity)
{
reserve(len + _size);
}
//挪动位置
while (_size >= pos)
{
_str[_size + len] = _str[_size];
_size--;
}
//赋值
for (int i = 0; i < len; i++)
{
_str[pos++] = ch[i];
}
_size = end;
_size += len;
}
13扩容
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
memcpy(tmp, _str, _size + 1);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
14erase的模拟实现,pos位置后删除len个字符
void erase(int pos, int len)
{
int end = _size;
assert(pos < _size);
if (len > _size - pos || len > _size)
{
_size = pos + 1;
}
else
{
int n = _size - pos - len - 1;
for (int i = 0; i < n; i++)
{
_str[pos + 1] = _str[pos + len + 1];
pos++;
}
}
_str[pos + 1] = '\0';
_size = end;
_size -= len;
}
15find的模拟实现,从pos位置找一个字符,返回下标位置
int find(char ch, int pos = 0)
{
assert(pos < _size);
int i = 0;
while (ch != _str[i] && i < _size)
{
i++;
}
if (ch == _str[i])
{
return i;
}
else
return npos;
}
16find的模拟实现,找字符串
int find(const char* ch, int pos = 0)//abcde cd
{
assert(pos < _size);
int n = strlen(ch);
const char* ptr = strstr(_str, ch);//在_str中找字符串ch
if (ptr)
{
return ptr - _str;//返回一个整数
}
else
return npos;
}
17模拟实现substr,从pos位置开始取len个字符
string substr(int pos, int len)
{
string ss;
if (len == npos || len + pos > _size)
{
len = _size - pos;
}
for (int i = 0; i < len; i++)
{
ss += _str[pos + i];
}
return ss;
}
18拷贝构造
string(const string& s)
{
_str = new char[s._capacity + 1];
memcpy(_str, s._str, s._size + 1);//先扩容
_size = s._size;
_capacity = s._capacity;
}
19赋值=的模拟实现,深拷贝
//赋值=的模拟实现,深拷贝
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[s._capacity + 1];
memcpy(tmp, s._str, s._size + 1);
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
20resize的模拟实现,可以删除数据
分情况://resize的模拟实现,可以删除数据,也可以扩容,不缩容,当_capacity为15,_size为10时
//resize(8)就是删除数据(删除两个),resize(12)就是插入数据,resize(18)就是扩容
void resize(int n, char ch = '\0')
{
if (n < _size)
{
_size = n;
}
else
{
if (n > _capacity)
{
reserve(n);//不加if也行因为reserve会自行判断。
}
int k = n - _size;
for (int i = 0; i < k; i++)
{
_str[_size] = ch;
_size++;
}
}
//记得加\0
_str[n] = '\0';
}
21clear
void clear()
{
_str[0] = '\0';
_size = 0;
}
22字符串比较
字符串比较,strcmp会有\0问题memcmp有别的问题 word和wordxxx,长的大
/三种情况
/hello hello
/helloxx hello
/hello helloxx
法1
bool operator<(const string& s) const
{
int i1 = 0;
int i2 = 0;
//当i1<i1._size和i2<i2._size时的情况
while (i1 < _size && i2 < s._size)//左边的是this
{
if (_str[i1] < s._str[i2])
{
return true;
}
else if (_str[i1] > s._str[i2])
{
return false;
}
else
{
i1++;
i2++;
}
}
//如果没返回就是因为全都相等在_size的范围,小的_size的范围,也有可能相等
if (_size == s._size)//相等也不是小于,所以返回false
{
return false;
}
else if (_size < s._size)
{
return true;
}
else if (_size > s._size)
{
return false;
}
}
法2
//return _size < s._size;简便方法,如果左边大于等于右边的长度则为假
//直接赋用,用memcmp或者strcmp,也只能判断相不相等,不能判断谁大谁小
//三种情况
//hello hello
//helloxx hello
//hello helloxx
//int ret = memcmp(_str, s._str, _size < s._size ? _size : s._size);//取小的那个比较
//
相等返回0,小于0就是左边小于右边,大于0就是右边大于左边
//return ret==0?_size<s._size:ret<0;//ret==0,代表小的_size范围内都相等,然后再看长度
翻译如下
//if (ret == 0 && _size == s._size)
//{
// return false;
//}
//if (ret != 0)
//{
// return _size < s._size;
//}
23字符串比较==
bool operator==(const string& s) const
{
return _size==s._size&&memcmp(_str, s._str,_size) == 0;//等于0 就是相等
}
24字符串<=
bool operator<=(const string& s) const
{
return *this < s || *this == s;
}
25字符串>
bool operator>(const string& s) const
{
return !(*this <= s );
}
26字符串!=
bool operator!=(const string& s) const
{
return !(*this == s);
}
27字符串>=
bool operator>=(const string& s) const//都加上const普通对象和const的对象都可以用,权限可以缩小
{
return !(*this < s);
}
28private:
private:
int _size;
int _capacity;
char* _str;
int npos = 11111111;
29<<的模拟实现
ostream&operator<<(ostream&out,const string&s),不能在类里
ostream& operator<<( ostream& out, const string& s)
{
for (auto& e : s)//范围for需要调用迭代器,const sting就要调用const的迭代器
{
out << e;
}
return out;
}
30>>的模拟实现,流插
istream& operator>>(istream& in, string& s)
{
s.clear();
char ch = in.get();
while (ch != ' ' && ch != '\n')//输入时有空格或者回车就结束了,输入的时候就是这样,要是想输入空格就需要用getline
{
s += ch;
ch = in.get();//get是用来读取下一个字符,会依次往后读
}
return in;
}
31main测试
int main()
{
const ss::string s1("aaa");//加上一个const,c_str和size()后都需要加上const
ss:: string s2("baa");//s2不加const也可以打印,因为权限可以缩小不可以放大
cout << s1.c_str() << endl;
cout << s1.size() << endl;
cout << s2.c_str() << endl;
cout << s2.size() << endl;
//[]的操作符重载测试
cout << s1[1] << endl;
//迭代器的模拟实现
ss::string s3("abcde");
ss::string::iterator it = s3.begin();
while (it != s3.end())
{
cout << *it <<" ";
++it;
}
cout << endl;
for (auto ch : s3)
{
cout << ch << " " ;
}
cout << endl;
//尾插的模拟实现
ss::string s4("abcd");
s4.push_back('e');
cout << s4.c_str() << endl;
//插入字符串
ss::string s5("aaaaa");
s5.append("bbbb");
cout << s5.c_str() << endl;
//+=的操作符重载
ss::string s6("aaab");
s6+= "ccc";
cout << s6.c_str() << endl;
s6 += 'd';
cout << s6.c_str() << endl;
//insert的模拟实现
ss::string s7("aa");
s7.insert(0, 7, 'x');//在下标为一的位置插入7个'x'。
cout << s7.c_str() << endl;
//pos位置插入字符串
ss::string s8("abcd");
s8.insert(0, "kkkk");
cout << s8.c_str() << endl;
//erase的模拟实现,pos位置后删除len个字符
ss::string s9("abcdef");
s9.erase(1,2);
cout << s9.c_str() << endl;
//find的模拟实现,找字符
ss::string s10("ggc");
cout<<s10.find('c',2) << endl;
//find的模拟实现,找字符串
ss::string s11("abcde");
cout << s11.find("cd", 0)<<endl;
//模拟实现substr,从pos位置开始取len个字符
ss::string ss = s11.substr(s11.find("cd", 0), 2);
cout << ss.c_str() << endl;
ss::string s12("abcdef");
ss::string sss = s12.substr(0, 100);
cout << sss.c_str()<< endl;
//拷贝构造
ss::string s13(s12);
cout << s13.c_str() << endl;
//resize的模拟实现,可以删除数据,也可以扩容,不缩容,当_capacity为15,_size为10时
//resize(8)就是删除数据(删除两个),resize(12)就是插入数据,resize(18)就是扩容
ss::string s14("abcdefg");//7
s14.resize(10,'x');//第二个不传就是默认是'\0',这里的意思是删除数据
cout << s14.c_str() << endl;
//<<的模拟实现ostream&operator<<(ostream&out,const string&s)
ss::string s15("abcd");
cout << s15 << endl;
//>>的模拟实现,流插入
ss::string s16;
//cin >>s16;
//cout << s16 << endl;
//字符串比较,strcmp会有\0问题memcmp有别的问题 word和wordxxx,长的大
//三种情况
//hello hello
//helloxx hello
//hello helloxx
ss::string s17("helloxx");
ss::string s18("hello");
cout << (s17 != s18) << endl;
//赋值=的模拟实现,深拷贝
s18 = s17;
std::swap(s18, s15);//可以直接交换
cout << s18 << endl;
return 0;
}
32总代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<assert.h>
using namespace std;
namespace ss
{
class string
{
public:
//构造初始化带参
string(const char* str)
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
memcpy(_str, str, _size + 1);//要加一因为不会拷贝\0.
}
//无参的构造,有一个\0
string()
:_size(0)
, _capacity(0)
, _str(new char[1])
{
_str[0] = '\0';
}
//析构
~string()
{
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
//打印的方式
const char* c_str()const
{
return _str;
}
const char* c_str()
{
return _str;
}
//长度的计算
size_t size()const
{
return _size;
}
//[]的操作符重载
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];//空间不销毁可以用&返回
}
const char& operator[](size_t pos)const
{
assert(pos < _size);
return _str[pos];//空间不销毁可以用&返回
}
//+=的操作符重载
string& operator+=(const char* ch)//字符串
{
append(ch);
return *this;
}
string& operator+=(char ch)//字符
{
push_back(ch);
return *this;
}
//迭代器的模拟实现
typedef char* iterator;
typedef const char* const_iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
//insert的模拟实现(插入字符) "abcde\0\0\0\0" 2,2,'x'
void insert(int pos, int n, char ch)
{
assert(pos < _size);
int end = _size;
if (n + _size > _capacity)
{
reserve(n + _size);
}
//挪动数据,先把pos位置后的往后移动,也要把pos位置的字符往后移动n个,_size!=pos正好可以
while (_size >= pos)
{
_str[_size + n] = _str[_size];//记住是+n因为是加了n个字符
_size--;
}
//给值
for (int i = 0; i < n; i++)
{
_str[pos++] = ch;
}
_size = end;
_size += n;
}
//insert的模拟实现,pos位置插入字符串
void insert(int pos, const char* ch)
{
assert(pos < _size);
int end = _size;
int len = strlen(ch);
if (len + _size > _capacity)
{
reserve(len + _size);
}
//挪动位置
while (_size >= pos)
{
_str[_size + len] = _str[_size];
_size--;
}
//赋值
for (int i = 0; i < len; i++)
{
_str[pos++] = ch[i];
}
_size = end;
_size += len;
}
//扩容
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
memcpy(tmp, _str, _size + 1);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
//尾插的模拟实现
void push_back(const char ch)
{
if (_size == _capacity)
{
//2倍扩容,还要防止为空的情况
reserve(_capacity == 0 ? 4 : (_capacity * 2));
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
//插入字符串
void append(const char* ch)
{
int len = strlen(ch);
if (_size + len > _capacity)
{
//2倍扩容,还要防止为空的情况
reserve(_size + len);
}
memcpy(_str + _size, ch, len + 1);
}
//erase的模拟实现,pos位置后删除len个字符 "abcdef" 1 2
void erase(int pos, int len)
{
int end = _size;
assert(pos < _size);
if (len > _size - pos || len > _size)
{
_size = pos + 1;
}
else
{
int n = _size - pos - len - 1;
for (int i = 0; i < n; i++)
{
_str[pos + 1] = _str[pos + len + 1];
pos++;
}
}
_str[pos + 1] = '\0';
_size = end;
_size -= len;
}
//find的模拟实现,从pos位置找一个字符,返回下标位置
int find(char ch, int pos = 0)
{
assert(pos < _size);
int i = 0;
while (ch != _str[i] && i < _size)
{
i++;
}
if (ch == _str[i])
{
return i;
}
else
return npos;
}
//find的模拟实现,找字符串
int find(const char* ch, int pos = 0)//abcde cd
{
assert(pos < _size);
int n = strlen(ch);
const char* ptr = strstr(_str, ch);//在_str中找字符串ch
if (ptr)
{
return ptr - _str;//返回一个整数
}
else
return npos;
}
//模拟实现substr,从pos位置开始取len个字符 "abcde" 2 2
string substr(int pos, int len)
{
string ss;
if (len == npos || len + pos > _size)
{
len = _size - pos;
}
for (int i = 0; i < len; i++)
{
ss += _str[pos + i];
}
return ss;
}
//拷贝构造
string(const string& s)
{
_str = new char[s._capacity + 1];
memcpy(_str, s._str, s._size + 1);//先扩容
_size = s._size;
_capacity = s._capacity;
}
//赋值=的模拟实现,深拷贝
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[s._capacity + 1];
memcpy(tmp, s._str, s._size + 1);
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
//resize的模拟实现,可以删除数据,也可以扩容,不缩容,当_capacity为15,_size为10时
//resize(8)就是删除数据(删除两个),resize(12)就是插入数据,resize(18)就是扩容
void resize(int n, char ch = '\0')
{
if (n < _size)
{
_size = n;
}
else
{
if (n > _capacity)
{
reserve(n);//不加if也行因为reserve会自行判断。
}
int k = n - _size;
for (int i = 0; i < k; i++)
{
_str[_size] = ch;
_size++;
}
}
//记得加\0
_str[n] = '\0';
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
//<<的模拟实现ostream&operator<<(ostream&out,const string&s),不能在类里
/*ostream& operator<<(ostream& out, const string& s)
{
}*/
//字符串比较,strcmp会有\0问题memcmp有别的问题 word和wordxxx,长的大
//三种情况
//hello hello
//helloxx hello
//hello helloxx
bool operator<(const string& s) const
{
int i1 = 0;
int i2 = 0;
//当i1<i1._size和i2<i2._size时的情况
while (i1 < _size && i2 < s._size)//左边的是this
{
if (_str[i1] < s._str[i2])
{
return true;
}
else if (_str[i1] > s._str[i2])
{
return false;
}
else
{
i1++;
i2++;
}
}
//如果没返回就是因为全都相等在_size的范围,小的_size的范围,也有可能相等
if (_size == s._size)//相等也不是小于,所以返回false
{
return false;
}
else if (_size < s._size)
{
return true;
}
else if (_size > s._size)
{
return false;
}
//return _size < s._size;简便方法,如果左边大于等于右边的长度则为假
//直接赋用,用memcmp或者strcmp,也只能判断相不相等,不能判断谁大谁小
//三种情况
//hello hello
//helloxx hello
//hello helloxx
//int ret = memcmp(_str, s._str, _size < s._size ? _size : s._size);//取小的那个比较
//
相等返回0,小于0就是左边小于右边,大于0就是右边大于左边
//return ret==0?_size<s._size:ret<0;//ret==0,代表小的_size范围内都相等,然后再看长度
翻译如下
//if (ret == 0 && _size == s._size)
//{
// return false;
//}
//if (ret != 0)
//{
// return _size < s._size;
//}
}
//字符串比较==
bool operator==(const string& s) const
{
return _size==s._size&&memcmp(_str, s._str,_size) == 0;//等于0 就是相等
}
//字符串<=
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);
}
//字符串>=
bool operator>=(const string& s) const//都加上const普通对象和const的对象都可以用,权限可以缩小
{
return !(*this < s);
}
private:
int _size;
int _capacity;
char* _str;
int npos = 11111111;
};
//<<的模拟实现ostream&operator<<(ostream&out,const string&s),不能在类里
ostream& operator<<( ostream& out, const string& s)
{
for (auto& e : s)//范围for需要调用迭代器,const sting就要调用const的迭代器
{
out << e;
}
return out;
}
//>>的模拟实现,流插入
istream& operator>>(istream& in, string& s)
{
s.clear();
char ch = in.get();
while (ch != ' ' && ch != '\n')//输入时有空格或者回车就结束了,输入的时候就是这样,要是想输入空格就需要用getline
{
s += ch;
ch = in.get();//get是用来读取下一个字符,会依次往后读
}
return in;
}
}
int main()
{
const ss::string s1("aaa");//加上一个const,c_str和size()后都需要加上const
ss:: string s2("baa");//s2不加const也可以打印,因为权限可以缩小不可以放大
cout << s1.c_str() << endl;
cout << s1.size() << endl;
cout << s2.c_str() << endl;
cout << s2.size() << endl;
//[]的操作符重载测试
cout << s1[1] << endl;
//迭代器的模拟实现
ss::string s3("abcde");
ss::string::iterator it = s3.begin();
while (it != s3.end())
{
cout << *it <<" ";
++it;
}
cout << endl;
for (auto ch : s3)
{
cout << ch << " " ;
}
cout << endl;
//尾插的模拟实现
ss::string s4("abcd");
s4.push_back('e');
cout << s4.c_str() << endl;
//插入字符串
ss::string s5("aaaaa");
s5.append("bbbb");
cout << s5.c_str() << endl;
//+=的操作符重载
ss::string s6("aaab");
s6+= "ccc";
cout << s6.c_str() << endl;
s6 += 'd';
cout << s6.c_str() << endl;
//insert的模拟实现
ss::string s7("aa");
s7.insert(0, 7, 'x');//在下标为一的位置插入7个'x'。
cout << s7.c_str() << endl;
//pos位置插入字符串
ss::string s8("abcd");
s8.insert(0, "kkkk");
cout << s8.c_str() << endl;
//erase的模拟实现,pos位置后删除len个字符
ss::string s9("abcdef");
s9.erase(1,2);
cout << s9.c_str() << endl;
//find的模拟实现,找字符
ss::string s10("ggc");
cout<<s10.find('c',2) << endl;
//find的模拟实现,找字符串
ss::string s11("abcde");
cout << s11.find("cd", 0)<<endl;
//模拟实现substr,从pos位置开始取len个字符
ss::string ss = s11.substr(s11.find("cd", 0), 2);
cout << ss.c_str() << endl;
ss::string s12("abcdef");
ss::string sss = s12.substr(0, 100);
cout << sss.c_str()<< endl;
//拷贝构造
ss::string s13(s12);
cout << s13.c_str() << endl;
//resize的模拟实现,可以删除数据,也可以扩容,不缩容,当_capacity为15,_size为10时
//resize(8)就是删除数据(删除两个),resize(12)就是插入数据,resize(18)就是扩容
ss::string s14("abcdefg");//7
s14.resize(10,'x');//第二个不传就是默认是'\0',这里的意思是删除数据
cout << s14.c_str() << endl;
//<<的模拟实现ostream&operator<<(ostream&out,const string&s)
ss::string s15("abcd");
cout << s15 << endl;
//>>的模拟实现,流插入
ss::string s16;
//cin >>s16;
//cout << s16 << endl;
//字符串比较,strcmp会有\0问题memcmp有别的问题 word和wordxxx,长的大
//三种情况
//hello hello
//helloxx hello
//hello helloxx
ss::string s17("helloxx");
ss::string s18("hello");
cout << (s17 != s18) << endl;
//赋值=的模拟实现,深拷贝
s18 = s17;
std::swap(s18, s15);//可以直接交换
cout << s18 << endl;
return 0;
}