• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于C++STL string类的介绍及模拟实现

c++ 搞代码 4年前 (2022-01-06) 116次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了关于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搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于C++STL string类的介绍及模拟实现

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址