WWW.DISSERS.RU

БЕСПЛАТНАЯ ЭЛЕКТРОННАЯ БИБЛИОТЕКА

   Добро пожаловать!


Pages:     | 1 |   ...   | 11 | 12 || 14 | 15 |   ...   | 16 |

// (5) Each iterator in [finish + 1, end_of_storage) points to // unininitialized memory.

// Note one important consequence: a string of length n must manage // a block of memory whose size is at least n + 1.

template class basic_string : private _String_base<_CharT,_Alloc> { public:

typedef _CharT value_type;

typedef _Traits traits_type;

typedef value_type* pointer;

typedef const value_type* const_pointer;

typedef value_type& reference;

typedef const value_type& const_reference;

typedef size_t size_type;

typedef ptrdiff_t difference_type;

typedef const value_type* const_iterator;

typedef value_type* iterator;

#ifdef STL_CLASS_PARTIAL_SPECIALIZATION typedef reverse_iterator const_reverse_iterator;

typedef reverse_iterator reverse_iterator;

#else /* STL_CLASS_PARTIAL_SPECIALIZATION */ typedef reverse_iterator const_reverse_iterator;

typedef reverse_iterator reverse_iterator;

#endif /* STL_PARTIAL_SPECIALIZATION */ static const size_type npos;

typedef _String_base<_CharT,_Alloc> _Base;

public: // Constructor, destructor, assignment.

typedef typename _Base::allocator_type allocator_type;

allocator_type get_allocator() const { return _Base::get_allocator(); } explicit basic_string(const allocator_type& a = allocator_type()) : _Base(a, 8) { _M_terminate_string(); } struct _Reserve_t {};

basic_string(_Reserve_t, size_t n, const allocator_type& a = allocator_type()) : _Base(a, n + 1) { _M_terminate_string(); } basic_string(const basic_string& s) : _Base(s.get_allocator()) { _M_range_initialize(s.begin(), s.end()); } basic_string(const basic_string& s, size_type pos, size_type n = npos, const allocator_type& a = allocator_type()) - 101 - УМП «Автоматизированные методы разработки архитектуры ПО» : _Base(a) { if (pos > s.size()) _M_throw_out_of_range();

else _M_range_initialize(s.begin() + pos, s.begin() + pos + min(n, s.size() - pos));

} basic_string(const _CharT* s, size_type n, const allocator_type& a = allocator_type()) : _Base(a) { _M_range_initialize(s, s + n); } basic_string(const _CharT* s, const allocator_type& a = allocator_type()) : _Base(a) { _M_range_initialize(s, s + _Traits::length(s)); } basic_string(size_type n, _CharT c, const allocator_type& a = allocator_type()) : _Base(a, n + 1) { _M_finish = uninitialized_fill_n(_M_start, n, c);

_M_terminate_string();

} // Check to see if _InputIterator is an integer type. If so, then // it can't be an iterator.

#ifdef STL_MEMBER_TEMPLATES template basic_string(_InputIterator f, _InputIterator l, const allocator_type& a = allocator_type()) : _Base(a) { typedef typename _Is_integer<_InputIterator>::_Integral _Integral;

_M_initialize_dispatch(f, l, _Integral());

} #else /* STL_MEMBER_TEMPLATES */ basic_string(const _CharT* f, const _CharT* l, const allocator_type& a = allocator_type()) : _Base(a) { _M_range_initialize(f, l);

} #endif ~basic_string() { destroy(_M_start, _M_finish + 1); } basic_string& operator=(const basic_string& s) { if (&s != this) assign(s.begin(), s.end());

return *this;

} basic_string& operator=(const _CharT* s) { return assign(s, s + _Traits::length(s)); } basic_string& operator=(_CharT c) { return assign(static_cast(1), c); } protected: // Protected members inherited from base.

#ifdef STL_HAS_NAMESPACES using _Base::_M_allocate;

using _Base::_M_deallocate;

using _Base::_M_allocate_block;

