📄 basic_string.h
字号:
const_reverse_iterator rbegin() const { return const_reverse_iterator(this->end()); } reverse_iterator rend() { return reverse_iterator(this->begin()); } const_reverse_iterator rend() const { return const_reverse_iterator(this->begin()); } public: // Capacity: size_type size() const { return _M_rep()->_M_length; } size_type length() const { return _M_rep()->_M_length; } size_type max_size() const { return _Rep::_S_max_size; } void resize(size_type __n, _CharT __c); void resize(size_type __n) { this->resize(__n, _CharT()); } size_type capacity() const { return _M_rep()->_M_capacity; } void reserve(size_type __res_arg = 0); void clear() { _M_mutate(0, this->size(), 0); } bool empty() const { return this->size() == 0; } // Element access: const_reference operator[] (size_type __pos) const { return _M_data()[__pos]; } reference operator[](size_type __pos) { _M_leak(); return _M_data()[__pos]; } const_reference at(size_type __n) const { if (__n >= this->size()) __throw_out_of_range("basic_string::at"); return _M_data()[__n]; } reference at(size_type __n) { if (__n >= size()) __throw_out_of_range("basic_string::at"); _M_leak(); return _M_data()[__n]; } // Modifiers: basic_string& operator+=(const basic_string& __str) { return this->append(__str); } basic_string& operator+=(const _CharT* __s) { return this->append(__s); } basic_string& operator+=(_CharT __c) { return this->append(size_type(1), __c); } basic_string& append(const basic_string& __str); basic_string& append(const basic_string& __str, size_type __pos, size_type __n); basic_string& append(const _CharT* __s, size_type __n); basic_string& append(const _CharT* __s) { return this->append(__s, traits_type::length(__s)); } basic_string& append(size_type __n, _CharT __c); template<class _InputIterator> basic_string& append(_InputIterator __first, _InputIterator __last) { return this->replace(_M_iend(), _M_iend(), __first, __last); } void push_back(_CharT __c) { this->replace(_M_iend(), _M_iend(), 1, __c); } basic_string& assign(const basic_string& __str); basic_string& assign(const basic_string& __str, size_type __pos, size_type __n) { const size_type __strsize = __str.size(); if (__pos > __strsize) __throw_out_of_range("basic_string::assign"); const bool __testn = __n < __strsize - __pos; const size_type __newsize = __testn ? __n : __strsize - __pos; return this->assign(__str._M_data() + __pos, __newsize); } basic_string& assign(const _CharT* __s, size_type __n) { if (__n > this->max_size()) __throw_length_error("basic_string::assign"); if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data()) || less<const _CharT*>()(_M_data() + this->size(), __s)) return _M_replace_safe(_M_ibegin(), _M_iend(), __s, __s + __n); else { // Work in-place const size_type __pos = __s - _M_data(); if (__pos >= __n) traits_type::copy(_M_data(), __s, __n); else if (__pos) traits_type::move(_M_data(), __s, __n); _M_rep()->_M_length = __n; _M_data()[__n] = _Rep::_S_terminal; return *this; } } basic_string& assign(const _CharT* __s) { return this->assign(__s, traits_type::length(__s)); } basic_string& assign(size_type __n, _CharT __c) { return this->replace(_M_ibegin(), _M_iend(), __n, __c); } template<class _InputIterator> basic_string& assign(_InputIterator __first, _InputIterator __last) { return this->replace(_M_ibegin(), _M_iend(), __first, __last); } void insert(iterator __p, size_type __n, _CharT __c) { this->replace(__p, __p, __n, __c); } template<class _InputIterator> void insert(iterator __p, _InputIterator __beg, _InputIterator __end) { this->replace(__p, __p, __beg, __end); } basic_string& insert(size_type __pos1, const basic_string& __str) { return this->insert(__pos1, __str, 0, __str.size()); } basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n) { const size_type __strsize = __str.size(); if (__pos2 > __strsize) __throw_out_of_range("basic_string::insert"); const bool __testn = __n < __strsize - __pos2; const size_type __newsize = __testn ? __n : __strsize - __pos2; return this->insert(__pos1, __str._M_data() + __pos2, __newsize); } basic_string& insert(size_type __pos, const _CharT* __s, size_type __n) { const size_type __size = this->size(); if (__pos > __size) __throw_out_of_range("basic_string::insert"); if (__size > this->max_size() - __n) __throw_length_error("basic_string::insert"); if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data()) || less<const _CharT*>()(_M_data() + __size, __s)) return _M_replace_safe(_M_ibegin() + __pos, _M_ibegin() + __pos, __s, __s + __n); else { // Work in-place. If _M_mutate reallocates the string, __s // does not point anymore to valid data, therefore we save its // offset, then we restore it. const size_type __off = __s - _M_data(); _M_mutate(__pos, 0, __n); __s = _M_data() + __off; _CharT* __p = _M_data() + __pos; if (__s + __n <= __p) traits_type::copy(__p, __s, __n); else if (__s >= __p) traits_type::copy(__p, __s + __n, __n); else { traits_type::copy(__p, __s, __p - __s); traits_type::copy(__p + (__p - __s), __p + __n, __n - (__p - __s)); } return *this; } } basic_string& insert(size_type __pos, const _CharT* __s) { return this->insert(__pos, __s, traits_type::length(__s)); } basic_string& insert(size_type __pos, size_type __n, _CharT __c) { this->insert(_M_check(__pos), __n, __c); return *this; } iterator insert(iterator __p, _CharT __c = _CharT()) { size_type __pos = __p - _M_ibegin(); this->insert(_M_check(__pos), size_type(1), __c); _M_rep()->_M_set_leaked(); return this->_M_ibegin() + __pos; } basic_string& erase(size_type __pos = 0, size_type __n = npos) { return this->replace(_M_check(__pos), _M_fold(__pos, __n), _M_data(), _M_data()); } iterator erase(iterator __position) { size_type __i = __position - _M_ibegin(); this->replace(__position, __position + 1, _M_data(), _M_data()); _M_rep()->_M_set_leaked(); return _M_ibegin() + __i; } iterator erase(iterator __first, iterator __last) { size_type __i = __first - _M_ibegin(); this->replace(__first, __last, _M_data(), _M_data()); _M_rep()->_M_set_leaked(); return _M_ibegin() + __i; } basic_string& replace(size_type __pos, size_type __n, const basic_string& __str) { return this->replace(__pos, __n, __str._M_data(), __str.size()); } basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2); basic_string& replace(size_type __pos, size_type __n1, const _CharT* __s, size_type __n2) { const size_type __size = this->size(); if (__pos > __size) __throw_out_of_range("basic_string::replace"); const bool __testn1 = __n1 < __size - __pos; const size_type __foldn1 = __testn1 ? __n1 : __size - __pos; if (__size - __foldn1 > this->max_size() - __n2) __throw_length_error("basic_string::replace"); if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data()) || less<const _CharT*>()(_M_data() + __size, __s)) return _M_replace_safe(_M_ibegin() + __pos, _M_ibegin() + __pos + __foldn1, __s, __s + __n2); // Todo: optimized in-place replace. else return _M_replace(_M_ibegin() + __pos, _M_ibegin() + __pos + __foldn1, __s, __s + __n2, typename iterator_traits<const _CharT*>::iterator_category()); } basic_string& replace(size_type __pos, size_type __n1, const _CharT* __s) { return this->replace(__pos, __n1, __s, traits_type::length(__s)); } basic_string& replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) { return this->replace(_M_check(__pos), _M_fold(__pos, __n1), __n2, __c); } basic_string& replace(iterator __i1, iterator __i2, const basic_string& __str) { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } basic_string& replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n) { return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); } basic_string& replace(iterator __i1, iterator __i2, const _CharT* __s) { return this->replace(__i1, __i2, __s, traits_type::length(__s)); } basic_string& replace(iterator __i1, iterator __i2, size_type __n, _CharT __c); template<class _InputIterator> basic_string& replace(iterator __i1, iterator __i2, _InputIterator __k1, _InputIterator __k2) { return _M_replace(__i1, __i2, __k1, __k2, typename iterator_traits<_InputIterator>::iterator_category()); } // Specializations for the common case of pointer and iterator: // useful to avoid the overhead of temporary buffering in _M_replace. basic_string& replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2) { return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __k1, __k2 - __k1); } basic_string& replace(iterator __i1, iterator __i2, const _CharT* __k1, const _CharT* __k2) { return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __k1, __k2 - __k1); } basic_string& replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2) { return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __k1.base(), __k2 - __k1); } basic_string& replace(iterator __i1, iterator __i2, const_iterator __k1, const_iterator __k2) { return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __k1.base(), __k2 - __k1); } private: template<class _InputIterator> basic_string& _M_replace(iterator __i1, iterator __i2, _InputIterator __k1, _InputIterator __k2, input_iterator_tag); template<class _ForwardIterator> basic_string& _M_replace_safe(iterator __i1, iterator __i2, _ForwardIterator __k1, _ForwardIterator __k2); // _S_construct_aux is used to implement the 21.3.1 para 15 which // requires special behaviour if _InIter is an integral type template<class _InIter> static _CharT* _S_construct_aux(_InIter __beg, _InIter __end, const _Alloc& __a, __false_type) { typedef typename iterator_traits<_InIter>::iterator_category _Tag; return _S_construct(__beg, __end, __a, _Tag()); } template<class _InIter> static _CharT* _S_construct_aux(_InIter __beg, _InIter __end, const _Alloc& __a, __true_type) { return _S_construct(static_cast<size_type>(__beg),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -