21xrx.com
2024-06-02 23:07:57 Sunday
登录
文章检索 我的文章 写文章
C++11 STL string 源码分析
2023-07-13 13:15:33 深夜i     --     --
C++11 STL string 源码分析 编程语言

C++11是C++语言的一个版本,其中包括了STL库中的string类,是对C语言字符串处理的加强与优化。在本文中我们将深入分析C++11 STL string的源码实现。

首先,STL string类的定义如下:


class string {

public:

//数据类型定义,省略其他成员变量,成员函数

typedef std::size_t size_type;

typedef char value_type;

typedef const char* const_iterator;

typedef const_iterator iterator;

//构造函数

string();

explicit string(const Allocator&);

string(const string& str);

string(string&& str)

 noexcept(noexcept(Allocator(str)))

 : basic_string(std::move(str)) {}

string(const string& str, size_type pos, size_type n = npos,

    const Allocator& a = Allocator());

string(const char* s, size_type n, const Allocator& a = Allocator());

string(const char* s, const Allocator& a = Allocator());

string(size_type n, char c, const Allocator& a = Allocator());

template<class InputIt>

string(InputIt first, InputIt last, const Allocator& a = Allocator());

string(std::initializer_list<char> init, const Allocator& alloc = Allocator());

</span>~string();

//成员函数

const char* c_str() const noexcept;

const char* data() const noexcept;

size_type size() const noexcept;

size_type length() const noexcept;

size_type capacity() const noexcept;

void resize(size_type n, char c);

void resize(size_type n);

void reserve(size_type res_arg = 0);

void clear() noexcept;

bool empty() const noexcept;

void shrink_to_fit();

void push_back(char c);

void pop_back();

string& operator=(const string& str);

string& operator=(string&& str)

 noexcept(noexcept(this->alloc_ == std::move(str).alloc_));

string& operator=(const char* s);

string& operator=(char c);

string& operator=(std::initializer_list<char> ilist);

char& operator[](size_type pos);

const char& operator[](size_type pos) const;

char& at(size_type pos);

const char& at(size_type pos) const;

char& back();

const char& back() const;

char& front();

const char& front() const;

string& operator+=(const string& str);

string& operator+=(const char* s);

string& operator+=(char c);

string& operator+=(std::initializer_list<char> ilist);

int compare(const string& str) const noexcept;

int compare(size_type pos1, size_type count1, const string& str) const;

int compare(size_type pos1, size_type count1, const string& str, size_type pos2, size_type count2 = npos) const;

int compare(const char* s) const noexcept;

int compare(size_type pos1, size_type count1, const char* s) const;

int compare(size_type pos1, size_type count1, const char* s, size_type count2) const;

// 其他函数

std::allocator allocator() const noexcept;

};

在源码实现中,STL string提供了多个构造函数,用于创建不同类型的字符串。其中,最常用的构造函数为默认构造函数,其实现如下:


string::string()

 : basic_string<CharT>(allocator_type())

{}

同时,该类还提供了多种赋值和比较操作,例如赋值操作符:


string& string::operator=(const string& str) {

 basic_string<CharT>::operator=(str);

 return *this;

}

该操作符将一个string类型的变量作为参数,然后将该变量的值赋给当前的string类型变量。实现中,该操作符调用的是基类basic_string中的操作符,对字符串进行赋值操作。

另外,该类还提供了一系列的成员函数,包括resize()、push_back()、pop_back()等,用于对字符串进行修改。其中,resize()函数用于调整字符串的大小,该函数实现如下:


void string::resize(size_type n, char c) {

 basic_string<CharT>::resize(n, static_cast<CharT>(c));

}

该函数接收两个参数:一个是需要调整的大小,另一个是用于填充新添加的字符。在实现中,该函数调用的是基类basic_string中的resize()函数。

最后,STL string还提供了类似于c_str()、length()、size()等函数,用于获取字符串的一些属性。例如,length()函数实现如下:


size_type string::length() const noexcept {

 return basic_string<CharT>::size();

}

该函数返回当前字符串的长度,实现中调用的是基类basic_string中的size()函数。

总结来说,C++11 STL string类的实现通过基类basic_string和一系列成员函数、比较操作符来为开发者提供了一个高效稳定的字符串操作方案。对于需要进行大量字符串操作的C++开发者来说,系统自带的字符串操作方式不足以满足业务需求时,可以考虑使用STL string类。

  
  
下一篇: C++ 的 move 函数

评论区

{{item['qq_nickname']}}
()
回复
回复