1.string类介绍
- string是表示字符串的字符串类
- 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操 作。
- string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator> string;
- 不能操作多字节或者变长字符的序列。
在使用string类时,必须包含头文件以及using namespace std;
2.string类的常用接口
1. string类对象的常见构造
函数名称 | 功能说明 |
---|---|
string() | 构造空的string类对象,即空字符串 |
string(const char* s) | 用C-string来构造string类对象 |
string(size_t n, char c) | string类对象中包含n个字符c |
string(const string&s) | 拷贝构造函数 |
string(const string&s, size_t n) | 用s中的前n个字符构造新的string类对象 |
用例:
void TestString()
{
string s1; // 构造空的string类对象s1
string s2("hello bit"); // 用C格式字符串构造string类对象s2
string s3(10, 'a'); // 用10个字符'a'构造string类对象s3
string s4(s2); // 拷贝构造s4
string s5(s3, 5); // 用s3中前5个字符构造string对象s5
}
2. string类对象的容量操作
函数名称 | 功能说明 |
---|---|
size_t size() const | 返回字符串有效字符长度 |
size_t length() const | 返回字符串有效字符长度 |
size_t capacity ( ) const | 返回空间总大小 |
bool empty ( ) const | 检测字符串释放为空串,是返回true,否则返回false |
void clear() | 清空有效字符 |
void resize ( size_t n, char c ) | 将有效字符的个数该成n个,多出的空间用字符c填充 |
void resize ( size_t n ) | 将有效字符的个数改成n个,多出的空间用0填充 |
void reserve ( size_t res_arg=0 ) | 为字符串预留空间 |
注意:
- size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
- clear()只是将string中有效字符清空,不改变底层空间大小。
- resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
- reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。
3. string类对象的访问操作
函数名称 | 功能说明 |
---|---|
char& operator[] ( size_t pos ) | 返回pos位置的字符,const string类对象调用 |
const char& operator[] ( size_t pos ) const | 返回pos位置的字符,非const string类对象调用 |
4. string类对象的修改操作
函数名称 | 功能说明 |
---|---|
void push_back(char c) | 在字符串后尾插字符c |
string& append (const char* s); | 在字符串后追加一个字符串 |
string& operator+=(const string& str) | 在字符串后追加字符串str |
string& operator+=(const char* s) | 在字符串后追加C个数字符串 |
string& operator+=(char c) | 在字符串后追加字符c |
const char* c_str( )const | 返回C格式字符串 |
size_t find (char c, size_t pos = 0)const | 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置 |
size_t rfind(char c, size_t pos = npos) | 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置 |
string substr(size_t pos = 0, size_t n = npos)const | 在str中从pos位置开始,截取n个字符,然后将其返回 |
注意:
- 在string尾部追加字符时,s.push_back© / s.append(1, c) / s += 'c’三种的实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。
- 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。
5. string类非成员函数
函数 | 功能说明 |
---|---|
operator+ | 尽量少用,因为效率低 |
operator>> | 输入运算符重载 |
operator<< | 输出运算符重载 |
getline | 获取一行字符串 |
relational operators | 大小比较 |
3. string类的模拟实现
1.浅拷贝
浅拷贝:也称位拷贝,编译器只是将对象中的值拷贝过来。如果对象中管理资源,最后就会导致多个对象共享同一份资源,当一个对象销毁时就会将该资源释放掉,而此时另一些对象不知道该资源已经被释放,以为还有效,所以 当继续对资源进项操作时,就会发生发生了访问违规。
2.深拷贝
如果一个类中涉及到资源的管理,其拷贝构造函数、赋值运算符重载以及析构函数必须要显式给出。一般情况都是按照深拷贝方式提供。
3.写时拷贝
引用计数:用来记录资源使用者的个数。在构造时,将资源的计数给成1,每增加一个对象使用该资源,就给计数增加1,当某个对象被销毁时,先给该计数减1,然后再检查是否需要释放资源,如果计数为1,说明该对象时资源的最后一个使用者,将该资源释放;否则就不能释放,因为还有其他对象在使用该资源。
4.模拟实现
#include<iostream>
#include<assert.h>
using namespace std;
namespace fxl
{
class MyString{
public:
typedef char* Iterator;
public:
MyString(const char* str="")
{
if (nullptr == str)
{
assert(str);
return;
}
_str = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
MyString(const MyString& s)
:_str(new char[_capacity + 1])
, _capacity(s._size)
, _size(s._capacity)
{
strcpy(_str, s._str);
}
MyString& operator=(const MyString& s)
{
if (this != &s)
{
char* Ptr = new char[s._capacity + 1];
strcpy(Ptr, s._str);
delete _str;
_str = Ptr;
_capacity = s._capacity;
_size = s._size;
}
return *this;
}
~MyString()
{
if (_str)
{
delete _str;
_str = nullptr;
}
}
Iterator Begin()
{
return _str;
}
Iterator End()
{
return _str + _size;
}
void Push_Back(char c)
{
if (_size == _capacity)
{
Reserve(_capacity = 2 * _capacity);
}
_str[_size++] = c;
_str[_size] = '\0';
}
void Append(size_t n, char c)
{
for (size_t i = 0; i < n; i++);
Push_Back(c);
}
MyString& operator+=(char s)
{
Push_Back(s);
return *this;
}
void Append(const char* str){
while (*str)
{
Push_Back(*str);
str++;
}
}
MyString& operator+=(const char* str)
{
while (*str){
Push_Back(*str);
str++;
}
}
void Clear()
{
_size = 0;
_str[_size] = '\0';
}
void Swap(MyString& s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
const char* C_Str()const
{
return _str;
}
size_t Size()const
{
return _size;
}
size_t Capacity()const
{
return _capacity;
}
bool Empty()const
{
return 0 == _size;
}
void Resize(size_t newSize, char c = char())
{
if (newSize > _size)
{
if (newSize > _capacity)
Reserve(newSize);
memset(_str + _size, c, newSize - _size);
}
_size = newSize;
_str[_size] = '\0';
}
void Reserve(size_t newCapacity)
{
if (newCapacity > _capacity)
{
char* str = new char[newCapacity+1];
strcpy(str, _str);
delete _str;
_str = str;
_capacity = newCapacity;
}
}
char& operator[](size_t index)
{
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index)const
{
assert(index < _size);
return _str[index];
}
bool operator<(const MyString& s)
{
int i = 0;
for (i = 0; i<_size&&i<s._size; i++)
{
if (*_str<s[i])
return false;
if (*_str>s[i])
return true;
}
if (i == _size)
return false;
else return true;
}
bool operator<=(const MyString& s)
{
int i = 0;
for (i = 0; i<_size&&i<s._size; i++)
{
if (*_str>s[i])
return false;
if (*_str<s[i])
return true;
}
if (i == _size)
return true;
else return false;
}
bool operator>(const MyString& s)
{
int i = 0;
for (i = 0; i<_size&&i<s._size; i++)
{
if (*_str<s[i])
return false;
if (*_str>=s[i])
return true;
}
if (i == _size)
return false;
else return true;
}
bool operator>=(const MyString& s)
{
int i = 0;
for (i = 0; i<_size&&i<s._size; i++)
{
if (*_str>=s[i])
return false;
if (*_str<s[i])
return true;
}
if (i == _size)
return true;
else return false;
}
bool operator==(const MyString& s)
{
int i = 0;
for (i = 0; i < _size&&i < s._size; i++)
{
if (_str[i] != s[i])
return false;
}
if (i == _size&&i == s._size)
return true;
else
{
return false;
}
}
bool operator!=(const MyString& s)
{
int i = 0;
for (i = 0; i < _size&&i < s._size; i++)
{
if (_str[i] == s[i])
return false;
}
if (i == _size&&i == s._size)
return false;
else
{
return true;
}
}
private:
friend ostream& operator<<(ostream& _cout, const fxl::MyString& s);
private:
char* _str;
size_t _capacity;
size_t _size;
};
}