string模拟实现
string类成员
#pragma once
#include <iostream>
#include <stdio.h>
#include <assert.h>
#include <string.h>
using namespace std;
namespace bit
{
//string类的声明
class string
{
public:
friend ostream& operator<<(ostream& _cout, const string& s);
friend istream& operator>>(istream& _cin, string& s);
public:
static const int npos;
private:
char* _str;//字符串数组的指针
size_t _capacity;//字符串数组的容量大小
size_t _size;//字符串数组中字符个数
};
构造函数
//【构造函数】
//因为string的使用可能是空字符串,或者是字符串
//string实际调用的时候,如果它的参数是空字符串,那么就是capacity和size初始化为0
//string如果实际调用,参数的字符串的时候,那么就将字符串的内容交给string,capacity与size也是
string(const char* str = "")
:_size(strlen(str))//将给定字符串的大小计算出来
{
_capacity = _size;//虽然此处的capacity与size大小一致,但是不用但心,扩容可以拉开距离
_str = new char[_capacity + 1];//此时str就按照capacity+1的大小进行扩容
//扩容的时候一般,要将扩容比实际情况的容量大1个数据,给\0多开出一个空间
strcpy(_str, str);//将将字符串拷贝到新开辟的空间里
}
析构函数
//【析构函数】
~string()
{
//析构函数就是将释放的空间归还,将capacity和size的大小重新更新为0
delete[] _str;
_str = nullptr;
_capacity = _size = 0;
}
c_str返回字符串
//只是单纯地返回字符串指针并不修改,所以就是const保护起来
const char* c_str() const
{
return _str;
}
size与capacity
size_t size() const
{
return _size;
}
size_t capacity() const
{
return _capacity;
}
operator[]
非const版本
char& operator[](int pos)
{
//判断此时形参传入的下标是否是合法的
assert(pos < _size);
//返回值:返回当前下标的相应数据
// h e l l o w o r l d size capacity
// 0 1 2 3 4 5 6 7 8 9 10 11
//str pos
//return *(_str + pos);
return _str[pos];
}
const版本
const char& operator[](int pos) const
{
//判断此时形参传入的下标是否是合法的
assert(pos <= _size);
//返回值:返回当前下标的相应数据
// h e l l o w o r l d size capacity
// 0 1 2 3 4 5 6 7 8 9 10 11
//str pos
//return *(_str + pos);
return _str[pos];
}
迭代器
typedef char* iterator;
typedef const char* iterator_const;
//非const
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
//const
iterator_const begin() const
{
return _str;
}
iterator_const end() const
{
return _str + _size;
}
扩容reserve函数
void reserve(size_t n)
{
//插入数据,先判断容量是否充足
if (_size >= _capacity)
{
//如果当前数组个数与容量的大小相等,容量不足,进行扩容
char* tmp = new char[n + 1];
//在实际进行开辟空间时,要多开出一个空间,用来存储"\0"
//如果扩容成功,那么就把新的交给它
if (tmp != nullptr)
{
strcpy(tmp, _str);
delete[]_str;
_str = tmp;
_capacity = n;
}
}
}
push_back尾插
void push_back(char ch)
{
//增加数据先进行扩容
//容量为0,手动为其开辟4字节
//否则就开辟一半
reserve(_capacity == 0 ? 4 : _capacity * 2);
//开辟好空间之后,尾插数据
// size capacity
// h e l l o w o r l d \0
// 0 1 2 3 4 5 6 7 8 9 10 11
//str pos
_str[_size] = ch;
_size++;
_str[_size] = '\0';
}
append追加字符串
//【append追加字符串】
void append(const char* str)
{
size_t len = strlen(str);
//插入数据,先进行判断并且进行扩容
if (_size + len > _capacity)
{
reserve(len + _size);
}
//开辟好空间之后,尾插数据
// size capacity
// h e l l o \0
// 0 1 2 3 4 5 6 7 8 9 10
// w o r l d
strcpy(_str + _size, str);
_size += len;
}
operator +=
//【operator +=】
// +=是字符串与 字符串/字符 相加,连接起来
// +=返回的仍是这个字符串
// s1+s2
// +=字符
string& operator+=(const char ch)
{
push_back(ch);
return *this;
}
// +=字符串
string& operator+=(const char* str)
{
append(str);
return *this;
}
operator = 赋值
//【operator =】赋值
// s1=s2
//都是string类型
string& operator=(const string& str)
{
// s1: h e l l o
// s2: w o r l d h h
// 将s2赋值给s1;
// 首先得保证s1与s2的大小容量相等
char* tmp = new char[str._capacity + 1];
//但是在手动扩容的时候要确保capacity是有足够的空间来进行存\0的
_str = tmp;
strcpy(_str, str._str);
_size = str._size;
return *this;
}
insert
//【insert】
//在pos位置插入ch字符
string& insert(size_t pos,char ch)
{
assert(pos <= _size);
// size capacity
// h e l l o w o r l d \0
// 0 1 2 3 4 5 6 7 8 9 10 11
// x end
//在pos位置插入ch,先给ch腾出地方
reserve(_capacity == 0 ? 4 : _capacity * 2);
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = ch;
_size++;
return *this;
}
//【自己的版本】
在pos位置插入str字符串
//string& insert(size_t pos, const char* str)
//{
// // size capacity
// // h e l l o w o r l d \0
// // 0 1 2 3 4 5 6 7 8 9 10 11
// // x x x end
// //在pos位置插入ch,先给str腾出地方
// assert(pos <= _size);
// size_t len = strlen(str);
// if (_size + len >= _capacity)
// {
// reserve(_size + len);
// }
// size_t end = _size + pos + 1;
// while (end > pos)
// {
// _str[end] = _str[end - 1 - pos];
// end--;
// }
// int i = 0; int tmp = (int)len;
// while (tmp)
// {
// _str[pos++] = str[i++];
// tmp--;
// }
// _size = _size + len;
//
// return *this;
//}
//【杭哥版本】
//在pos位置插入str字符串
string& insert(size_t pos, const char* str)
{
// size capacity
// h e l l o w o r l d \0
// 0 1 2 3 4 5 6 7 8 9 10 11
// x x x end
//在pos位置插入ch,先给str腾出地方
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len >= _capacity)
{
reserve(_size + len);
}
//挪动位置,为其str腾出地方
size_t end = _size + pos;//这里认为size与pos都是大于0的
while (end > pos + len)
{
_str[end] = _str[end - pos];
end--;
}
/*strcpy(_str + pos, str);
注意:strcpy会将字符串中的\0,也拷贝进去,此时就不能实现插入的问题了*/
strncpy(_str + pos, str, len);
_size = _size + len;
return *this;
}
erase
//【erase】函数
string& erase(size_t pos, size_t len = npos)
{
// size capacity
// h l o w o r l d \0
// 0 1 2 3 4 5 6 7 8 9 10 11
// pos=2,len=2
assert(pos < _size);
if (len == npos || len + pos > _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
拷贝构造S1(S2)
//拷贝构造S1(S2)
string(const string& s)
{
string tmp(s._str);
swap(tmp);
}
swap
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
find
//找到对应位置的下标
size_t find(char ch, size_t pos = 0) const
{
assert(pos < _size);
//找下标,遍历找到哪个位置上的元素与当前元素一致
int i = 0;
for (i = 0; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char* sub, size_t pos = 0) const
{
assert(pos < _size);
//找字符串
//找字符串1中包含字符串2的位置
//
char* tmp = strstr(_str + pos, sub);
if (tmp)//如果能找到子串的开始,为真
{
return tmp - _str;
//为真表示找到了,就返回此时对应位置与其str字符串开始为0
//对应的下标
}
else
{
return npos;
}
}
分割字符串
//分割字符串
//在pos位置,切割出对应的长度len的大小
string substr(size_t pos = 0, size_t len = npos)
{
//pos+len>=_size||len==npos
// 超过了string的大小
//此时说明需要将所有的数据都进行拷贝过来
//如果pos+len<_size
// 在string的内部
//就说明此时只需要将len长度的字符串拷贝
string sub;
if (len >= _size - pos)
{
for (size_t i = pos; i < _size; i++)
{
sub += _str[i];
}
}
else
{
for (size_t i = pos; i < pos+len; i++)
{
sub += _str[i];
}
}
return sub;
}
clear
//清空字符串
void clear()
{
_size = 0;
_str[0] = '\0';
}
运算符重载
operator<<
const int string::npos = -1;
ostream& operator<<(ostream& _cout,const string& s)
{
//【错误写法】
/*for (auto e : s)
{
_cout << s;
}*/
for (int i = 0; i < (int)s.size(); i++)
{
_cout << s[i];
}
return _cout;
}
operator==
bool operator==(const string& s1, const string& s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return ret == 0;
}
operator<
bool operator<(const string& s1, const string& s2)
{
int ret = strcmp(s1.c_str(), s2.c_str());
return ret < 0;
}
operator<=
bool operator<=(const string& s1, const string& s2)
{
return (s1 == s2) || (s1 < s2);
}
operator>
bool operator>(const string& s1, const string& s2)
{
return !(s1 <= s2);
}
operator>=
bool operator>=(const string& s1, const string& s2)
{
return !(s2 < s1);
}
operator!=
bool operator!=(const string& s1, const string& s2)
{
return !(s1 == s2);
}
operator>>
istream& operator>>(istream& _cin, string& s)
{
//string的本质是字符串==》字符数组
//将string的s字符串看做好多个字符
//那么此时就可以利用+=运算符重载
//起初假设这个数组的大小是固定的
//先一点一点进行输入
//最终的数组
char buff[128] = { '\0' };
size_t i = 0;
char ch = _cin.get();//也就是看为字符
while (ch != ' ' && ch != '\n')
{
if (i < 127)//不能=127,因为多一个位置需要留给\0
{
buff[i++] = ch;
}
else
{
//此时容量不够了,那么就将满了的交给目标的,然后重新啊开始
s += buff;
i = 0;
buff[i++] = ch;
}
ch = _cin.get();
}
if (i > 0)
{
buff[i] = '\0';
//此时没有满,但是遇到‘ ’‘\0’
s += buff;
}
return _cin;
}
完整代码
string.h文件
#pragma once
#include <iostream>
#include <stdio.h>
#include <assert.h>
#include <string.h>
using namespace std;
namespace bit
{
//string类的声明
class string
{
public:
friend ostream& operator<<(ostream& _cout, const string& s);
friend istream& operator>>(istream& _cin, string& s);
//【构造函数】
//因为string的使用可能是空字符串,或者是字符串
//string实际调用的时候,如果它的参数是空字符串,那么就是capacity和size初始化为0
//string如果实际调用,参数的字符串的时候,那么就将字符串的内容交给string,capacity与size也是
string(const char* str = "")
:_size(strlen(str))//将给定字符串的大小计算出来
{
_capacity = _size;//虽然此处的capacity与size大小一致,但是不用但心,扩容可以拉开距离
_str = new char[_capacity + 1];//此时str就按照capacity+1的大小进行扩容
//扩容的时候一般,要将扩容比实际情况的容量大1个数据,给\0多开出一个空间
strcpy(_str, str);//将将字符串拷贝到新开辟的空间里
}
//【析构函数】
~string()
{
//析构函数就是将释放的空间归还,将capacity和size的大小重新更新为0
delete[] _str;
_str = nullptr;
_capacity = _size = 0;
}
//【c_str返回字符串】
//只是单纯地返回字符串指针并不修改,所以就是const保护起来
const char* c_str() const
{
return _str;
}
//【size】
size_t size() const
{
return _size;
}
size_t capacity() const
{
return _capacity;
}
//【operator[]】下标引用运算符重载
//可以根据下标引用操作符,开修改当前数据的,所以此时可以是非const,也可以是const仅仅读取
//非const版本:
char& operator[](int pos)
{
//判断此时形参传入的下标是否是合法的
assert(pos < _size);
//返回值:返回当前下标的相应数据
// h e l l o w o r l d size capacity
// 0 1 2 3 4 5 6 7 8 9 10 11
//str pos
//return *(_str + pos);
return _str[pos];
}
//const版本:
const char& operator[](int pos) const
{
//判断此时形参传入的下标是否是合法的
assert(pos <= _size);
//返回值:返回当前下标的相应数据
// h e l l o w o r l d size capacity
// 0 1 2 3 4 5 6 7 8 9 10 11
//str pos
//return *(_str + pos);
return _str[pos];
}
//【迭代器】:类似指针
//假设就是简单的指针
typedef char* iterator;
typedef const char* iterator_const;
//非const
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
//const
iterator_const begin() const
{
return _str;
}
iterator_const end() const
{
return _str + _size;
}
//扩容reserve函数
void reserve(size_t n)
{
//插入数据,先判断容量是否充足
if (_size >= _capacity)
{
//如果当前数组个数与容量的大小相等,容量不足,进行扩容
char* tmp = new char[n + 1];
//在实际进行开辟空间时,要多开出一个空间,用来存储"\0"
//如果扩容成功,那么就把新的交给它
if (tmp != nullptr)
{
strcpy(tmp, _str);
delete[]_str;
_str = tmp;
_capacity = n;
}
}
}
//【push_back尾插】
//尾插的时候,向字符串的末尾插入一个字符串,并且返回这个字符串的起始位置
void push_back(char ch)
{
//增加数据先进行扩容
//容量为0,手动为其开辟4字节
//否则就开辟一半
reserve(_capacity == 0 ? 4 : _capacity * 2);
//开辟好空间之后,尾插数据
// size capacity
// h e l l o w o r l d \0
// 0 1 2 3 4 5 6 7 8 9 10 11
//str pos
_str[_size] = ch;
_size++;
_str[_size] = '\0';
}
//【append追加字符串】
void append(const char* str)
{
size_t len = strlen(str);
//插入数据,先进行判断并且进行扩容
if (_size + len > _capacity)
{
reserve(len + _size);
}
//开辟好空间之后,尾插数据
// size capacity
// h e l l o \0
// 0 1 2 3 4 5 6 7 8 9 10
// w o r l d
strcpy(_str + _size, str);
_size += len;
}
//【operator +=】
// +=是字符串与 字符串/字符 相加,连接起来
// +=返回的仍是这个字符串
// s1+s2
// +=字符
string& operator+=(const char ch)
{
push_back(ch);
return *this;
}
// +=字符串
string& operator+=(const char* str)
{
append(str);
return *this;
}
//【operator =】赋值
// s1=s2
//都是string类型
string& operator=(const string& str)
{
// s1: h e l l o
// s2: w o r l d h h
// 将s2赋值给s1;
// 首先得保证s1与s2的大小容量相等
char* tmp = new char[str._capacity + 1];
//但是在手动扩容的时候要确保capacity是有足够的空间来进行存\0的
_str = tmp;
strcpy(_str, str._str);
_size = str._size;
return *this;
}
//【insert】
//在pos位置插入ch字符
string& insert(size_t pos, char ch)
{
assert(pos <= _size);
// size capacity
// h e l l o w o r l d \0
// 0 1 2 3 4 5 6 7 8 9 10 11
// x end
//在pos位置插入ch,先给ch腾出地方
reserve(_capacity == 0 ? 4 : _capacity * 2);
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = ch;
_size++;
return *this;
}
//【自己的版本】
在pos位置插入str字符串
//string& insert(size_t pos, const char* str)
//{
// // size capacity
// // h e l l o w o r l d \0
// // 0 1 2 3 4 5 6 7 8 9 10 11
// // x x x end
// //在pos位置插入ch,先给str腾出地方
// assert(pos <= _size);
// size_t len = strlen(str);
// if (_size + len >= _capacity)
// {
// reserve(_size + len);
// }
// size_t end = _size + pos + 1;
// while (end > pos)
// {
// _str[end] = _str[end - 1 - pos];
// end--;
// }
// int i = 0; int tmp = (int)len;
// while (tmp)
// {
// _str[pos++] = str[i++];
// tmp--;
// }
// _size = _size + len;
//
// return *this;
//}
//【杭哥版本】
//在pos位置插入str字符串
string& insert(size_t pos, const char* str)
{
// size capacity
// h e l l o w o r l d \0
// 0 1 2 3 4 5 6 7 8 9 10 11
// x x x end
//在pos位置插入ch,先给str腾出地方
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len >= _capacity)
{
reserve(_size + len);
}
//挪动位置,为其str腾出地方
size_t end = _size + pos;//这里认为size与pos都是大于0的
while (end > pos + len)
{
_str[end] = _str[end - pos];
end--;
}
/*strcpy(_str + pos, str);
注意:strcpy会将字符串中的\0,也拷贝进去,此时就不能实现插入的问题了*/
strncpy(_str + pos, str, len);
_size = _size + len;
return *this;
}
//【erase】函数
string& erase(size_t pos, size_t len = npos)
{
// size capacity
// h l o w o r l d \0
// 0 1 2 3 4 5 6 7 8 9 10 11
// pos=2,len=2
assert(pos < _size);
if (len == npos || len + pos > _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
//拷贝构造S1(S2)
string(const string& s)
{
string tmp(s._str);
swap(tmp);
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
//找到对应位置的下标
size_t find(char ch, size_t pos = 0) const
{
assert(pos < _size);
//找下标,遍历找到哪个位置上的元素与当前元素一致
int i = 0;
for (i = 0; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char* sub, size_t pos = 0) const
{
assert(pos < _size);
//找字符串
//找字符串1中包含字符串2的位置
//
char* tmp = strstr(_str + pos, sub);
if (tmp)//如果能找到子串的开始,为真
{
return tmp - _str;
//为真表示找到了,就返回此时对应位置与其str字符串开始为0
//对应的下标
}
else
{
return npos;
}
}
//分割字符串
//在pos位置,切割出对应的长度len的大小
string substr(size_t pos = 0, size_t len = npos)
{
//pos+len>=_size||len==npos
// 超过了string的大小
//此时说明需要将所有的数据都进行拷贝过来
//如果pos+len<_size
// 在string的内部
//就说明此时只需要将len长度的字符串拷贝
string sub;
if (len >= _size - pos)
{
for (size_t i = pos; i < _size; i++)
{
sub += _str[i];
}
}
else
{
for (size_t i = pos; i < pos + len; i++)
{
sub += _str[i];
}
}
return sub;
}
//清空字符串
void clear()
{
_size = 0;
_str[0] = '\0';
}
public:
static const int npos;
private:
char* _str;//字符串数组的指针
size_t _capacity;//字符串数组的容量大小
size_t _size;//字符串数组中字符个数
};
const int string::npos = -1;
ostream& operator<<(ostream& _cout, const string& s)
{
//【错误写法】
/*for (auto e : s)
{
_cout << s;
}*/
for (int i = 0; i < (int)s.size(); i++)
{
_cout << s[i];
}
return _cout;
}
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 !(s2 < s1);
}
bool operator!=(const string& s1, const string& s2)
{
return !(s1 == s2);
}
istream& operator>>(istream& _cin, string& s)
{
//string的本质是字符串==》字符数组
//将string的s字符串看做好多个字符
//那么此时就可以利用+=运算符重载
//起初假设这个数组的大小是固定的
//先一点一点进行输入
//最终的数组
char buff[128] = { '\0' };
size_t i = 0;
char ch = _cin.get();//也就是看为字符
while (ch != ' ' && ch != '\n')
{
if (i < 127)//不能=127,因为多一个位置需要留给\0
{
buff[i++] = ch;
}
else
{
//此时容量不够了,那么就将满了的交给目标的,然后重新啊开始
s += buff;
i = 0;
buff[i++] = ch;
}
ch = _cin.get();
}
if (i > 0)
{
buff[i] = '\0';
//此时没有满,但是遇到‘ ’‘\0’
s += buff;
}
return _cin;
}
void test1()
{
string s1("hello\0");
cout << s1.c_str() << endl;
string s2;
cout << s2.c_str() << endl;
cout << s1.size() << endl;
}
//测试[]重载
void test2_index()
{
//非const
string s3("hello bit");
for (int i = 0; i < s3.size(); i++)
{
cout << s3[i];
}
cout << endl;
//const
const string s4("hello anyue");
for (int i = 0; i < s4.size(); i++)
{
cout << s4[i];
}
cout << endl;
}
//测试迭代器
void test_iterator()
{
//非const
string s5("hello hello bit");
//迭代器就是类似指针,所以让返回值为char*指针
string::iterator it5 = s5.begin();
while (it5 != s5.end())
{
cout << *it5;
it5++;
}
cout << endl;
//const
const string s6("hello hello bittt");
//迭代器就是类似指针,所以让返回值为char*指针
string::iterator_const it6 = s6.begin();
while (it6 != s6.end())
{
cout << *it6;
it6++;
}
cout << endl;
}
//范围for
//此时的范围for并未实现,但是仍然能够运行
//范围for的底层就是iterator,就是将语句替换为了迭代器
void test_for_auto()
{
string s7("hello hello bit");
for (auto e1 : s7)
{
cout << e1;
}
cout << endl;
string s8("hello hello bityyy");
for (auto e2 : s8)
{
cout << e2;
}
cout << endl;
}
//测试push_back尾插
void test_push_back()
{
string s9("hello");
s9.push_back('a');
cout << s9.c_str() << endl;
s9.push_back('b');
cout << s9.c_str() << endl;
s9.push_back('c');
cout << s9.c_str() << endl;
s9.push_back('d');
cout << s9.c_str() << endl;
}
//测试append追加字符串
void test_append()
{
string s10("hello");
s10.append(" anyue1314");
cout << s10.c_str() << endl;
}
//测试+=操作符
void test_operator_AddEqual()
{
//+=字符串
string s11("abcdef");
s11 += "12345";
cout << s11.c_str() << endl;
//+=字符
string s12("abcdef");
s12 += 'z';
cout << s12.c_str() << endl;
}
//测试=操作符
void test_assignment()
{
//+=字符串
string s13("abcdef");
s13 = "12345";
cout << s13.c_str() << endl;
//+=字符
string s14("abcdef");
s14 = "z";
cout << s14.c_str() << endl;
}
//测试insert在pos位置插入,一个字符
void test_insert()
{
string s15("abcdef");
s15.insert(2, '6');
cout << s15.c_str() << endl;
string s16("abcdef");
s16.insert(2, "xxx");
cout << s16.c_str() << endl;
}
//测试erase,从pos位置开始删除len个字符
void test_erase()
{
string s17("abcdef");
s17.erase(2);
cout << s17.c_str() << endl;
string s18("abcdef");
s18.erase(2, 1);
cout << s18.c_str() << endl;
}
//测试输入输出流<<>>重载
void test_iostream()
{
string s19("abcdeffff");
cout << s19 << endl;
string s20;
cin >> s20;
cout << s20 << endl;
}
void test_stringcpy()
{
string s19("abcdeffff");
cout << s19 << endl;
string s20(s19);
cout << s20 << endl;
}
void test_swap()
{
string s19("abcdeffff");
cout << s19 << endl;
string s20("xxxx");
cout << s20 << endl;
swap(s19, s20);
cout << s19 << endl;
cout << s20 << endl;
}
/*void test_find()
{
string s19("abcdeffff");
char a = 'd';
size_t ret = s19.find('d', 0);
cout << ret << endl;
}*/
void test_find()
{
string s19("abcdeffff");
const char* sub = "def";
size_t ret = s19.find(sub, 0);
cout << ret << endl;
}
void test_substr()
{
string s19("abcdeffff");
string s20 = s19.substr(3, 3);
cout << s20 << endl;
string s22 = s19.substr(0);
cout << s22 << endl;
string s23 = s19.substr(3, 8);
cout << s23 << endl;
}
void test_clear()
{
string s19("abcdeffff");
cout << s19 << endl;
s19.clear();
cout << s19 << endl;
}
void test_iostream_()
{
string s19("abcdeffff");
cout << s19 << endl;
string s20("a");
cout << s20 << endl;
if (s19 > s20)
{
cout << "dayu" << endl;
}
}
}
stringtest.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "string.h"
int main()
{
bit::test1();
bit::test2_index();
bit::test_iterator();
bit::test_for_auto();
bit::test_push_back();
bit::test_append();
bit::test_operator_AddEqual();
bit::test_assignment();
bit::test_erase();
bit::test_iostream();
bit::test_insert();
bit::test_stringcpy();
bit::test_swap();
bit::test_find();
bit::test_substr();
bit::test_clear();
bit::test_iostream_();
return 0;
}