String.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace bit
{
class String
{
friend ostream& operator<<(ostream& _cout, const String& s);
friend istream& operator>>(istream& _cin, String& s);
public:
typedef char* iterator;
typedef const char* const_iterator;
public:
String(const char* str = "")
:_size(strlen(str))
{
_capacity = _size;
_str = new char[_size + 1];
strcpy(_str, str);
}
String(const String& s)
:_str(new char[strlen(s._str) + 1])
{
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
传统写法
//String& operator=(const String& s)
//{
// if (this != &s)
// {
// delete[] _str;
// _str = new char[s._capacity];
// strcpy(_str, s._str);
// _size = s._size;
// _capacity = s._capacity;
// }
// return *this;
//}
//现代写法1(使用拷贝构造函数创建一个临时对象)
//String& operator=(const String& s)
//{
// if (this != &s)
// {
// String tmp(s);
// std::swap(_str, tmp._str);
// _size = tmp._size;
// _capacity = tmp._capacity;
// }
// return *this;
//}
现代写法2
//String& operator=(const String& s)
//{
// if (this != &s)
// {
// String tmp(s._str);
// std::swap(_str, tmp._str);
// _size = tmp._size;
// _capacity = tmp._capacity;
// }
// return *this;
//}
//现代写法3
String& operator=(String& s)
{
swap(s);
return *this;
}
//~String()
//{
// if (_str)
// {
// delete[] _str;
// _str = nullptr;
// }
//}
iterator
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
///
modify
void push_back(char c)
{
if (_size == _capacity)
{
reserve(_capacity * 2);
}
_str[_size++] = c;
_str[_size] = '\0';
}
String& operator+=(char c)
{
push_back(c);
return *this;
}
void append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
String& operator+=(const char* str)
{
append(str);
return *this;
}
void clear()
{
_size = 0;
_str[_size] = '\0';
}
void swap(String& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
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 n, char c = '\0')
{
if (n > _size)
{
if (n > _capacity)
{
reserve(n);
}
memset(_str + _size, c, n - _size);
}
_size = n;
_str[n] = '\0';
}
void reserve(size_t n)
{
if (_capacity < n)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
///
access
char& operator[](size_t index)
{
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index)const
{
assert(index < _size);
return _str[index];
}
///
relational operators
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(_str, s._str) > 0;
}
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);
}
返回c在string中第一次出现的位置
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;
}
返回子串s在string中第一次出现的位置
size_t find(const char* s, size_t pos = 0) const
{
assert(pos < _size);
const char* p = strstr(_str + pos, s);
if (p)
{
return p - _str;
}
return npos;
}
在pos位置上插入字符c/字符串str,并返回该字符的位置
void insert(size_t pos, char c)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = c;
_size++;
}
void insert(size_t pos, const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
size_t end = _size + len;
while (end > pos + len - 1)
{
_str[end] = _str[end - len];
end--;
}
strncpy(_str + pos, str, len);
_size += len;
}
删除pos位置上的元素,并返回该元素的下一个位置
void erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || len + pos >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
private:
char* _str;
size_t _capacity;
size_t _size;
public:
static const int npos;
};
ostream& operator<<(ostream& _cout, const String& s)
{
for (auto e : s)
{
_cout << e;
}
return _cout;
}
istream& operator>>(istream& _cin, String& s)
{
s.clear();
char ch;
char buff[127];
ch = _cin.get();
size_t i = 0;
while (ch != '\n' && ch != ' ')
{
buff[i++] = ch;
if (i == 127)
{
buff[i] = '\0';
s += buff;
i = 0;
}
ch = _cin.get();
}
if (i > 0)
{
buff[i] = '\0';
s += buff;
}
return _cin;
}
void swap(String& x, String& y)
{
x.swap(y);
}
const int bit::String::npos = -1;
void test_string1()
{
cout << "-------------------------------------test_string1-------------------------------------" << endl;
String s1("hello world");
String s2;
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
for (size_t i = 0; i < s1.size(); i++)
{
s1[i]++;
}
cout << endl;
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
const String s3("xxxx");
for (size_t i = 0; i < s3.size(); i++)
{
//s3[i]++;
cout << s3[i] << " ";
}
cout << endl;
}
void test_string2()
{
cout << "-------------------------------------test_string2-------------------------------------" << endl;
String s3("hello world");
for (auto ch : s3)
{
cout << ch << " ";
}
cout << endl;
String::iterator it3 = s3.begin();
while (it3 != s3.end())
{
*it3 -= 1;
cout << *it3 << " ";
++it3;
}
cout << endl;
const String s4("xxxx");
String::const_iterator it4 = s4.begin();
while (it4 != s4.end())
{
//*it4 += 3;
cout << *it4 << " ";
++it4;
}
cout << endl;
for (auto ch : s4)
{
cout << ch << " ";
}
cout << endl;
}
void test_string3()
{
cout << "-------------------------------------test_string3-------------------------------------" << endl;
String s3("hello world");
s3.push_back('1');
s3.push_back('2');
cout << s3.c_str() << endl;
s3 += 'x';
s3 += "yyyyyy";
cout << s3.c_str() << endl;
String s1("hello world");
s1.insert(11, 'x');
cout << s1.c_str() << endl;
s1.insert(0, 'x');
cout << s1.c_str() << endl;
}
void test_string4()
{
cout << "-------------------------------------test_string4-------------------------------------" << endl;
String s1("hello world");
cout << s1.c_str() << endl;
s1.erase(6, 3);
cout << s1.c_str() << endl;
s1.erase(6, 30);
cout << s1.c_str() << endl;
s1.erase(3);
cout << s1.c_str() << endl;
String s2("hello world");
cout << s2.c_str() << endl;
s2.resize(5);
cout << s2.c_str() << endl;
s2.resize(20, 'x');
cout << s2.c_str() << endl;
}
void test_string5()
{
cout << "-------------------------------------test_string5-------------------------------------" << endl;
String s1("hello world");
cout << s1.c_str() << endl;
String s2(s1);
cout << s2.c_str() << endl;
s1[0] = 'x';
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
String s3("xxxxx");
s1 = s3;
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
}
void test_string6()
{
cout << "-------------------------------------test_string6-------------------------------------" << endl;
String s1("hello world");
cout << s1.c_str() << endl;
s1.insert(6, "xxx");
cout << s1.c_str() << endl;
String s2("xxxxxxx");
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
swap(s1, s2);
s1.swap(s2);
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
}
void test_string7()
{
cout << "-------------------------------------test_string7-------------------------------------" << endl;
String s1;
cin >> s1;
cout << s1.capacity() << endl;
}
};
test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"String.h"
using namespace bit;
int main()
{
test_string1();
test_string2();
test_string3();
test_string4();
test_string5();
test_string6();
test_string7();
return 0;
}
测试结果截图
