string 类 ( 了解 )string 类的文档介绍1. 字符串是表示字符序列的类2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。3. string 类是使用 char( 即作为它的字符类型,使用它的默认 char_traits 和分配器类型 ( 关于模板的更多信息,请参阅 basic_string) 。4. string 类是 basic_string 模板类的一个实例,它使用 char 来实例化 basic_string 模板类,并用 char_traits和 allocator 作为 basic_string 的默认参数 ( 根于更多的模板信息请参考 basic_string) 。5. 注意,这个类独立于所使用的编码来处理字节 : 如果用来处理多字节或变长字符 ( 如 UTF-8) 的序列,这个 类的所有成员( 如长度或大小 ) 以及它的迭代器,将仍然按照字节 ( 而不是实际编码的字符 ) 来操作。总结:1. string 是表示字符串的字符串类2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作 string 的常规操作。3. string 在底层实际是: basic_string 模板类的别名, typedef basic_string<char, char_traits, allocator>string;4. 不能操作多字节或者变长字符的序列。在 使用 string 类时,必须包含 #include 头文件以及 using namespace std ;
简单了解string过后,这个博客我们主要经行string的常见函数的常见接口的模拟实现,这可以有效帮助大家理解string。
#pragma once
//模拟实现string类,并完成测试
#include<iostream>
#include<assert.h>
using namespace std;
namespace bit
{
class string
{
friend istream& operator>>(istream& in, string& s);
friend ostream& operator<<(ostream& out, string& s);
public:
//
// iterator
static const size_t npos = -1;
typedef char* iterator;
typedef const char* const_iterator;
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
string(const char* str = "")
:_size(strlen(str))
{
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str,str);
}
string(const string& s)
{
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
~string()
{
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
const char* c_str() const
{
return _str;
}
string& operator=(const string& s) {
char* tmp = new char[s._capacity + 1];
_size = s._size;
_capacity = s._capacity;
delete[] _str;
strcpy(tmp, s._str);
_str = tmp;
return *this;
}
/
// modify
void push_back(char c) {
if (_size == _capacity) {
reserve(++_size);
}
_str[_size - 1] = c;
_str[_size] = '\0';
}
string& operator+=(char c) {
if (_size == _capacity) {
reserve(++_size);
}
_str[_size - 1] = c;
_str[_size] = '\0';
return *this;
}
void append(const char* str) {
if (_size + strlen(str) > _capacity) {
reserve(_size + strlen(str));
}
strcpy(&_str[_size], str);
_size += strlen(str);
}
string& operator+=(const char* str) {
if (_size + strlen(str) > _capacity) {
reserve(_size + strlen(str));
}
strcpy(&_str[_size], str);
_size += strlen(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);
}
/
// capacity
size_t size()const {
return _size;
}
size_t capacity()const {
return _capacity;
}
bool empty()const {
return _size ? false : true;
}
void resize(size_t n, char c = '\0') {
if (n <= _size) {
_str[n] = '\0';
_size = n;
}
else {
reserve(n);
for (size_t i = _size; i < n; i++) {
_str[i] = c;
}
_str[n] = '\0';
_size = n;
}
}
void reserve(size_t n) {
if (n > _capacity) {
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
/
// access
char& operator[](size_t index) {
return _str[index];
}
const char& operator[](size_t index)const {
return _str[index];
}
/
//relational operators
bool operator<(const string& s) {
return strcmp(_str, s._str) < 0 ? true : false;
}
bool operator<=(const string& s) {
return strcmp(_str, s._str) <= 0 ? true : false;
}
bool operator>(const string& s) {
return strcmp(_str, s._str) > 0 ? true : false;
}
bool operator>=(const string& s) {
return strcmp(_str, s._str) >= 0 ? true : false;
}
bool operator==(const string& s) {
return strcmp(_str, s._str) == 0 ? true : false;
}
bool operator!=(const string& s) {
return strcmp(_str, s._str) != 0 ? true : false;
}
// 返回c在string中第一次出现的位置
size_t find(char c, size_t pos = 0) const {
assert(pos < _size);
while (pos < _size) {
if (_str[pos] == c) {
return pos;
}
pos++;
}
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;
}
else
{
return npos;
}
}
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
string& insert(size_t pos, char c) {
assert(pos <= _size);
if (_size ==_capacity)
{
// 扩容
reserve(++_size);
}
size_t end = _size;
while (end > pos)
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = c;
return *this;
}
string& insert(size_t pos, const char* str) {
assert(pos <= _size);
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;
return *this;
}
// 删除pos位置上的元素,并返回该元素的下一个位置
void erase(size_t pos, size_t len) {
assert(pos < _size);
if (len >= _size - pos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
string substr(size_t pos = 0, size_t len = npos)
{
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;
}
private:
char* _str = nullptr;
size_t _size = 0;
size_t _capacity = 0;
};
istream& operator>>(istream& in, string& s) {
s.clear();
char ch=in.get();
char buff[128] = { 0 };
int i = 0;
while (ch != ' ' && ch != '\n') {
buff[i] = ch;
i++;
if (i == 127) {
s += buff;
i = 0;
}
ch = in.get();
}
if (i) {
buff[i] = 0;
s += buff;
}
return in;
}
ostream& operator<<(ostream& out, string& s) {
for (auto e : s) {
out << e;
}
return out;
}
void test1(){
string s1("hello world");
string s2;
//cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
}
void test2() {
string s3("hello world");
string s4;
s3.push_back('x');
s3.push_back('x');
s3.push_back('x');
s3.push_back('x');
//cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
}
void test3() {
string s5("hello world");
s5.push_back('x');
s5.resize(5);
cout << s5.c_str() << endl;
}
void test4() {
string s6("hello world");
s6 += 'x';
s6 += 'x';
s6 += 'x';
cout << s6.c_str() << endl;
}
void test5() {
string s7("hello world");
s7 += "mao";
s7 += "毛少东";
cout << s7.c_str() << endl;
}
void test6() {
string s8("hello world");
s8[1]++;
cout << s8.c_str() << endl;
s8[1]--;
cout << s8.c_str() << endl;
}
void test7() {
string s9("hello world");
s9.insert(0," 毛少东");
cout << s9.c_str() << endl;
s9.insert(0,'x');
cout << s9.c_str() << endl;
}
void test8() {
string s10("hello world");
cout << s10 << endl;
cout << s10 << "niubi" << endl;
}
void test9() {
string s11("hello world");
string s12;
cin >> s11 >> s11;
cout << s11 << endl;
cin >> s12>>s11;
cout << s12 << endl;
cout << s11 << endl;
}
}