📄 stl_string.h
字号:
}
void insert(iterator __p, size_t __n, _CharT __c);
#ifdef __STL_MEMBER_TEMPLATES
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
template <class _InputIter>
void insert(iterator __p, _InputIter __first, _InputIter __last) {
typedef typename _Is_integer<_InputIter>::_Integral _Integral;
_M_insert_dispatch(__p, __first, __last, _Integral());
}
#else /* __STL_MEMBER_TEMPLATES */
void insert(iterator __p, const _CharT* __first, const _CharT* __last);
# ifdef __STL_DEBUG
void insert(iterator __p, const_iterator __first, const_iterator __last) {
insert(__p, __first._M_iterator, __last._M_iterator);
}
# endif
#endif /* __STL_MEMBER_TEMPLATES */
private: // Helper functions for insert.
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIter>
void insert(iterator __p, _InputIter __first, _InputIter __last,
input_iterator_tag)
{
for ( ; __first != __last; ++__first) {
__p = insert(__p, *__first);
++__p;
}
}
template <class _ForwardIter>
void insert(iterator __position, _ForwardIter __first, _ForwardIter __last,
forward_iterator_tag)
# ifndef __STL_INLINE_MEMBER_TEMPLATES
;
# else
{
__stl_debug_do(__check_range(__first,__last));
if (__first != __last) {
difference_type __n = 0;
distance(__first, __last, __n);
if (_M_end_of_storage._M_data - _M_finish >= __n + 1) {
const difference_type __elems_after = _M_finish - _Make_ptr(__position);
pointer __old_finish = _M_finish;
if (__elems_after >= __n) {
uninitialized_copy((_M_finish - __n) + 1, _M_finish + 1,
_M_finish + 1);
_M_finish += __n;
_Traits::move(_Make_ptr(__position) + __n,
_Make_ptr(__position), (__elems_after - __n) + 1);
_M_copy(__first, __last, _Make_ptr(__position));
}
else {
_ForwardIter __mid = __first;
advance(__mid, __elems_after + 1);
uninitialized_copy(__mid, __last, _M_finish + 1);
_M_finish += __n - __elems_after;
__STL_TRY {
uninitialized_copy(_Make_ptr(__position), __old_finish + 1, _M_finish);
_M_finish += __elems_after;
}
__STL_UNWIND((destroy(__old_finish + 1, _M_finish),
_M_finish = __old_finish));
_M_copy(__first, __mid, _Make_ptr(__position));
}
}
else {
const size_type __old_size = size();
const size_type __len
= __old_size + max(__old_size, __STATIC_CAST(size_type,__n)) + 1;
pointer __new_start = _M_end_of_storage.allocate(__len);
pointer __new_finish = __new_start;
__STL_TRY {
__new_finish = uninitialized_copy(_M_start, _Make_ptr(__position), __new_start);
__new_finish = uninitialized_copy(__first, __last, __new_finish);
__new_finish
= uninitialized_copy(_Make_ptr(__position), _M_finish, __new_finish);
_M_construct_null(__new_finish);
}
__STL_UNWIND((destroy(__new_start,__new_finish),
_M_end_of_storage.deallocate(__new_start,__len)));
destroy(_M_start, _M_finish + 1);
_M_deallocate_block();
_M_start = __new_start;
_M_finish = __new_finish;
_M_end_of_storage._M_data = __new_start + __len;
}
}
}
# endif /* __STL_INLINE_MEMBER_TEMPLATES */
template <class _Integer>
void _M_insert_dispatch(iterator __p, _Integer __n, _Integer __x,
__true_type) {
insert(__p, (size_type) __n, (_CharT) __x);
}
template <class _InputIter>
void _M_insert_dispatch(iterator __p, _InputIter __first, _InputIter __last,
__false_type) {
__stl_debug_do(__check_range(__first, __last));
# if defined ( __STL_CLASS_PARTIAL_SPECIALIZATION )
typedef typename iterator_traits<_InputIter>::iterator_category _Category;
insert(__p, __first, __last, _Category());
# else
insert(__p, __first, __last, __ITERATOR_CATEGORY(__first));
# endif
}
template <class _InputIterator>
void
_M_copy(_InputIterator __first, _InputIterator __last, pointer __result) {
__stl_debug_do(__check_range(__first, __last));
for ( ; __first != __last; ++__first, ++__result)
_Traits::assign(*__result, *__first);
}
#endif /* __STL_MEMBER_TEMPLATES */
pointer _M_insert_aux(pointer, _CharT);
void
_M_copy(const _CharT* __first, const _CharT* __last, _CharT* __result) {
__stl_debug_do(__check_range(__first, __last));
_Traits::copy(__result, __first, __last - __first);
}
public: // Erase.
_Self& erase(size_type __pos = 0, size_type __n = npos) {
if (__pos > size())
_M_throw_out_of_range();
erase(begin() + __pos, begin() + __pos + min(__n, size() - __pos));
return *this;
}
iterator erase(iterator __position) {
__stl_debug_do(__check_if_owner(&_M_iter_list, __position));
// The move includes the terminating _CharT().
_Traits::move(_Make_ptr(__position), _Make_ptr(__position) + 1, _M_finish - _Make_ptr(__position));
__stl_debug_do(__invalidate_iterator(&_M_iter_list,end()));
destroy(_M_finish);
--_M_finish;
return __position;
}
iterator erase(iterator __first, iterator __last) {
__stl_debug_do(__check_range(__first, __last)&&__check_if_owner(&_M_iter_list,__first));
if (__first != __last) {
// The move includes the terminating _CharT().
_Traits::move(_Make_ptr(__first), _Make_ptr(__last), (_M_finish - _Make_ptr(__last)) + 1);
pointer __new_finish = _M_finish - (__last - __first);
destroy(__new_finish + 1, _M_finish + 1);
__stl_debug_do(__invalidate_range(&_M_iter_list, _Make_iterator(__new_finish+1), end()));
_M_finish = __new_finish;
}
return __first;
}
public: // Replace. (Conceptually equivalent
// to erase followed by insert.)
_Self& replace(size_type __pos, size_type __n,
const _Self& __s) {
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n, size() - __pos);
if (size() - __len >= max_size() - __s.size())
_M_throw_length_error();
return replace(begin() + __pos, begin() + __pos + __len,
__s._M_start, __s._M_finish);
}
_Self& replace(size_type __pos1, size_type __n1,
const _Self& __s,
size_type __pos2, size_type __n2) {
if (__pos1 > size() || __pos2 > __s.size())
_M_throw_out_of_range();
const size_type __len1 = min(__n1, size() - __pos1);
const size_type __len2 = min(__n2, __s.size() - __pos2);
if (size() - __len1 >= max_size() - __len2)
_M_throw_length_error();
return replace(begin() + __pos1, begin() + __pos1 + __len1,
__s._M_start + __pos2, __s._M_start + __pos2 + __len2);
}
_Self& replace(size_type __pos, size_type __n1,
const _CharT* __s, size_type __n2) {
__STL_FIX_LITERAL_BUG(__s)
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n1, size() - __pos);
if (__n2 > max_size() || size() - __len >= max_size() - __n2)
_M_throw_length_error();
return replace(begin() + __pos, begin() + __pos + __len,
__s, __s + __n2);
}
_Self& replace(size_type __pos, size_type __n1,
const _CharT* __s) {
__STL_FIX_LITERAL_BUG(__s)
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n1, size() - __pos);
const size_type __n2 = _Traits::length(__s);
if (__n2 > max_size() || size() - __len >= max_size() - __n2)
_M_throw_length_error();
return replace(begin() + __pos, begin() + __pos + __len,
__s, __s + _Traits::length(__s));
}
_Self& replace(size_type __pos, size_type __n1,
size_type __n2, _CharT __c) {
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n1, size() - __pos);
if (__n2 > max_size() || size() - __len >= max_size() - __n2)
_M_throw_length_error();
return replace(begin() + __pos, begin() + __pos + __len, __n2, __c);
}
_Self& replace(iterator __first, iterator __last,
const _Self& __s)
{ return replace(__first, __last, __s._M_start, __s._M_finish); }
_Self& replace(iterator __first, iterator __last,
const _CharT* __s, size_type __n)
{ __STL_FIX_LITERAL_BUG(__s) return replace(__first, __last, __s, __s + __n); }
_Self& replace(iterator __first, iterator __last,
const _CharT* __s) {
__STL_FIX_LITERAL_BUG(__s)
return replace(__first, __last, __s, __s + _Traits::length(__s));
}
_Self& replace(iterator __first, iterator __last,
size_type __n, _CharT __c);
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIter>
_Self& replace(iterator __first, iterator __last,
_InputIter __f, _InputIter __l) {
typedef typename _Is_integer<_InputIter>::_Integral _Integral;
return _M_replace_dispatch(__first, __last, __f, __l, _Integral());
}
#else /* __STL_MEMBER_TEMPLATES */
_Self& replace(iterator __first, iterator __last,
const _CharT* __f, const _CharT* __l);
#endif /* __STL_MEMBER_TEMPLATES */
private: // Helper functions for replace.
#ifdef __STL_MEMBER_TEMPLATES
template <class _Integer>
_Self& _M_replace_dispatch(iterator __first, iterator __last,
_Integer __n, _Integer __x,
__true_type) {
return replace(__first, __last, (size_type) __n, (_CharT) __x);
}
template <class _InputIter>
_Self& _M_replace_dispatch(iterator __first, iterator __last,
_InputIter __f, _InputIter __l,
__false_type) {
__stl_debug_do(__check_if_owner(__first) && __check_range(__first, __last)
&& __check_range(__f, __l));
# ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
typedef typename iterator_traits<_InputIter>::iterator_category _Category;
return replace(__first, __last, __f, __l, _Category());
# else
return replace(__first, __last, __f, __l, __ITERATOR_CATEGORY(__f));
# endif
}
template <class _InputIter>
_Self& replace(iterator __first, iterator __last,
_InputIter __f, _InputIter __l, input_iterator_tag) {
for ( ; __first != __last && __f != __l; ++__first, ++__f)
_Traits::assign(*__first, *__f);
if (__f == __l)
erase(__first, __last);
else
insert(__last, __f, __l);
return *this;
}
template <class _ForwardIter>
_Self& replace(iterator __first, iterator __last,
_ForwardIter __f, _ForwardIter __l,
forward_iterator_tag) {
difference_type __n = 0;
distance(__f, __l, __n);
const difference_type __len = __last - __first;
if (__len >= __n) {
_M_copy(__f, __l, __first);
erase(__first + __n, __last);
}
else {
_ForwardIter __m = __f;
advance(__m, __len);
_M_copy(__f, __m, __first);
insert(__last, __m, __l);
}
return *this;
}
#endif /* __STL_MEMBER_TEMPLATES */
public: // Other modifier member functions.
size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const {
__STL_FIX_LITERAL_BUG(__s)
if (__pos > size())
_M_throw_out_of_range();
const size_type __len = min(__n, size() - __pos);
_Traits::copy(__s, _M_start + __pos, __len);
return __len;
}
void swap(_Self& __s) {
__stl_debug_do(_M_iter_list._Swap_owners(__s._M_iter_list));
__STLPORT_STD::swap(_M_start, __s._M_start);
__STLPORT_STD::swap(_M_finish, __s._M_finish);
__STLPORT_STD::swap(_M_end_of_storage, __s._M_end_of_storage);
}
public: // Conversion to C string.
const _CharT* c_str() const { return _M_start; }
const _CharT* data() const { return _M_start; }
public: // find.
size_type find(const _Self& __s, size_type __pos = 0) const
{ return find(__s._M_start, __pos, __s.size()); }
size_type find(const _CharT* __s, size_type __pos = 0) const
{ __STL_FIX_LITERAL_BUG(__s) return find(__s, __pos, _Traits::length(__s)); }
size_type find(const _CharT* __s, size_type __pos, size_type __n) const;
size_type find(_CharT __c, size_type __pos = 0) const;
public: // rfind.
size_type rfind(const _Self& __s, size_type __pos = npos) const
{ return rfind(__s._M_start, __pos, __s.size()); }
size_type rfind(const _CharT* __s, size_type __pos = npos) const
{ return rfind(__s, __pos, _Traits::length(__s)); }
size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const;
size_type rfind(_CharT __c, size_type __pos = npos) const;
public: // find_first_of
size_type find_first_of(const _Self& __s, size_type __pos = 0) const
{ return find_first_of(__s._M_start, __pos, __s.size()); }
size_type find_first_of(const _CharT* __s, size_type __pos = 0) const
{ __STL_FIX_LITERAL_BUG(__s) return find_first_of(__s, __pos, _Traits::length(__s)); }
size_type find_first_of(const _CharT* __s, size_type __pos,
size_type __n) const;
size_type find_first_of(_CharT __c, size_type __pos = 0) const
{ return find(__c, __pos); }
public: // find_last_of
size_type find_last_of(const _Self& __s,
size_type __pos = npos) const
{ return find_last_of(__s._M_start, __pos, __s.size()); }
size_type find_last_of(const _CharT* __s, size_type __pos = npos) const
{ __STL_FIX_LITERAL_BUG(__s) return find_last_of(__s, __pos, _Traits::length(__s)); }
size_type find_last_of(const _CharT* __s, size_type __pos,
size_type __n) const;
size_type find_last_of(_CharT __c, size_type __pos = npos) const {
return rfind(__c, __pos);
}
public: // find_first_not_of
size_type find_first_not_of(const _Self& __s,
size_type __pos = 0) const
{ return find_first_not_of(__s._M_start, __pos, __s.size()); }
size_type find_first_not_of(const _CharT* __s, size_type __pos = 0) const
{ __STL_FIX_LITERAL_BUG(__s) return find_first_not_of(__s, __pos, _Traits::length(__s)); }
size_type find_first_not_of(const _CharT* __s, size_type __pos,
size_type __n) const;
size_type find_first_not_of(_CharT __c, size_type __pos = 0) const;
public: // find_last_not_of
size_type find_last_not_of(const _Self& __s,
size_type __pos = npos) const
{ return find_last_not_of(__s._M_start, __pos, __s.size()); }
size_type find_last_not_of(const _CharT* __s, size_type __pos = npos) const
{ __STL_FIX_LITERAL_BUG(__s) return find_last_not_of(__s, __pos, _Traits::length(__s)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -