string类实现和测试

#include
#include
using namespace std;
namespace bit
{
class string
{
friend ostream& operator<<(ostream& _cout, const bit::string& s);
friend istream& operator>>(istream& _cin, bit::string& s);
public:
typedef char* iterator;
public:
string()
:_str(new char[16]), _size(0), _capacity(15)
{
_str[_size] = 0;
}
string(const char* str = “”)
:_str(new char[strlen(str)+1]), _size(strlen(str)), _capacity(0)
{
strcpy(_str, str);
_capacity = _size;
}
template
void swap(T& a, T& b)
{
T c = a;
a = b;
b = c;
}
void Swap(string& str)
{
swap(_str, str._str);
swap(_size, str._size);
swap(_capacity, str._capacity);
}
string(const string& s)
{
string tmp(s._str);
Swap(tmp);
}
string& operator=(const string& s)
{
string tmp(s._str);
Swap(tmp);
}
~string()
{
if (_str)
{
delete[]_str;
_size = 0;
_capacity = 0;
}
}
//
// iterator
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
/
// modify
void PushBack(char c)
{
insert(_size, c);
}
string& operator+=(char c)
{
insert(_size, c);
return this;
}
void Append(const char
str)
{
insert(_size, str);
}
string& operator+=(const char* str)
{
Append(str);
return this;
}
void clear()
{
erase(0, _size);
}
const char
c_str()const
{
return _str;
}
/
// capacity
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool empty()const
{
return _size == 0;
}
void resize(size_t newSize, char c = ‘\0’)
{
if (newSize > _capacity)
{
reserve(newSize);
}
if (newSize > _size)
{
memset(_str + _size, c, newSize-_size);
}
_size = newSize;
_str[_size] = ‘\0’;
}
void pop_back()
{
erase(_size - 1, 1);
}
void reserve(size_t newCapacity)
{
if (newCapacity > _capacity)
{
char* tmp = new char[newCapacity+1];
strcpy(tmp,_str);
delete[]_str;
_str = tmp;
_capacity = newCapacity;
}
}
/
// access
char& operator[](size_t index)
{
if (index < _size)
return _str[index];
}
const char& operator[](size_t index)const
{
if (index < _size)
return _str[index];
}
/
//relational operators
bool operator<(const string& s)
{
return !(*this >= s);
}
bool operator<=(const string& s)
{
return *this < s || this == s;
}
bool operator>(const string& s)
{
return strcmp(_str, s._str)>0;
}
bool operator>=(const string& s)
{
return this > s || this == s;
}
bool operator==(const string& s)
{
return strcmp(s._str, _str) == 0;
}
bool operator!=(const string& s)
{
return !(this == s);
}
// 返回c在string中第一次出现的位置
size_t find(char c, size_t pos = 0) const
{
for (int i = 0; i < _size; i++)
{
if (_str[i] == c)
{
return i;
}
}
return -1;
}
// 返回子串s在string中第一次出现的位置
size_t find(const char
s, size_t pos = 0) const
{
char
p = strstr(_str, s);
if §
{
return _str - p;
}
return -1;
}
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
void insert(size_t pos, char c)
{
if (pos > _size)
return;
if (_size + 1 > _capacity)
{
int newc = _capacity == 0 ? 15 : _capacity * 2;
reserve(newc);
}
int end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
end–;
}
_str[pos] = c;
_size++;
}
void insert(size_t pos, const char
str)
{
if (pos > _size)
return;
int len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
int end = _size + len;
while (end > pos + len - 1)
{
_str[end] = _str[end - len];
end–;
}
for (int i = 0; i < len; i++)
{
_str[i + pos] = str[i];
}
_size += len;
}
// 删除pos位置上的元素,并返回该元素的下一个位置
void erase(size_t pos, size_t len)
{
if (pos >=_size)
return;
if (pos + len >= _size)
{
_size = pos;
_str[_size] = ‘\0’;
return;
}
for (int i = pos + len; i <= _size; i++)
{
_str[pos] = _str[i];
pos++;
}
_size -= len;
}
private:
char
_str;
size_t _capacity;
size_t _size;
};
ostream& operator << (ostream& _cout, const bit::string& s)
{
for (int i = 0; i < s.size(); i++)
{
_cout << s[i];
}
return _cout;
}
istream& operator>>(istream& _cin, bit::string& s)
{
cin >> s._str;
return _cin;
}
};
void test()
{
bit::string a(“123”);
bit::string b(a);
bit::string c = b;
cout << a.c_str() << endl;
cout << b.c_str() << endl;
cout << c.c_str() << endl;
}
void test1()
{
bit::string str(“123456”);
bit::string::iterator p = str.begin();
while (p != str.end())
{
cout << *p << " ";
p++;
}
cout << endl;
for (auto a : str)
{
cout << a << " ";
}
}
void test3()
{
bit::string str(“12345”);
cout << str.c_str() << endl;
cout << "size: " << str.size() << endl;
cout << "capacity: " << str.capacity() << endl;
str.reserve(100);
cout << str.c_str() << endl;
cout << "size: " << str.size() << endl;
cout << "capacity: " << str.capacity() << endl;
}
void test4()
{
bit::string str(“12345”);
str.insert(0, ‘0’);
cout << str.c_str();
cout << endl;
cout << "size: " << str.size() << endl;
cout << "capacity: " << str.capacity() << endl;
str.insert(6, ‘6’);
cout << str.c_str();
cout << endl;
cout << "size: " << str.size() << endl;
cout << "capacity: " << str.capacity() << endl;
str.insert(0, “abc”);
cout << str.c_str();
cout << endl;
cout << "size: " << str.size() << endl;
cout << "capacity: " << str.capacity() << endl;
str.erase(0, 3);
cout << str.c_str();
cout << endl;
cout << "size: " << str.size() << endl;
cout << "capacity: " << str.capacity() << endl;
}

void test5()
{
bit::string str1(“123”);
bit::string str2(“123”);
cout << (str1 < str2)<<endl;
cout << (str1 > str2)<<endl;
cout << (str1 == str2)<<endl;
cout << (str1 >= str2)<<endl;
cout << (str1 <= str2)<<endl;
}
void test6()
{
bit::string str(“12345”);
bit::string str2(“12345”);
std::cout << str<<endl<<str2;
}
int main()
{
//test();
//test1();
//test3();
//test4();
test6();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值