using _Base::_M_deallocate_block;

using _Base::_M_throw_length_error;

using _Base::_M_throw_out_of_range;

using _Base::_M_start;

using _Base::_M_finish;

using _Base::_M_end_of_storage;

#endif /* STL_HAS_NAMESPACES */ private: // Helper functions used by constructors // and elsewhere.

void _M_construct_null(_CharT* p) { construct(p);

# ifdef STL_DEFAULT_CONSTRUCTOR_BUG STL_TRY { - 102 - УМП «Автоматизированные методы разработки архитектуры ПО» *p = (_CharT) 0;

} STL_UNWIND(destroy(p));

# endif } static _CharT _M_null() { # ifndef STL_DEFAULT_CONSTRUCTOR_BUG return _CharT();

# else return (_CharT) 0;

# endif } private:

// Helper functions used by constructors. It is a severe error for // any of them to be called anywhere except from within constructors.

void _M_terminate_string() { STL_TRY { _M_construct_null(_M_finish);

} STL_UNWIND(destroy(_M_start, _M_finish));

} #ifdef STL_MEMBER_TEMPLATES template void _M_range_initialize(_InputIter f, _InputIter l, input_iterator_tag) { _M_allocate_block(8);

_M_construct_null(_M_finish);

STL_TRY { append(f, l);

} STL_UNWIND(destroy(_M_start, _M_finish + 1));

} template void _M_range_initialize(_ForwardIter f, _ForwardIter l, forward_iterator_tag) { difference_type n = 0;

distance(f, l, n);

_M_allocate_block(n + 1);

_M_finish = uninitialized_copy(f, l, _M_start);

_M_terminate_string();

} template void _M_range_initialize(_InputIter f, _InputIter l) { typedef typename iterator_traits<_InputIter>::iterator_category _Category;

_M_range_initialize(f, l, _Category());

} template void _M_initialize_dispatch(_Integer n, _Integer x, true_type) { _M_allocate_block(n + 1);

_M_finish = uninitialized_fill_n(_M_start, n, x);

_M_terminate_string();

} template void _M_initialize_dispatch(_InputIter f, _InputIter l, false_type) { _M_range_initialize(f, l);

} #else /* STL_MEMBER_TEMPLATES */ void _M_range_initialize(const _CharT* f, const _CharT* l) { ptrdiff_t n = l - f;

_M_allocate_block(n + 1);

_M_finish = uninitialized_copy(f, l, _M_start);

_M_terminate_string();

} #endif /* STL_MEMBER_TEMPLATES */ public: // Iterators.

iterator begin() { return _M_start; } iterator end() { return _M_finish; } - 103 - УМП «Автоматизированные методы разработки архитектуры ПО» const_iterator begin() const { return _M_start; } const_iterator end() const { return _M_finish; } reverse_iterator rbegin() { return reverse_iterator(_M_finish); } reverse_iterator rend() { return reverse_iterator(_M_start); } const_reverse_iterator rbegin() const { return const_reverse_iterator(_M_finish); } const_reverse_iterator rend() const { return const_reverse_iterator(_M_start); } public: // Size, capacity, etc.

size_type size() const { return _M_finish - _M_start; } size_type length() const { return size(); } size_t max_size() const { return _Base::max_size(); } void resize(size_type n, _CharT c) { if (n <= size()) erase(begin() + n, end());

else append(n - size(), c);

} void resize(size_type n) { resize(n, _M_null()); } void reserve(size_type = 0);

size_type capacity() const { return (_M_end_of_storage - _M_start) - 1; } void clear() { if (!empty()) { _Traits::assign(*_M_start, _M_null());

destroy(_M_start+1, _M_finish+1);

_M_finish = _M_start;

} } bool empty() const { return _M_start == _M_finish; } public: // Element access.

