文章目录[隐藏]
这篇文章主要介绍了关于C++STL string类的介绍及模拟实现的相关资料,需要的朋友可以参考下面具体的文章内容
一、标准库中的string类
1.string类
字符串的表示字符序列的类
标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作
单字节字符字符串的设计特性。
string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型(关于模板的更多信
息,请参阅basic_string)。
string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits
和allocator作为basic_string的默认参数(根于更多的模板信息请参考basic_string)。
注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个
类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。
2.string类中的常用接口说明+模拟实现
2.1 string类对象的常见构造+模拟实现
代码演示:
#include #include using namespace std; int main() { string s1; string s4("hello world"); string s5("hello world", 7); string s6(10, 'x'); string s2(s4); string s3(s4, 6, 3); cout << "s1:"<< s1.c_str() << endl; cout << "s4:" << s4.c_str() << endl; cout << "s5:" << s5.c_str() << endl; cout << "s6:" << s6.c_str() << endl; cout << "s2:" << s2.c_str() << endl; cout << "s3:" << s3.c_str() << endl; }
运行结果:
模拟实现
由于上面有些接口不常用,所以我就模拟实现了一部分常用的接口
string (const char* s)
namespace cxy { class string { public: string(const char*s = "") { if (s==nullptr) return; _size = strlen(s); _capacity = _size; _str = new char[_capacity + 1]; strcpy(_str, s); } const char* c_str() { return _str; } private: size_t _size; size_t _capacity; char* _str; }; }
string (const string& str)
void swap (string& str)
namespace cxy { class string { public: void swap(string& str) { //下面的swap会调用库里面的接口 ::swap(_size, str._size); ::swap(_capacity, str._capacity); ::swap(_str, str._str); } string(const char*s = "") { if (s==nullptr) return; _size = strlen(s); _capacity = _size; _str = new char[_capacity + 1]; strcpy(_str, s); } string(const string& str) :_str(nullptr), _size(0), _capacity(0) { string tmp(str._str); swap(tmp); } char* c_str() { return _str; } private: size_t _size; size_t _capacity; char* _str; }; }
2.2 string类对象的容量操作+模拟实现
代码演示:
int main() { string s1("hello world"); cout <<"s1.size(): " <<s1.size() << endl; cout <<"s1.length(): "<< s1.length() << endl; cout <<"s1.capacity(): "<<s1.capacity() << endl; cout <<"s1:"<< s1 << endl; cout << endl; s1.clear(); cout <<"s1:"<< s1 << endl; cout << "s1.size(): " << s1.size() << endl; cout << "s1.capacity(): " << s1.capacity() << endl; cout << endl; s1 = "hello world"; cout << "s1:" << s1 << endl; cout << "s1.size(): " << s1.size() << endl; cout << "s1.capacity(): " << s1.capacity() <capacity,则扩容,并且把0~27上位置的空余位置填充‘字符' cout << "s1:" << s1 << endl; cout << "s1.size(): " << s1.size() << endl; cout << "s1.capacity(): " << s1.capacity() << endl; s1.resize(27, 'x'); //当siz<div style="color:transparent">来源gaodai^.ma#com搞#代!码网</div>e<n<capacity,则把0~27上位置的空余位置填充‘字符' cout << "s1:" << s1 << endl; cout << "s1.size(): " << s1.size() << endl; cout << "s1.capacity(): " << s1.capacity() << endl; s1.resize(5, 'x'); //当n<size,则只保留n个‘字符',空间大小不变 cout << "s1:" << s1 << endl; cout << "s1.size(): " << s1.size() << endl; cout << "s1.capacity(): " << s1.capacity() << endl; cout << endl; string s2("hello world"); s2.reserve(5); //当n<=capacity时,空间大小不变,且不改变数据内容 cout << "s2:" << s2 << endl; cout << "s2.size(): " << s2.size() << endl; cout << "s2.capacity(): " << s2.capacity() <capacity时,空间会增大 cout << "s2:" << s2 << endl; cout << "s2.size(): " << s2.size() << endl; cout << "s2.capacity(): " << s2.capacity() << endl; }
运行结果:
<p style="t
以上就是关于C++STL string类的介绍及模拟实现的详细内容,更多请关注gaodaima搞代码网其它相关文章!