标准库中的string类
string类
- string是表示字符串的字符串类。
- 该类的接口与容器的常规接口基本相同,再添加一些专门用来操作string的常规操作。
- string在底层实际是:basic_string类模板的别名,
typedef basic_string<char, char_traits, allocator> string;
- 不能操作多字节或变长字符的序列。
在使用string类时必须包括#include头文件以及using namespace std.
string类的常用接口
string类对象的常见构造
函数名称 | 功能说明 |
---|
string() | 构造空的string类,即空的字符串 |
string(const char* s) | 用C_string类来构造string类对象 |
string(size_t n,char c) | string类对象中包含n个字符c |
string(const string& s) | 拷贝构造函数 |
void teststring(){
string s1;
string s2("hello world");
string s3(5,10);
string s4(s2);
}
string类对象的容量操作
函数名称 | 功能说明 |
---|
size | 返回字符串的有效字符长度 |
length | 返回字符串的有效字符长度 |
capacity | 返回空间总大小 |
empty | 检测字符串释放为空串,是返回true,否则返回false |
clear | 清空有效字符 |
reserve | 为字符串预留空间 |
resize | 将有效字符的个数改为n个,多出的空间用字符c填充 |
void teststring(){
string s(“hello world!”);
cout<<s.size()<<endl;
cout<<s.lenth()<<endl;
cout<<s.capacity()<<endl;
cout<<s<<endl;
s.clear();
cout<<s.size()<<endl;
cout<<s.capacity()<<endl;
cout<<s<<endl;
s. resize(10,'a');
cout<<s.size()<<endl;
cout<<s.capacity()<<endl;
cout<<s<<endl;
s.resize(20,'b');
cout<<s.size()<<endl;
cout<<s.capacity()<<endl;
cout<<s<<endl;
s.resize(5);
cout<<s.size()<<endl;
cout<<s.capacity()<<endl;
cout<<s<<endl;
s.reserve(100);
cout<<s.size()<<endl;
cout<<s.capacity()<<endl;111
s.reserve(50);
cout << s.size() << endl;
cout << s.capacity() << endl;
}
string类的模拟实现
深浅拷贝
class String{
public:
String(const char* str = "")
:_str(new char[strlen(str)+1])
{
strcpy(_str, str);
}
~String(){
delete[] _str;
_str = nullptr;
}
String(const String& str)
:_str(new char[strlen(str._str)+1])
{
strcpy(_str, str._str);
}
String& operator=(const String& s){
if (this != &s){
delete[] _str;
_str = new char[strlen(s._str) + 1];
strcpy(_str, s._str);
}
return *this;
}
char& operator[](size_t pos){
return _str[pos];
}
const char* c_str(){
return _str;
}
private:
char* _str;
};
int main(){
String s1("hello");
String s2;
cout << s1.c_str() << endl;
s1[0] = ' ';
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
String c1(s1);
cout <<c1.c_str() << endl;
c1[0] = 'h';
cout << c1.c_str() << endl;
s2 = "world";
s1 = s2;
cout << s1.c_str() << endl;
system("pause");
return 0;
}
namespace cyy
{
class string
{
public:
typedef const char* const_iterator;
typedef char* iterator;
const_iterator begin() const
{
return _str;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
string(const char* str = "")
:_str(new char[strlen(str) + 1])
{
strcpy(_str, str);
_size = strlen(str);
_capacity = _size;
}
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
string(const string& s)
:_str(new char[s._size + 1])
, _size(s._size)
, _capacity(s._size)
{
strcpy(_str, s._str);
}
string& operator=(const string& s)
{
if (this != &s)
{
delete[] _str;
_str = new char[s._size + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
const char* c_str()
{
return _str;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
size_t size()
{
return _size;
}
size_t capacity()
{
return _capacity;
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n+1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void push_back(char ch)
{
insert(_size, ch);
}
void append(const char* str)
{
insert(_size, str);
}
const string& operator+=(char ch)
{
push_back(ch);
return *this;
}
const string& operator+=(const char* str)
{
append(str);
return *this;
}
const string& operator+=(const string& s)
{
append(s._str);
return *this;
}
void insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity * 2);
}
size_t end = _size+1;
while (end >= pos + 1)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_size;
}
void 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)
{
_str[end] = _str[end-len];
--end;
}
while (*str)
{
_str[pos++] = *str++;
}
_size += len;
}
bool operator>(const string& s) const
{
const char* str1 = _str;
const char* str2 = s._str;
while (*str1 && *str2)
{
if (*str1 > *str2)
{
return true;
}
else if (*str1 < *str2)
{
return false;
}
else
{
++str1;
++str2;
}
}
if (*str1)
{
return true;
}
else
{
return false;
}
}
bool operator==(const string& s) const
{
const char* str1 = _str;
const char* str2 = s._str;
while (*str1 && *str2)
{
if (*str1 != *str2)
{
return false;
}
else
{
++str1;
++str2;
}
}
if (*str1 || *str2)
{
return false;
}
else
{
return true;
}
}
bool operator>=(const string& s) const
{
return *this > s || *this == s;
}
size_t find(char ch)
{
}
size_t find(const char* str)
{
}
private:
char* _str;
size_t _size;
size_t _capacity;
};
}
void test1()
{
cyy::string s1("hello");
cout << s1.c_str() << endl;
cyy::string copy1(s1);
cout << copy1.c_str() << endl;
for (size_t i = 0; i < s1.size(); ++i)
{
s1[i] = 'a';
cout << s1[i] << " ";
}
cout << endl;
cyy::string::iterator it1 = s1.begin();
while (it1 != s1.end())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
for (auto e : s1)
{
cout << e << " ";
}
cout << endl;
}
void test2()
{
cyy::string s1("hello");
cout << s1.capacity() << endl;
s1 += "world";
cout << s1.capacity() << endl;
s1 += '!';
cout << s1.capacity() << endl;
cout << s1.c_str() << endl;
cyy::string s2("helloworld!");
s2.insert(5, ' ');
cout << s2.c_str() << endl;
s2.insert(0, '$');
cout << s2.c_str() << endl;
s2.insert(0, "bit");
cout << s2.c_str() << endl;
}
int main()
{
test1();
test2();
return 0;
}