const_reference operator[](size_type n) const { return *(_M_start + n); } reference operator[](size_type n) { return *(_M_start + n); } const_reference at(size_type n) const { if (n >= size()) _M_throw_out_of_range();

return *(_M_start + n);

} reference at(size_type n) { if (n >= size()) _M_throw_out_of_range();

return *(_M_start + n);

} public: // Append, operator+=, push_back.

basic_string& operator+=(const basic_string& s) { return append(s); } basic_string& operator+=(const _CharT* s) { return append(s); } basic_string& operator+=(_CharT c) { push_back(c); return *this; } basic_string& append(const basic_string& s) { return append(s.begin(), s.end()); } basic_string& append(const basic_string& s, size_type pos, size_type n) { if (pos > s.size()) _M_throw_out_of_range();

return append(s.begin() + pos, s.begin() + pos + min(n, s.size() - pos));

} basic_string& append(const _CharT* s, size_type n) - 104 - УМП «Автоматизированные методы разработки архитектуры ПО» { return append(s, s+n); } basic_string& append(const _CharT* s) { return append(s, s + _Traits::length(s)); } basic_string& append(size_type 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 basic_string& append(_InputIter first, _InputIter last) { typedef typename _Is_integer<_InputIter>::_Integral _Integral;

return _M_append_dispatch(first, last, _Integral());

} #else /* STL_MEMBER_TEMPLATES */ basic_string& append(const _CharT* first, const _CharT* last);

#endif /* STL_MEMBER_TEMPLATES */ void push_back(_CharT c) { if (_M_finish + 1 == _M_end_of_storage) reserve(size() + max(size(), static_cast(1)));

_M_construct_null(_M_finish + 1);

_Traits::assign(*_M_finish, c);

++_M_finish;

} void pop_back() { _Traits::assign(*(_M_finish - 1), _M_null());

destroy(_M_finish);

--_M_finish;

} private: // Helper functions for append.

#ifdef STL_MEMBER_TEMPLATES template basic_string& append(_InputIter f, _InputIter l, input_iterator_tag);

template basic_string& append(_ForwardIter f, _ForwardIter l, forward_iterator_tag);

template basic_string& _M_append_dispatch(_Integer n, _Integer x, true_type) { return append((size_type) n, (_CharT) x);

} template basic_string& _M_append_dispatch(_InputIter f, _InputIter l, false_type) { typedef typename iterator_traits<_InputIter>::iterator_category _Category;

return append(f, l, _Category());

} #endif /* STL_MEMBER_TEMPLATES */ public: // Assign basic_string& assign(const basic_string& s) { return assign(s.begin(), s.end()); } basic_string& assign(const basic_string& s, size_type pos, size_type n) { if (pos > s.size()) _M_throw_out_of_range();

return assign(s.begin() + pos, s.begin() + pos + min(n, s.size() - pos));

} basic_string& assign(const _CharT* s, size_type n) { return assign(s, s + n); } basic_string& assign(const _CharT* s) { return assign(s, s + _Traits::length(s)); } - 105 - УМП «Автоматизированные методы разработки архитектуры ПО» basic_string& assign(size_type 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 basic_string& assign(_InputIter first, _InputIter last) { typedef typename _Is_integer<_InputIter>::_Integral _Integral;

return _M_assign_dispatch(first, last, _Integral());

} #endif /* STL_MEMBER_TEMPLATES */ basic_string& assign(const _CharT* f, const _CharT* l);

private: // Helper functions for assign.

#ifdef STL_MEMBER_TEMPLATES template basic_string& _M_assign_dispatch(_Integer n, _Integer x, true_type) { return assign((size_type) n, (_CharT) x);

} template basic_string& _M_assign_dispatch(_InputIter f, _InputIter l, false_type);

#endif /* STL_MEMBER_TEMPLATES */ public: // Insert basic_string& insert(size_type pos, const basic_string& s) { if (pos > size()) _M_throw_out_of_range();

if (size() > max_size() - s.size()) _M_throw_length_error();

insert(_M_start + pos, s.begin(), s.end());

