我们要自己开一个命名空间,以便与库中string做区分
要利用C语言中的字符串函数模拟实现string
构造函数
无参初始化不能将对象直接置为nullptr,要给一个'\0'
"" 空字符串 自带一个\0
" " 带有一个空格的字符串
'\0' 字符
"\0' 字符串 有两个\0
常量字符串默认第一个\0
初始化要注意类型匹配
析构函数
c_str
返回数据
c_size
返回数据个数
operator[ ](0.45)
两个版本
当两个都存在时,编译器会走最匹配的那一个
iterator(迭代器)(0.50)
两种形式
写成内部类或者typedef(指针),在string中iterator就是指针
begin第一个数据的位置,end最后一个数据的下一个位置
范围for
实现了迭代器就实现了范围for
编译器会盲目地把它替换成范围for,因此实现不能随意更改名字
1.05
push_back(尾插)
append
reverse反转
reserve 保留,扩容
operator+=
insert(2.00)
在一个运算符两边,当两个操作数不是同一个类型时,会发生类型提升,通常是范围小的向范围大的提升
类型范围小的与类型范围大的做比较时,会进行隐式类型转换,小转向大的
从第0个位置插入
传参时会不会发生类型提升
erase(2.19)
find
strstr碰到'\0'会不会终止,
substr(提取子串)
ostream(流插入)<<
防拷贝
有多少打多少,而不是遇到\0就终止
string是遇到_sz终止,而不是\0
istream>>(流提取)
比较大小
结束的\0不应该参与比较,大的那个是汉字就不行
写一个比较,就能引用所有比较
赋值拷贝
传统写法
现代写法
自定义类型不能用swap
引用计数和写时拷贝
浅拷贝两大问题
1.多次析构(引用计数)
2.一个改变影响另外的对象改变(写的时候引用计数如果不是1,则进行深拷贝,再修改)
引用对象代表有几个对象指向同一快空间,解决析构问题,先--,再判断,当引用计数为0时,表示已经没有人管理这块空间,那么将该空间释放。
最后一个走的人关灯。
Linux下就运用了这个技术
vscode2019下,就没有使用这种技术
vs弄了一个buf数组,有16个字节,能存储15个字符,还有一个给'\0'.
当然,字符串过大的时候会存在空间浪费,它是不会分开存的
size_t,32位下,4字节 64位下,8字节
string模拟实现代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<assert.h>
#include<string.h>
using namespace std;
//与库里面的string做区分
namespace bit {
class string {
public:
friend ostream& operator <<(ostream& out, const string& s);
friend istream& operator >>(istream& cin, bit::string& s);
typedef char* iterator;
typedef const char* const_iterator;
string(const char* s = "")
{
_size = strlen(s);
_capacity = _size;
_str = new char[_size + 1];
memcpy(_str, s, _size);
_str[_size] = '\0';
}
void swap(string& str)
{
std::swap(_str, str._str);
std::swap(_capacity, str._capacity);
std::swap(_size, str._size);
}
//现代写法有\0无法全部拷贝,hello\0world
// 传统写法可以全部拷贝
//不走初始化列表,编译器对内置类型不做处理,可能会出现随机值
string(const string& s)
:_size(0)
,_capacity(0)
,_str(nullptr)
{
/*
拷贝构造*this还没有给空间,用reserve有delete会直接报错
reserve(s._capacity);
for (int i = 0;i < s._size;i++)
{
_str[i] = s._str[i];
}
_size = s._size;
_str[_size] = '\0';*/
_str = new char[s._capacity + 1];
//strcpy(_str, s._str);
memcpy(_str, s._str, s._size + 1);
_size = s._size;
_capacity = s._capacity;
_str[_size] = '\0';
/*bit::string tmp(s._str);
swap(tmp);*/
}
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
const char* c_str()const
{
return _str;
}
size_t c_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];
}
iterator begin()
{
return _str;
}
const_iterator begin()const
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator end()const
{
return _str + _size;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
void reserve(size_t n = 0)
{
if (n> _capacity)
{
char* tmp = new char[n+ 1];
memcpy(tmp, _str, _size+1);
delete[]_str;
_str = tmp;
_capacity = n ;
}
}
void resize(size_t n, char c = '\0')
{
if (n < _size)
{
_size = n;
_str[_size] = '\0';
}
else
{
reserve(n);
for(size_t i=_size;i<n;i++)
{
_str[i] = c;
}
_size = n;
_str[_size] = '\0';
}
}
string& push_back(char c)
{
if (_size == _capacity)
{
//2倍扩容
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = c;
_size += 1;
_str[_size] = '\0';
return *this;
}
string& append(const char* s)
{
size_t len = strlen(s);
if(_size+len>_capacity)
//二倍扩容可能还是不够,所以需要扩到len+_size
reserve(len+_size);
/*for (int i = 0;i < len;i++)
{
_str[_size+i] = s[i];
}*/
memcpy(_str + _size, s, len + 1);
_size += len;
return *this;
}
string& operator+=(char c)
{
return push_back(c);
}
string& operator+=(const char* s)
{
return append(s);
}
string& insert(size_t pos, size_t n, char c)
{
//在string中可以在_size位置插入,相当于尾插
assert(pos <= _size);
if (_size + n > _capacity)
{
reserve(n+_size);
}
size_t end = _size;
//防止在第0个位置插入时出错,陷入死循环,因为这里会发生类型提升
while (end >= pos && end != npos)
{
_str[end + n] = _str[end];
//用前置--减少拷贝
--end;
}
for (size_t i = pos;i < pos + n;i++)
{
_str[i] = c;
}
_size += n;
_str[_size] = '\0';
return *this;
}
string& insert(size_t pos, const char* s)
{
size_t len = strlen(s);
assert(pos <= _size);
if (_size + len > _capacity)
{
reserve(len+_size);
}
size_t end = _size;
while (end >= pos && end != npos)
{
_str[end + len] = _str[end];
--end;
}
for (size_t i = pos;i < pos + len;i++)
{
_str[i] = *s;
s++;
}
_size += len;
_str[_size] = '\0';
return *this;
}
string& erase(size_t pos = 0, size_t len = npos)
{
assert(pos <= _size);
//全删
if (_size - pos < len)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t end = pos + len;
while (end <= _size)
{
_str[pos++] = _str[end++];
}
_size -= len;
//_str[_size] = '\0';
}
return *this;
}
size_t find(const char* s, size_t pos = 0) const
{
assert(pos < _size);
char* ptr= strstr(_str + pos, s);
if (ptr)
{
return ptr - _str;
}
else
{
//找不到库里面返回的是npos
return npos;
}
//return nnpos - _str;
}
size_t find(char c, size_t pos = 0) const
{
assert(pos < _size);
for (size_t i = pos;i < _size;i++)
{
if (_str[i] == c)
{
return i;
}
}
return npos;
}
string substr(size_t pos = 0, size_t len = npos) const
{
assert(pos < _size);
size_t n = len;
//先判断len,如果后判断,当len=npos时会溢出
if (len == npos || len + pos > _size)
{
n = _size - pos;
}
bit::string Tmp;
//n就是你需要取的数据个数,也就是你要开辟的空间大小
Tmp.reserve(n);
Tmp._size = n;
memcpy(Tmp._str, _str + pos, n);
Tmp._str[Tmp._size] = '\0';
return Tmp;
}
int operator<(const string&str)
{
int Bool = memcmp(_str, str._str, _size > str._size ? str._size:_size);
/*if (Bool == 0)
{
if (_size < str._size)
{
return 1;
}
else
{
return 0;
}
}*/
return Bool == 0 ? _size < str._size: 0;
}
int operator==(const string& str)
{
return _size == str._size && !memcmp(_str, str._str, _size);
}
int operator<=(const string& str)
{
return *this< str||*this==str;
}
int operator>(const string& str)
{
return !(*this<= str);
}
int operator>=(const string& str)
{
return !(*this <str);
}
int operator!=(const string& str)
{
return !(*this == str);
}
string& operator=(string Tmp)
{
swap(Tmp);
return *this;
}
private:
size_t _size;
size_t _capacity;
char* _str;
public:
//声明
const static size_t npos;
};
//定义
const size_t string::npos = -1;
ostream& operator <<(ostream& cout, const bit::string& s)
{
//用for循环或者范围for
for (int i=0;i<s._size;i++)
{
cout << s._str[i];
}
return cout;
}
istream& operator >>(istream& cin, bit::string& s)
{
s.clear();
char ch = cin.get();
// 处理前缓冲区前面的空格或者换行
while (ch== ' ' || ch == '\n')
{
ch = cin.get();
}
char charr[127];
memset(charr, 127, '\0');
int i = 0;
while (ch != ' '&&ch != '\n')
{
charr[i++] = ch;
ch = cin.get();
if (i == 126)
{
charr[i] = '\0';
s += charr;
i = 0;
}
}
if (i != 0)
{
charr[i] = '\0';
s += charr;
}
return cin;
}
}
测试样例
#define _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
void test_string1()
{
bit::string s1("hello world");
cout << s1.c_str() << endl;
bit::string s2;
cout << s2.c_str() << endl;
for (size_t i = 0; i < s1.c_size(); i++)
{
s1[i]++;
}
cout << endl;
for (size_t i = 0; i < s1.c_size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
const bit::string s3("hello world");
s3[0];
auto cit = s3.begin();
while (cit != s3.end())
{
//*cit += 1;
cout << *cit << " ";
++cit;
}
cout << endl;
bit::string::iterator it = s1.begin();
while (it != s1.end())
{
*it += 1;
cout << *it << " ";
++it;
}
cout << endl;
//名字只能是end和begin
for (auto ch : s1)
{
cout << ch << " ";
}
cout << endl;
}
void test_string2()
{
bit::string s1("hello world");
cout << s1.c_str() << endl;
s1.push_back(' ');
s1.push_back('#');
s1.append("hello bit");
cout << s1.c_str() << endl;
bit::string s2("hello world");
cout << s2.c_str() << endl;
s2 += ' ';
s2 += '#';
s2 += "hello bit";
cout << s2.c_str() << endl;
}
void test_string3()
{
bit::string s1("helloworld");
cout << s1.c_str() << endl;
s1.insert(5, 3, '#');
cout << s1.c_str() << endl;
s1.insert(0, 3, '#');
cout << s1.c_str() << endl;
bit::string s2("helloworld");
s2.insert(5, "%%%%%");
cout << s2.c_str() << endl;
}
void test_string4()
{
bit::string s1("helloworld");
cout << s1.c_str() << endl;
s1.erase(5, 3);
cout << s1.c_str() << endl;
s1.erase(5, 30);
cout << s1.c_str() << endl;
s1.erase(2);
cout << s1.c_str() << endl;
}
void test_string5()
{
// 21:18继续
bit::string url = "ftp://www.baidu.com/?tn=65081411_1_oem_dg";
size_t pos1 = url.find("://");
if (pos1 != bit::string::npos)
{
bit::string protocol = url.substr(0, pos1);
cout << protocol.c_str() << endl;
}
size_t pos2 = url.find('/', pos1 + 3);
if (pos2 != bit::string::npos)
{
bit::string domain = url.substr(pos1 + 3, pos2 - (pos1 + 3));
bit::string uri = url.substr(pos2 + 1);
cout << domain.c_str() << endl;
cout << uri.c_str() << endl;
}
}
void test_string6()
{
bit::string s("hello world");
s.resize(8);
cout << s.c_str() << endl;
cout << s << endl;
s.resize(13, 'x');
cout << s.c_str() << endl;
cout << s << endl;
s.resize(20, 'y');
cout << s.c_str() << endl;
cout << s << endl;
/*c的字符数组,以\0为终止算长度
string不看\0,以size为终止算长度*/
bit::string ss("hello world");
ss += '\0';
ss += "!!!!!!!!!!";
cout << ss.c_str() << endl;
cout << ss << endl;
bit::string copy(ss);
cout << ss << endl;
cout << copy << endl;
ss += "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
cout << ss << endl;
}
void test_string7()
{
bit::string s;
cin >> s;
cout << s << endl;
cin >> s;
cout << s << endl;
//char buff[128];
//for (size_t i = 0; i < 5; i++)
//{
// cin >> buff[i];
//}
//for (size_t i = 0; i < 5; i++)
//{
// cout << buff[i] << endl;
//}
}
void test_string8()
{
/*string s1("bb");
string s2("aaa");
cout << (s1 < s2) << endl;*/
bit::string s1("hello");
bit::string s2("hello");
cout << (s1 < s2) << endl;
cout << (s1 > s2) << endl;
cout << (s1 == s2) << endl << endl;
bit::string s3("hello");
bit::string s4("helloxxx");
cout << (s3 < s4) << endl;
cout << (s3 > s4) << endl;
cout << (s3 == s4) << endl << endl;
bit::string s5("helloxxx");
bit::string s6("hello");
cout << (s5 < s6) << endl;
cout << (s5 > s6) << endl;
cout << (s5 == s6) << endl << endl;
}
void test_string9()
{
bit::string str1("hello");
str1 += '\0';
str1 += " world";
bit::string str2("how are");
str2 += '\0';
str2+= " you";
cout << str1 << endl;
cout << str2 << endl;
//传统写法可以全部拷贝,现代写法无法全部拷贝
str1 = str2;
cout << str1 << endl;
}
//void test_string9()
//{
// bit::string s1("hello");
// bit::string s2(s1);
//
// cout << s1 << endl;
// cout << s2 << endl;
//
// bit::string s3("xxxxxxxxxxxxx");
// 先调用拷贝构造,再调用赋值
// s1 = s3;
//
// cout << s1 << endl;
// cout << s3 << endl;
//}
int main()
{
test_string9();
//bit::string s("hello world");
//cout << s.c_str() << endl;
/*bit::string s("hello world");
cout << s << endl;*/
//s.insert(0, 3,'x');
//cout << s.c_str() << endl;
return 0;
}