return *this;

} basic_string& insert(size_type pos, const basic_string& s, size_type beg, size_type n) { if (pos > size() || beg > s.size()) _M_throw_out_of_range();

size_type len = min(n, s.size() - beg);

if (size() > max_size() - len) _M_throw_length_error();

insert(_M_start + pos, s.begin() + beg, s.begin() + beg + len);

return *this;

} basic_string& insert(size_type pos, const _CharT* s, size_type n) { if (pos > size()) _M_throw_out_of_range();

if (size() > max_size() - n) _M_throw_length_error();

insert(_M_start + pos, s, s + n);

return *this;

} basic_string& insert(size_type pos, const _CharT* s) { if (pos > size()) _M_throw_out_of_range();

size_type len = _Traits::length(s);

if (size() > max_size() - len) _M_throw_length_error();

insert(_M_start + pos, s, s + len);

return *this;

} basic_string& insert(size_type pos, size_type n, _CharT c) { if (pos > size()) _M_throw_out_of_range();

if (size() > max_size() - n) _M_throw_length_error();

insert(_M_start + pos, n, c);

- 106 - УМП «Автоматизированные методы разработки архитектуры ПО» return *this;

} iterator insert(iterator p, _CharT c) { if (p == _M_finish) { push_back(c);

return _M_finish - 1;

} else return _M_insert_aux(p, c);

} 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 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);

#endif /* STL_MEMBER_TEMPLATES */ private: // Helper functions for insert.

#ifdef STL_MEMBER_TEMPLATES template void insert(iterator p, _InputIter, _InputIter, input_iterator_tag);

template void insert(iterator p, _ForwardIter, _ForwardIter, forward_iterator_tag);

template void _M_insert_dispatch(iterator p, _Integer n, _Integer x, true_type) { insert(p, (size_type) n, (_CharT) x);

} template void _M_insert_dispatch(iterator p, _InputIter first, _InputIter last, false_type) { typedef typename iterator_traits<_InputIter>::iterator_category _Category;

insert(p, first, last, _Category());

} template void _M_copy(_InputIterator first, _InputIterator last, iterator result) { for ( ; first != last; ++first, ++result) _Traits::assign(*result, *first);

} #endif /* STL_MEMBER_TEMPLATES */ iterator _M_insert_aux(iterator, _CharT);

void _M_copy(const _CharT* first, const _CharT* last, _CharT* result) { _Traits::copy(result, first, last - first);

} public: // Erase.

basic_string& erase(size_type pos = 0, size_type n = npos) { if (pos > size()) _M_throw_out_of_range();

erase(_M_start + pos, _M_start + pos + min(n, size() - pos));

return *this;

} iterator erase(iterator position) { - 107 - УМП «Автоматизированные методы разработки архитектуры ПО» // The move includes the terminating null.

_Traits::move(position, position + 1, _M_finish - position);

destroy(_M_finish);

--_M_finish;

return position;

} iterator erase(iterator first, iterator last) { if (first != last) { // The move includes the terminating null.

_Traits::move(first, last, (_M_finish - last) + 1);

const iterator new_finish = _M_finish - (last - first);

destroy(new_finish + 1, _M_finish + 1);

_M_finish = new_finish;

} return first;

} public: // Replace. (Conceptually equivalent // to erase followed by insert.) basic_string& replace(size_type pos, size_type n, const basic_string& 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(_M_start + pos, _M_start + pos + len, s.begin(), s.end());

} basic_string& replace(size_type pos1, size_type n1, const basic_string& s, size_type pos2, size_type n2) { if (pos1 > size() || pos2 > s.size()) _M_throw_out_of_range();

Pages:     | 1 |   ...   | 11 | 12 || 14 | 15 |   ...   | 16 |






















© 2011 www.dissers.ru - «Бесплатная электронная библиотека»

Материалы этого сайта размещены для ознакомления, все права принадлежат их авторам.
Если Вы не согласны с тем, что Ваш материал размещён на этом сайте, пожалуйста, напишите нам, мы в течении 1-2 рабочих дней удалим его.