1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 21 Strings library
35 /** @file basic_string.h
36 * This is an internal header file, included by other library headers.
37 * You should not attempt to use it directly.
40 #ifndef _BASIC_STRING_H
41 #define _BASIC_STRING_H 1
43 #pragma GCC system_header
45 #include <bits/atomicity.h>
46 #include <debug/debug.h>
51 * @class basic_string basic_string.h <string>
52 * @brief Managing sequences of characters and character-like objects.
57 * Meets the requirements of a <a href="tables.html#65">container</a>, a
58 * <a href="tables.html#66">reversible container</a>, and a
59 * <a href="tables.html#67">sequence</a>. Of the
60 * <a href="tables.html#68">optional sequence requirements</a>, only
61 * @c push_back, @c at, and array access are supported.
67 * Documentation? What's that?
68 * Nathan Myers <ncm@cantrip.org>.
70 * A string looks like this:
75 * [basic_string<char_type>] _M_capacity
76 * _M_dataplus _M_refcount
77 * _M_p ----------------> unnamed array of char_type
80 * Where the _M_p points to the first character in the string, and
81 * you cast it to a pointer-to-_Rep and subtract 1 to get a
82 * pointer to the header.
84 * This approach has the enormous advantage that a string object
85 * requires only one allocation. All the ugliness is confined
86 * within a single pair of inline functions, which each compile to
87 * a single "add" instruction: _Rep::_M_data(), and
88 * string::_M_rep(); and the allocation function which gets a
89 * block of raw bytes and with room enough and constructs a _Rep
90 * object at the front.
92 * The reason you want _M_data pointing to the character array and
93 * not the _Rep is so that the debugger can see the string
94 * contents. (Probably we should add a non-inline member to get
95 * the _Rep for the debugger to use, so users can check the actual
98 * Note that the _Rep object is a POD so that you can have a
99 * static "empty string" _Rep object already "constructed" before
100 * static constructors have run. The reference-count encoding is
101 * chosen so that a 0 indicates one reference, so you never try to
102 * destroy the empty-string _Rep object.
104 * All but the last paragraph is considered pretty conventional
105 * for a C++ string implementation.
108 // 21.3 Template class basic_string
109 template<typename _CharT, typename _Traits, typename _Alloc>
114 typedef _Traits traits_type;
115 typedef typename _Traits::char_type value_type;
116 typedef _Alloc allocator_type;
117 typedef typename _Alloc::size_type size_type;
118 typedef typename _Alloc::difference_type difference_type;
119 typedef typename _Alloc::reference reference;
120 typedef typename _Alloc::const_reference const_reference;
121 typedef typename _Alloc::pointer pointer;
122 typedef typename _Alloc::const_pointer const_pointer;
123 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
124 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
126 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
127 typedef std::reverse_iterator<iterator> reverse_iterator;
130 // _Rep: string representation
132 // 1. String really contains _M_length + 1 characters: due to 21.3.4
133 // must be kept null-terminated.
134 // 2. _M_capacity >= _M_length
135 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
136 // 3. _M_refcount has three states:
137 // -1: leaked, one reference, no ref-copies allowed, non-const.
138 // 0: one reference, non-const.
139 // n>0: n + 1 references, operations require a lock, const.
140 // 4. All fields==0 is an empty string, given the extra storage
141 // beyond-the-end for a null terminator; thus, the shared
142 // empty string representation needs no constructor.
147 size_type _M_capacity;
148 _Atomic_word _M_refcount;
151 struct _Rep : _Rep_base
154 typedef typename _Alloc::template rebind<size_type>::other _Raw_alloc;
156 // (Public) Data members:
158 // The maximum number of individual char_type elements of an
159 // individual string is determined by _S_max_size. This is the
160 // value that will be returned by max_size(). (Whereas npos
161 // is the maximum number of bytes the allocator can allocate.)
162 // If one was to divvy up the theoretical largest size string,
163 // with a terminating character and m _CharT elements, it'd
165 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
167 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
168 // In addition, this implementation quarters this amount.
169 static const size_type _S_max_size;
170 static const _CharT _S_terminal;
172 // The following storage is init'd to 0 by the linker, resulting
173 // (carefully) in an empty string with one reference.
174 static size_type _S_empty_rep_storage[];
178 { return *reinterpret_cast<_Rep*>(&_S_empty_rep_storage); }
182 { return this->_M_refcount < 0; }
186 { return this->_M_refcount > 0; }
190 { this->_M_refcount = -1; }
194 { this->_M_refcount = 0; }
197 _M_set_length_and_sharable(size_type __n)
199 this->_M_set_sharable(); // One reference.
200 this->_M_length = __n;
201 this->_M_refdata()[__n] = _S_terminal; // grrr. (per 21.3.4)
202 // You cannot leave those LWG people alone for a second.
207 { return reinterpret_cast<_CharT*>(this + 1); }
210 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
212 return (!_M_is_leaked() && __alloc1 == __alloc2)
213 ? _M_refcopy() : _M_clone(__alloc1);
218 _S_create(size_type, size_type, const _Alloc&);
221 _M_dispose(const _Alloc& __a)
223 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
224 if (__builtin_expect(this != &_S_empty_rep(), false))
226 if (__gnu_cxx::__exchange_and_add(&this->_M_refcount, -1) <= 0)
231 _M_destroy(const _Alloc&) throw();
236 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
237 if (__builtin_expect(this != &_S_empty_rep(), false))
239 __gnu_cxx::__atomic_add(&this->_M_refcount, 1);
244 _M_clone(const _Alloc&, size_type __res = 0);
247 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
248 struct _Alloc_hider : _Alloc
250 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
251 : _Alloc(__a), _M_p(__dat) { }
253 _CharT* _M_p; // The actual data.
257 // Data Members (public):
258 // NB: This is an unsigned type, and thus represents the maximum
259 // size that the allocator can hold.
260 /// Value returned by various member functions when they fail.
261 static const size_type npos = static_cast<size_type>(-1);
264 // Data Members (private):
265 mutable _Alloc_hider _M_dataplus;
269 { return _M_dataplus._M_p; }
273 { return (_M_dataplus._M_p = __p); }
277 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
279 // For the internal use we have functions similar to `begin'/`end'
280 // but they do not call _M_leak.
283 { return iterator(_M_data()); }
287 { return iterator(_M_data() + this->size()); }
290 _M_leak() // for use in begin() & non-const op[]
292 if (!_M_rep()->_M_is_leaked())
297 _M_check(size_type __pos, const char* __s) const
299 if (__pos > this->size())
300 __throw_out_of_range(__N(__s));
305 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
307 if (this->max_size() - (this->size() - __n1) < __n2)
308 __throw_length_error(__N(__s));
311 // NB: _M_limit doesn't check for a bad __pos value.
313 _M_limit(size_type __pos, size_type __off) const
315 const bool __testoff = __off < this->size() - __pos;
316 return __testoff ? __off : this->size() - __pos;
319 // True if _Rep and source do not overlap.
321 _M_disjunct(const _CharT* __s) const
323 return (less<const _CharT*>()(__s, _M_data())
324 || less<const _CharT*>()(_M_data() + this->size(), __s));
327 // When __n = 1 way faster than the general multichar
328 // traits_type::copy/move/assign.
330 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
333 traits_type::assign(*__d, *__s);
335 traits_type::copy(__d, __s, __n);
339 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
342 traits_type::assign(*__d, *__s);
344 traits_type::move(__d, __s, __n);
348 _M_assign(_CharT* __d, size_type __n, _CharT __c)
351 traits_type::assign(*__d, __c);
353 traits_type::assign(__d, __n, __c);
356 // _S_copy_chars is a separate template to permit specialization
357 // to optimize for the common case of pointers as iterators.
358 template<class _Iterator>
360 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
362 for (; __k1 != __k2; ++__k1, ++__p)
363 traits_type::assign(*__p, *__k1); // These types are off.
367 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
368 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
371 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
372 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
375 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
376 { _M_copy(__p, __k1, __k2 - __k1); }
379 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
380 { _M_copy(__p, __k1, __k2 - __k1); }
383 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
390 { return _Rep::_S_empty_rep(); }
393 // Construct/copy/destroy:
394 // NB: We overload ctors in some cases instead of using default
395 // arguments, per 17.4.4.4 para. 2 item 2.
398 * @brief Default constructor creates an empty string.
404 * @brief Construct an empty string using allocator a.
407 basic_string(const _Alloc& __a);
409 // NB: per LWG issue 42, semantics different from IS:
411 * @brief Construct string with copy of value of @a str.
412 * @param str Source string.
414 basic_string(const basic_string& __str);
416 * @brief Construct string as copy of a substring.
417 * @param str Source string.
418 * @param pos Index of first character to copy from.
419 * @param n Number of characters to copy (default remainder).
421 basic_string(const basic_string& __str, size_type __pos,
422 size_type __n = npos);
424 * @brief Construct string as copy of a substring.
425 * @param str Source string.
426 * @param pos Index of first character to copy from.
427 * @param n Number of characters to copy.
428 * @param a Allocator to use.
430 basic_string(const basic_string& __str, size_type __pos,
431 size_type __n, const _Alloc& __a);
434 * @brief Construct string initialized by a character array.
435 * @param s Source character array.
436 * @param n Number of characters to copy.
437 * @param a Allocator to use (default is default allocator).
439 * NB: s must have at least n characters, '\0' has no special
442 basic_string(const _CharT* __s, size_type __n,
443 const _Alloc& __a = _Alloc());
445 * @brief Construct string as copy of a C string.
446 * @param s Source C string.
447 * @param a Allocator to use (default is default allocator).
449 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
451 * @brief Construct string as multiple characters.
452 * @param n Number of characters.
453 * @param c Character to use.
454 * @param a Allocator to use (default is default allocator).
456 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
459 * @brief Construct string as copy of a range.
460 * @param beg Start of range.
461 * @param end End of range.
462 * @param a Allocator to use (default is default allocator).
464 template<class _InputIterator>
465 basic_string(_InputIterator __beg, _InputIterator __end,
466 const _Alloc& __a = _Alloc());
469 * @brief Destroy the string instance.
472 { _M_rep()->_M_dispose(this->get_allocator()); }
475 * @brief Assign the value of @a str to this string.
476 * @param str Source string.
479 operator=(const basic_string& __str)
480 { return this->assign(__str); }
483 * @brief Copy contents of @a s into this string.
484 * @param s Source null-terminated string.
487 operator=(const _CharT* __s)
488 { return this->assign(__s); }
491 * @brief Set value to string of length 1.
492 * @param c Source character.
494 * Assigning to a character makes this string length 1 and
495 * (*this)[0] == @a c.
498 operator=(_CharT __c)
500 this->assign(1, __c);
506 * Returns a read/write iterator that points to the first character in
507 * the %string. Unshares the string.
513 return iterator(_M_data());
517 * Returns a read-only (constant) iterator that points to the first
518 * character in the %string.
522 { return const_iterator(_M_data()); }
525 * Returns a read/write iterator that points one past the last
526 * character in the %string. Unshares the string.
532 return iterator(_M_data() + this->size());
536 * Returns a read-only (constant) iterator that points one past the
537 * last character in the %string.
541 { return const_iterator(_M_data() + this->size()); }
544 * Returns a read/write reverse iterator that points to the last
545 * character in the %string. Iteration is done in reverse element
546 * order. Unshares the string.
550 { return reverse_iterator(this->end()); }
553 * Returns a read-only (constant) reverse iterator that points
554 * to the last character in the %string. Iteration is done in
555 * reverse element order.
557 const_reverse_iterator
559 { return const_reverse_iterator(this->end()); }
562 * Returns a read/write reverse iterator that points to one before the
563 * first character in the %string. Iteration is done in reverse
564 * element order. Unshares the string.
568 { return reverse_iterator(this->begin()); }
571 * Returns a read-only (constant) reverse iterator that points
572 * to one before the first character in the %string. Iteration
573 * is done in reverse element order.
575 const_reverse_iterator
577 { return const_reverse_iterator(this->begin()); }
581 /// Returns the number of characters in the string, not including any
582 /// null-termination.
585 { return _M_rep()->_M_length; }
587 /// Returns the number of characters in the string, not including any
588 /// null-termination.
591 { return _M_rep()->_M_length; }
593 /// Returns the size() of the largest possible %string.
596 { return _Rep::_S_max_size; }
599 * @brief Resizes the %string to the specified number of characters.
600 * @param n Number of characters the %string should contain.
601 * @param c Character to fill any new elements.
603 * This function will %resize the %string to the specified
604 * number of characters. If the number is smaller than the
605 * %string's current size the %string is truncated, otherwise
606 * the %string is extended and new elements are set to @a c.
609 resize(size_type __n, _CharT __c);
612 * @brief Resizes the %string to the specified number of characters.
613 * @param n Number of characters the %string should contain.
615 * This function will resize the %string to the specified length. If
616 * the new size is smaller than the %string's current size the %string
617 * is truncated, otherwise the %string is extended and new characters
618 * are default-constructed. For basic types such as char, this means
622 resize(size_type __n)
623 { this->resize(__n, _CharT()); }
626 * Returns the total number of characters that the %string can hold
627 * before needing to allocate more memory.
631 { return _M_rep()->_M_capacity; }
634 * @brief Attempt to preallocate enough memory for specified number of
636 * @param n Number of characters required.
637 * @throw std::length_error If @a n exceeds @c max_size().
639 * This function attempts to reserve enough memory for the
640 * %string to hold the specified number of characters. If the
641 * number requested is more than max_size(), length_error is
644 * The advantage of this function is that if optimal code is a
645 * necessity and the user can determine the string length that will be
646 * required, the user can reserve the memory in %advance, and thus
647 * prevent a possible reallocation of memory and copying of %string
651 reserve(size_type __res_arg = 0);
654 * Erases the string, making it empty.
658 { _M_mutate(0, this->size(), 0); }
661 * Returns true if the %string is empty. Equivalent to *this == "".
665 { return this->size() == 0; }
669 * @brief Subscript access to the data contained in the %string.
670 * @param n The index of the character to access.
671 * @return Read-only (constant) reference to the character.
673 * This operator allows for easy, array-style, data access.
674 * Note that data access with this operator is unchecked and
675 * out_of_range lookups are not defined. (For checked lookups
679 operator[] (size_type __pos) const
681 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
682 return _M_data()[__pos];
686 * @brief Subscript access to the data contained in the %string.
687 * @param n The index of the character to access.
688 * @return Read/write reference to the character.
690 * This operator allows for easy, array-style, data access.
691 * Note that data access with this operator is unchecked and
692 * out_of_range lookups are not defined. (For checked lookups
693 * see at().) Unshares the string.
696 operator[](size_type __pos)
698 // allow pos == size() as v3 extension:
699 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
700 // but be strict in pedantic mode:
701 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
703 return _M_data()[__pos];
707 * @brief Provides access to the data contained in the %string.
708 * @param n The index of the character to access.
709 * @return Read-only (const) reference to the character.
710 * @throw std::out_of_range If @a n is an invalid index.
712 * This function provides for safer data access. The parameter is
713 * first checked that it is in the range of the string. The function
714 * throws out_of_range if the check fails.
717 at(size_type __n) const
719 if (__n >= this->size())
720 __throw_out_of_range(__N("basic_string::at"));
721 return _M_data()[__n];
725 * @brief Provides access to the data contained in the %string.
726 * @param n The index of the character to access.
727 * @return Read/write reference to the character.
728 * @throw std::out_of_range If @a n is an invalid index.
730 * This function provides for safer data access. The parameter is
731 * first checked that it is in the range of the string. The function
732 * throws out_of_range if the check fails. Success results in
733 * unsharing the string.
739 __throw_out_of_range(__N("basic_string::at"));
741 return _M_data()[__n];
746 * @brief Append a string to this string.
747 * @param str The string to append.
748 * @return Reference to this string.
751 operator+=(const basic_string& __str)
752 { return this->append(__str); }
755 * @brief Append a C string.
756 * @param s The C string to append.
757 * @return Reference to this string.
760 operator+=(const _CharT* __s)
761 { return this->append(__s); }
764 * @brief Append a character.
765 * @param s The character to append.
766 * @return Reference to this string.
769 operator+=(_CharT __c)
771 this->push_back(__c);
776 * @brief Append a string to this string.
777 * @param str The string to append.
778 * @return Reference to this string.
781 append(const basic_string& __str);
784 * @brief Append a substring.
785 * @param str The string to append.
786 * @param pos Index of the first character of str to append.
787 * @param n The number of characters to append.
788 * @return Reference to this string.
789 * @throw std::out_of_range if @a pos is not a valid index.
791 * This function appends @a n characters from @a str starting at @a pos
792 * to this string. If @a n is is larger than the number of available
793 * characters in @a str, the remainder of @a str is appended.
796 append(const basic_string& __str, size_type __pos, size_type __n);
799 * @brief Append a C substring.
800 * @param s The C string to append.
801 * @param n The number of characters to append.
802 * @return Reference to this string.
805 append(const _CharT* __s, size_type __n);
808 * @brief Append a C string.
809 * @param s The C string to append.
810 * @return Reference to this string.
813 append(const _CharT* __s)
815 __glibcxx_requires_string(__s);
816 return this->append(__s, traits_type::length(__s));
820 * @brief Append multiple characters.
821 * @param n The number of characters to append.
822 * @param c The character to use.
823 * @return Reference to this string.
825 * Appends n copies of c to this string.
828 append(size_type __n, _CharT __c);
831 * @brief Append a range of characters.
832 * @param first Iterator referencing the first character to append.
833 * @param last Iterator marking the end of the range.
834 * @return Reference to this string.
836 * Appends characters in the range [first,last) to this string.
838 template<class _InputIterator>
840 append(_InputIterator __first, _InputIterator __last)
841 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
844 * @brief Append a single character.
845 * @param c Character to append.
848 push_back(_CharT __c)
850 const size_type __len = 1 + this->size();
851 if (__len > this->capacity() || _M_rep()->_M_is_shared())
852 this->reserve(__len);
853 traits_type::assign(_M_data()[this->size()], __c);
854 _M_rep()->_M_set_length_and_sharable(__len);
858 * @brief Set value to contents of another string.
859 * @param str Source string to use.
860 * @return Reference to this string.
863 assign(const basic_string& __str);
866 * @brief Set value to a substring of a string.
867 * @param str The string to use.
868 * @param pos Index of the first character of str.
869 * @param n Number of characters to use.
870 * @return Reference to this string.
871 * @throw std::out_of_range if @a pos is not a valid index.
873 * This function sets this string to the substring of @a str consisting
874 * of @a n characters at @a pos. If @a n is is larger than the number
875 * of available characters in @a str, the remainder of @a str is used.
878 assign(const basic_string& __str, size_type __pos, size_type __n)
879 { return this->assign(__str._M_data()
880 + __str._M_check(__pos, "basic_string::assign"),
881 __str._M_limit(__pos, __n)); }
884 * @brief Set value to a C substring.
885 * @param s The C string to use.
886 * @param n Number of characters to use.
887 * @return Reference to this string.
889 * This function sets the value of this string to the first @a n
890 * characters of @a s. If @a n is is larger than the number of
891 * available characters in @a s, the remainder of @a s is used.
894 assign(const _CharT* __s, size_type __n);
897 * @brief Set value to contents of a C string.
898 * @param s The C string to use.
899 * @return Reference to this string.
901 * This function sets the value of this string to the value of @a s.
902 * The data is copied, so there is no dependence on @a s once the
906 assign(const _CharT* __s)
908 __glibcxx_requires_string(__s);
909 return this->assign(__s, traits_type::length(__s));
913 * @brief Set value to multiple characters.
914 * @param n Length of the resulting string.
915 * @param c The character to use.
916 * @return Reference to this string.
918 * This function sets the value of this string to @a n copies of
922 assign(size_type __n, _CharT __c)
923 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
926 * @brief Set value to a range of characters.
927 * @param first Iterator referencing the first character to append.
928 * @param last Iterator marking the end of the range.
929 * @return Reference to this string.
931 * Sets value of string to characters in the range [first,last).
933 template<class _InputIterator>
935 assign(_InputIterator __first, _InputIterator __last)
936 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
939 * @brief Insert multiple characters.
940 * @param p Iterator referencing location in string to insert at.
941 * @param n Number of characters to insert
942 * @param c The character to insert.
943 * @throw std::length_error If new length exceeds @c max_size().
945 * Inserts @a n copies of character @a c starting at the position
946 * referenced by iterator @a p. If adding characters causes the length
947 * to exceed max_size(), length_error is thrown. The value of the
948 * string doesn't change if an error is thrown.
951 insert(iterator __p, size_type __n, _CharT __c)
952 { this->replace(__p, __p, __n, __c); }
955 * @brief Insert a range of characters.
956 * @param p Iterator referencing location in string to insert at.
957 * @param beg Start of range.
958 * @param end End of range.
959 * @throw std::length_error If new length exceeds @c max_size().
961 * Inserts characters in range [beg,end). If adding characters causes
962 * the length to exceed max_size(), length_error is thrown. The value
963 * of the string doesn't change if an error is thrown.
965 template<class _InputIterator>
967 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
968 { this->replace(__p, __p, __beg, __end); }
971 * @brief Insert value of a string.
972 * @param pos1 Iterator referencing location in string to insert at.
973 * @param str The string to insert.
974 * @return Reference to this string.
975 * @throw std::length_error If new length exceeds @c max_size().
977 * Inserts value of @a str starting at @a pos1. If adding characters
978 * causes the length to exceed max_size(), length_error is thrown. The
979 * value of the string doesn't change if an error is thrown.
982 insert(size_type __pos1, const basic_string& __str)
983 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
986 * @brief Insert a substring.
987 * @param pos1 Iterator referencing location in string to insert at.
988 * @param str The string to insert.
989 * @param pos2 Start of characters in str to insert.
990 * @param n Number of characters to insert.
991 * @return Reference to this string.
992 * @throw std::length_error If new length exceeds @c max_size().
993 * @throw std::out_of_range If @a pos1 > size() or
994 * @a pos2 > @a str.size().
996 * Starting at @a pos1, insert @a n character of @a str beginning with
997 * @a pos2. If adding characters causes the length to exceed
998 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
999 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1000 * thrown. The value of the string doesn't change if an error is
1004 insert(size_type __pos1, const basic_string& __str,
1005 size_type __pos2, size_type __n)
1006 { return this->insert(__pos1, __str._M_data()
1007 + __str._M_check(__pos2, "basic_string::insert"),
1008 __str._M_limit(__pos2, __n)); }
1011 * @brief Insert a C substring.
1012 * @param pos Iterator referencing location in string to insert at.
1013 * @param s The C string to insert.
1014 * @param n The number of characters to insert.
1015 * @return Reference to this string.
1016 * @throw std::length_error If new length exceeds @c max_size().
1017 * @throw std::out_of_range If @a pos is beyond the end of this
1020 * Inserts the first @a n characters of @a s starting at @a pos. If
1021 * adding characters causes the length to exceed max_size(),
1022 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1023 * thrown. The value of the string doesn't change if an error is
1027 insert(size_type __pos, const _CharT* __s, size_type __n);
1030 * @brief Insert a C string.
1031 * @param pos Iterator referencing location in string to insert at.
1032 * @param s The C string to insert.
1033 * @return Reference to this string.
1034 * @throw std::length_error If new length exceeds @c max_size().
1035 * @throw std::out_of_range If @a pos is beyond the end of this
1038 * Inserts the first @a n characters of @a s starting at @a pos. If
1039 * adding characters causes the length to exceed max_size(),
1040 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1041 * thrown. The value of the string doesn't change if an error is
1045 insert(size_type __pos, const _CharT* __s)
1047 __glibcxx_requires_string(__s);
1048 return this->insert(__pos, __s, traits_type::length(__s));
1052 * @brief Insert multiple characters.
1053 * @param pos Index in string to insert at.
1054 * @param n Number of characters to insert
1055 * @param c The character to insert.
1056 * @return Reference to this string.
1057 * @throw std::length_error If new length exceeds @c max_size().
1058 * @throw std::out_of_range If @a pos is beyond the end of this
1061 * Inserts @a n copies of character @a c starting at index @a pos. If
1062 * adding characters causes the length to exceed max_size(),
1063 * length_error is thrown. If @a pos > length(), out_of_range is
1064 * thrown. The value of the string doesn't change if an error is
1068 insert(size_type __pos, size_type __n, _CharT __c)
1069 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1070 size_type(0), __n, __c); }
1073 * @brief Insert one character.
1074 * @param p Iterator referencing position in string to insert at.
1075 * @param c The character to insert.
1076 * @return Iterator referencing newly inserted char.
1077 * @throw std::length_error If new length exceeds @c max_size().
1079 * Inserts character @a c at position referenced by @a p. If adding
1080 * character causes the length to exceed max_size(), length_error is
1081 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1082 * The value of the string doesn't change if an error is thrown.
1085 insert(iterator __p, _CharT __c)
1087 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1088 const size_type __pos = __p - _M_ibegin();
1089 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1090 _M_rep()->_M_set_leaked();
1091 return this->_M_ibegin() + __pos;
1095 * @brief Remove characters.
1096 * @param pos Index of first character to remove (default 0).
1097 * @param n Number of characters to remove (default remainder).
1098 * @return Reference to this string.
1099 * @throw std::out_of_range If @a pos is beyond the end of this
1102 * Removes @a n characters from this string starting at @a pos. The
1103 * length of the string is reduced by @a n. If there are < @a n
1104 * characters to remove, the remainder of the string is truncated. If
1105 * @a p is beyond end of string, out_of_range is thrown. The value of
1106 * the string doesn't change if an error is thrown.
1109 erase(size_type __pos = 0, size_type __n = npos)
1111 _M_mutate(_M_check(__pos, "basic_string::erase"),
1112 _M_limit(__pos, __n), size_type(0));
1117 * @brief Remove one character.
1118 * @param position Iterator referencing the character to remove.
1119 * @return iterator referencing same location after removal.
1121 * Removes the character at @a position from this string. The value
1122 * of the string doesn't change if an error is thrown.
1125 erase(iterator __position)
1127 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1128 && __position < _M_iend());
1129 const size_type __pos = __position - _M_ibegin();
1130 _M_mutate(__pos, size_type(1), size_type(0));
1131 _M_rep()->_M_set_leaked();
1132 return _M_ibegin() + __pos;
1136 * @brief Remove a range of characters.
1137 * @param first Iterator referencing the first character to remove.
1138 * @param last Iterator referencing the end of the range.
1139 * @return Iterator referencing location of first after removal.
1141 * Removes the characters in the range [first,last) from this string.
1142 * The value of the string doesn't change if an error is thrown.
1145 erase(iterator __first, iterator __last)
1147 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1148 && __last <= _M_iend());
1149 const size_type __pos = __first - _M_ibegin();
1150 _M_mutate(__pos, __last - __first, size_type(0));
1151 _M_rep()->_M_set_leaked();
1152 return _M_ibegin() + __pos;
1156 * @brief Replace characters with value from another string.
1157 * @param pos Index of first character to replace.
1158 * @param n Number of characters to be replaced.
1159 * @param str String to insert.
1160 * @return Reference to this string.
1161 * @throw std::out_of_range If @a pos is beyond the end of this
1163 * @throw std::length_error If new length exceeds @c max_size().
1165 * Removes the characters in the range [pos,pos+n) from this string.
1166 * In place, the value of @a str is inserted. If @a pos is beyond end
1167 * of string, out_of_range is thrown. If the length of the result
1168 * exceeds max_size(), length_error is thrown. The value of the string
1169 * doesn't change if an error is thrown.
1172 replace(size_type __pos, size_type __n, const basic_string& __str)
1173 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1176 * @brief Replace characters with value from another string.
1177 * @param pos1 Index of first character to replace.
1178 * @param n1 Number of characters to be replaced.
1179 * @param str String to insert.
1180 * @param pos2 Index of first character of str to use.
1181 * @param n2 Number of characters from str to use.
1182 * @return Reference to this string.
1183 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1185 * @throw std::length_error If new length exceeds @c max_size().
1187 * Removes the characters in the range [pos1,pos1 + n) from this
1188 * string. In place, the value of @a str is inserted. If @a pos is
1189 * beyond end of string, out_of_range is thrown. If the length of the
1190 * result exceeds max_size(), length_error is thrown. The value of the
1191 * string doesn't change if an error is thrown.
1194 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1195 size_type __pos2, size_type __n2)
1196 { return this->replace(__pos1, __n1, __str._M_data()
1197 + __str._M_check(__pos2, "basic_string::replace"),
1198 __str._M_limit(__pos2, __n2)); }
1201 * @brief Replace characters with value of a C substring.
1202 * @param pos Index of first character to replace.
1203 * @param n1 Number of characters to be replaced.
1204 * @param str C string to insert.
1205 * @param n2 Number of characters from str to use.
1206 * @return Reference to this string.
1207 * @throw std::out_of_range If @a pos1 > size().
1208 * @throw std::length_error If new length exceeds @c max_size().
1210 * Removes the characters in the range [pos,pos + n1) from this string.
1211 * In place, the first @a n2 characters of @a str are inserted, or all
1212 * of @a str if @a n2 is too large. If @a pos is beyond end of string,
1213 * out_of_range is thrown. If the length of result exceeds max_size(),
1214 * length_error is thrown. The value of the string doesn't change if
1215 * an error is thrown.
1218 replace(size_type __pos, size_type __n1, const _CharT* __s,
1222 * @brief Replace characters with value of a C string.
1223 * @param pos Index of first character to replace.
1224 * @param n1 Number of characters to be replaced.
1225 * @param str C string to insert.
1226 * @return Reference to this string.
1227 * @throw std::out_of_range If @a pos > size().
1228 * @throw std::length_error If new length exceeds @c max_size().
1230 * Removes the characters in the range [pos,pos + n1) from this string.
1231 * In place, the first @a n characters of @a str are inserted. If @a
1232 * pos is beyond end of string, out_of_range is thrown. If the length
1233 * of result exceeds max_size(), length_error is thrown. The value of
1234 * the string doesn't change if an error is thrown.
1237 replace(size_type __pos, size_type __n1, const _CharT* __s)
1239 __glibcxx_requires_string(__s);
1240 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1244 * @brief Replace characters with multiple characters.
1245 * @param pos Index of first character to replace.
1246 * @param n1 Number of characters to be replaced.
1247 * @param n2 Number of characters to insert.
1248 * @param c Character to insert.
1249 * @return Reference to this string.
1250 * @throw std::out_of_range If @a pos > size().
1251 * @throw std::length_error If new length exceeds @c max_size().
1253 * Removes the characters in the range [pos,pos + n1) from this string.
1254 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1255 * end of string, out_of_range is thrown. If the length of result
1256 * exceeds max_size(), length_error is thrown. The value of the string
1257 * doesn't change if an error is thrown.
1260 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1261 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1262 _M_limit(__pos, __n1), __n2, __c); }
1265 * @brief Replace range of characters with string.
1266 * @param i1 Iterator referencing start of range to replace.
1267 * @param i2 Iterator referencing end of range to replace.
1268 * @param str String value to insert.
1269 * @return Reference to this string.
1270 * @throw std::length_error If new length exceeds @c max_size().
1272 * Removes the characters in the range [i1,i2). In place, the value of
1273 * @a str is inserted. If the length of result exceeds max_size(),
1274 * length_error is thrown. The value of the string doesn't change if
1275 * an error is thrown.
1278 replace(iterator __i1, iterator __i2, const basic_string& __str)
1279 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1282 * @brief Replace range of characters with C substring.
1283 * @param i1 Iterator referencing start of range to replace.
1284 * @param i2 Iterator referencing end of range to replace.
1285 * @param s C string value to insert.
1286 * @param n Number of characters from s to insert.
1287 * @return Reference to this string.
1288 * @throw std::length_error If new length exceeds @c max_size().
1290 * Removes the characters in the range [i1,i2). In place, the first @a
1291 * n characters of @a s are inserted. If the length of result exceeds
1292 * max_size(), length_error is thrown. The value of the string doesn't
1293 * change if an error is thrown.
1296 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1298 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1299 && __i2 <= _M_iend());
1300 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1304 * @brief Replace range of characters with C string.
1305 * @param i1 Iterator referencing start of range to replace.
1306 * @param i2 Iterator referencing end of range to replace.
1307 * @param s C string value to insert.
1308 * @return Reference to this string.
1309 * @throw std::length_error If new length exceeds @c max_size().
1311 * Removes the characters in the range [i1,i2). In place, the
1312 * characters of @a s are inserted. If the length of result exceeds
1313 * max_size(), length_error is thrown. The value of the string doesn't
1314 * change if an error is thrown.
1317 replace(iterator __i1, iterator __i2, const _CharT* __s)
1319 __glibcxx_requires_string(__s);
1320 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1324 * @brief Replace range of characters with multiple characters
1325 * @param i1 Iterator referencing start of range to replace.
1326 * @param i2 Iterator referencing end of range to replace.
1327 * @param n Number of characters to insert.
1328 * @param c Character to insert.
1329 * @return Reference to this string.
1330 * @throw std::length_error If new length exceeds @c max_size().
1332 * Removes the characters in the range [i1,i2). In place, @a n copies
1333 * of @a c are inserted. If the length of result exceeds max_size(),
1334 * length_error is thrown. The value of the string doesn't change if
1335 * an error is thrown.
1338 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1340 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1341 && __i2 <= _M_iend());
1342 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1346 * @brief Replace range of characters with range.
1347 * @param i1 Iterator referencing start of range to replace.
1348 * @param i2 Iterator referencing end of range to replace.
1349 * @param k1 Iterator referencing start of range to insert.
1350 * @param k2 Iterator referencing end of range to insert.
1351 * @return Reference to this string.
1352 * @throw std::length_error If new length exceeds @c max_size().
1354 * Removes the characters in the range [i1,i2). In place, characters
1355 * in the range [k1,k2) are inserted. If the length of result exceeds
1356 * max_size(), length_error is thrown. The value of the string doesn't
1357 * change if an error is thrown.
1359 template<class _InputIterator>
1361 replace(iterator __i1, iterator __i2,
1362 _InputIterator __k1, _InputIterator __k2)
1364 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1365 && __i2 <= _M_iend());
1366 __glibcxx_requires_valid_range(__k1, __k2);
1367 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1368 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1371 // Specializations for the common case of pointer and iterator:
1372 // useful to avoid the overhead of temporary buffering in _M_replace.
1374 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1376 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1377 && __i2 <= _M_iend());
1378 __glibcxx_requires_valid_range(__k1, __k2);
1379 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1384 replace(iterator __i1, iterator __i2,
1385 const _CharT* __k1, const _CharT* __k2)
1387 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1388 && __i2 <= _M_iend());
1389 __glibcxx_requires_valid_range(__k1, __k2);
1390 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1395 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1397 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1398 && __i2 <= _M_iend());
1399 __glibcxx_requires_valid_range(__k1, __k2);
1400 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1401 __k1.base(), __k2 - __k1);
1405 replace(iterator __i1, iterator __i2,
1406 const_iterator __k1, const_iterator __k2)
1408 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1409 && __i2 <= _M_iend());
1410 __glibcxx_requires_valid_range(__k1, __k2);
1411 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1412 __k1.base(), __k2 - __k1);
1416 template<class _Integer>
1418 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1419 _Integer __val, __true_type)
1420 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1422 template<class _InputIterator>
1424 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1425 _InputIterator __k2, __false_type);
1428 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1432 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1435 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1436 // requires special behaviour if _InIter is an integral type
1437 template<class _InIterator>
1439 _S_construct_aux(_InIterator __beg, _InIterator __end,
1440 const _Alloc& __a, __false_type)
1442 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1443 return _S_construct(__beg, __end, __a, _Tag());
1446 template<class _InIterator>
1448 _S_construct_aux(_InIterator __beg, _InIterator __end,
1449 const _Alloc& __a, __true_type)
1450 { return _S_construct(static_cast<size_type>(__beg),
1451 static_cast<value_type>(__end), __a); }
1453 template<class _InIterator>
1455 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1457 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1458 return _S_construct_aux(__beg, __end, __a, _Integral());
1461 // For Input Iterators, used in istreambuf_iterators, etc.
1462 template<class _InIterator>
1464 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1465 input_iterator_tag);
1467 // For forward_iterators up to random_access_iterators, used for
1468 // string::iterator, _CharT*, etc.
1469 template<class _FwdIterator>
1471 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1472 forward_iterator_tag);
1475 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1480 * @brief Copy substring into C string.
1481 * @param s C string to copy value into.
1482 * @param n Number of characters to copy.
1483 * @param pos Index of first character to copy.
1484 * @return Number of characters actually copied
1485 * @throw std::out_of_range If pos > size().
1487 * Copies up to @a n characters starting at @a pos into the C string @a
1488 * s. If @a pos is greater than size(), out_of_range is thrown.
1491 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1494 * @brief Swap contents with another string.
1495 * @param s String to swap with.
1497 * Exchanges the contents of this string with that of @a s in constant
1501 swap(basic_string& __s);
1503 // String operations:
1505 * @brief Return const pointer to null-terminated contents.
1507 * This is a handle to internal data. Do not modify or dire things may
1512 { return _M_data(); }
1515 * @brief Return const pointer to contents.
1517 * This is a handle to internal data. Do not modify or dire things may
1522 { return _M_data(); }
1525 * @brief Return copy of allocator used to construct this string.
1528 get_allocator() const
1529 { return _M_dataplus; }
1532 * @brief Find position of a C substring.
1533 * @param s C string to locate.
1534 * @param pos Index of character to search from.
1535 * @param n Number of characters from @a s to search for.
1536 * @return Index of start of first occurrence.
1538 * Starting from @a pos, searches forward for the first @a n characters
1539 * in @a s within this string. If found, returns the index where it
1540 * begins. If not found, returns npos.
1543 find(const _CharT* __s, size_type __pos, size_type __n) const;
1546 * @brief Find position of a string.
1547 * @param str String to locate.
1548 * @param pos Index of character to search from (default 0).
1549 * @return Index of start of first occurrence.
1551 * Starting from @a pos, searches forward for value of @a str within
1552 * this string. If found, returns the index where it begins. If not
1553 * found, returns npos.
1556 find(const basic_string& __str, size_type __pos = 0) const
1557 { return this->find(__str.data(), __pos, __str.size()); }
1560 * @brief Find position of a C string.
1561 * @param s C string to locate.
1562 * @param pos Index of character to search from (default 0).
1563 * @return Index of start of first occurrence.
1565 * Starting from @a pos, searches forward for the value of @a s within
1566 * this string. If found, returns the index where it begins. If not
1567 * found, returns npos.
1570 find(const _CharT* __s, size_type __pos = 0) const
1572 __glibcxx_requires_string(__s);
1573 return this->find(__s, __pos, traits_type::length(__s));
1577 * @brief Find position of a character.
1578 * @param c Character to locate.
1579 * @param pos Index of character to search from (default 0).
1580 * @return Index of first occurrence.
1582 * Starting from @a pos, searches forward for @a c within this string.
1583 * If found, returns the index where it was found. If not found,
1587 find(_CharT __c, size_type __pos = 0) const;
1590 * @brief Find last position of a string.
1591 * @param str String to locate.
1592 * @param pos Index of character to search back from (default end).
1593 * @return Index of start of last occurrence.
1595 * Starting from @a pos, searches backward for value of @a str within
1596 * this string. If found, returns the index where it begins. If not
1597 * found, returns npos.
1600 rfind(const basic_string& __str, size_type __pos = npos) const
1601 { return this->rfind(__str.data(), __pos, __str.size()); }
1604 * @brief Find last position of a C substring.
1605 * @param s C string to locate.
1606 * @param pos Index of character to search back from.
1607 * @param n Number of characters from s to search for.
1608 * @return Index of start of last occurrence.
1610 * Starting from @a pos, searches backward for the first @a n
1611 * characters in @a s within this string. If found, returns the index
1612 * where it begins. If not found, returns npos.
1615 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1618 * @brief Find last position of a C string.
1619 * @param s C string to locate.
1620 * @param pos Index of character to start search at (default 0).
1621 * @return Index of start of last occurrence.
1623 * Starting from @a pos, searches backward for the value of @a s within
1624 * this string. If found, returns the index where it begins. If not
1625 * found, returns npos.
1628 rfind(const _CharT* __s, size_type __pos = npos) const
1630 __glibcxx_requires_string(__s);
1631 return this->rfind(__s, __pos, traits_type::length(__s));
1635 * @brief Find last position of a character.
1636 * @param c Character to locate.
1637 * @param pos Index of character to search back from (default 0).
1638 * @return Index of last occurrence.
1640 * Starting from @a pos, searches backward for @a c within this string.
1641 * If found, returns the index where it was found. If not found,
1645 rfind(_CharT __c, size_type __pos = npos) const;
1648 * @brief Find position of a character of string.
1649 * @param str String containing characters to locate.
1650 * @param pos Index of character to search from (default 0).
1651 * @return Index of first occurrence.
1653 * Starting from @a pos, searches forward for one of the characters of
1654 * @a str within this string. If found, returns the index where it was
1655 * found. If not found, returns npos.
1658 find_first_of(const basic_string& __str, size_type __pos = 0) const
1659 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1662 * @brief Find position of a character of C substring.
1663 * @param s String containing characters to locate.
1664 * @param pos Index of character to search from (default 0).
1665 * @param n Number of characters from s to search for.
1666 * @return Index of first occurrence.
1668 * Starting from @a pos, searches forward for one of the first @a n
1669 * characters of @a s within this string. If found, returns the index
1670 * where it was found. If not found, returns npos.
1673 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1676 * @brief Find position of a character of C string.
1677 * @param s String containing characters to locate.
1678 * @param pos Index of character to search from (default 0).
1679 * @return Index of first occurrence.
1681 * Starting from @a pos, searches forward for one of the characters of
1682 * @a s within this string. If found, returns the index where it was
1683 * found. If not found, returns npos.
1686 find_first_of(const _CharT* __s, size_type __pos = 0) const
1688 __glibcxx_requires_string(__s);
1689 return this->find_first_of(__s, __pos, traits_type::length(__s));
1693 * @brief Find position of a character.
1694 * @param c Character to locate.
1695 * @param pos Index of character to search from (default 0).
1696 * @return Index of first occurrence.
1698 * Starting from @a pos, searches forward for the character @a c within
1699 * this string. If found, returns the index where it was found. If
1700 * not found, returns npos.
1702 * Note: equivalent to find(c, pos).
1705 find_first_of(_CharT __c, size_type __pos = 0) const
1706 { return this->find(__c, __pos); }
1709 * @brief Find last position of a character of string.
1710 * @param str String containing characters to locate.
1711 * @param pos Index of character to search back from (default end).
1712 * @return Index of last occurrence.
1714 * Starting from @a pos, searches backward for one of the characters of
1715 * @a str within this string. If found, returns the index where it was
1716 * found. If not found, returns npos.
1719 find_last_of(const basic_string& __str, size_type __pos = npos) const
1720 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1723 * @brief Find last position of a character of C substring.
1724 * @param s C string containing characters to locate.
1725 * @param pos Index of character to search back from (default end).
1726 * @param n Number of characters from s to search for.
1727 * @return Index of last occurrence.
1729 * Starting from @a pos, searches backward for one of the first @a n
1730 * characters of @a s within this string. If found, returns the index
1731 * where it was found. If not found, returns npos.
1734 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1737 * @brief Find last position of a character of C string.
1738 * @param s C string containing characters to locate.
1739 * @param pos Index of character to search back from (default end).
1740 * @return Index of last occurrence.
1742 * Starting from @a pos, searches backward for one of the characters of
1743 * @a s within this string. If found, returns the index where it was
1744 * found. If not found, returns npos.
1747 find_last_of(const _CharT* __s, size_type __pos = npos) const
1749 __glibcxx_requires_string(__s);
1750 return this->find_last_of(__s, __pos, traits_type::length(__s));
1754 * @brief Find last position of a character.
1755 * @param c Character to locate.
1756 * @param pos Index of character to search back from (default 0).
1757 * @return Index of last occurrence.
1759 * Starting from @a pos, searches backward for @a c within this string.
1760 * If found, returns the index where it was found. If not found,
1763 * Note: equivalent to rfind(c, pos).
1766 find_last_of(_CharT __c, size_type __pos = npos) const
1767 { return this->rfind(__c, __pos); }
1770 * @brief Find position of a character not in string.
1771 * @param str String containing characters to avoid.
1772 * @param pos Index of character to search from (default 0).
1773 * @return Index of first occurrence.
1775 * Starting from @a pos, searches forward for a character not contained
1776 * in @a str within this string. If found, returns the index where it
1777 * was found. If not found, returns npos.
1780 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1781 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1784 * @brief Find position of a character not in C substring.
1785 * @param s C string containing characters to avoid.
1786 * @param pos Index of character to search from (default 0).
1787 * @param n Number of characters from s to consider.
1788 * @return Index of first occurrence.
1790 * Starting from @a pos, searches forward for a character not contained
1791 * in the first @a n characters of @a s within this string. If found,
1792 * returns the index where it was found. If not found, returns npos.
1795 find_first_not_of(const _CharT* __s, size_type __pos,
1796 size_type __n) const;
1799 * @brief Find position of a character not in C string.
1800 * @param s C string containing characters to avoid.
1801 * @param pos Index of character to search from (default 0).
1802 * @return Index of first occurrence.
1804 * Starting from @a pos, searches forward for a character not contained
1805 * in @a s within this string. If found, returns the index where it
1806 * was found. If not found, returns npos.
1809 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1811 __glibcxx_requires_string(__s);
1812 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1816 * @brief Find position of a different character.
1817 * @param c Character to avoid.
1818 * @param pos Index of character to search from (default 0).
1819 * @return Index of first occurrence.
1821 * Starting from @a pos, searches forward for a character other than @a c
1822 * within this string. If found, returns the index where it was found.
1823 * If not found, returns npos.
1826 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1829 * @brief Find last position of a character not in string.
1830 * @param str String containing characters to avoid.
1831 * @param pos Index of character to search from (default 0).
1832 * @return Index of first occurrence.
1834 * Starting from @a pos, searches backward for a character not
1835 * contained in @a str within this string. If found, returns the index
1836 * where it was found. If not found, returns npos.
1839 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1840 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1843 * @brief Find last position of a character not in C substring.
1844 * @param s C string containing characters to avoid.
1845 * @param pos Index of character to search from (default 0).
1846 * @param n Number of characters from s to consider.
1847 * @return Index of first occurrence.
1849 * Starting from @a pos, searches backward for a character not
1850 * contained in the first @a n characters of @a s within this string.
1851 * If found, returns the index where it was found. If not found,
1855 find_last_not_of(const _CharT* __s, size_type __pos,
1856 size_type __n) const;
1858 * @brief Find position of a character not in C string.
1859 * @param s C string containing characters to avoid.
1860 * @param pos Index of character to search from (default 0).
1861 * @return Index of first occurrence.
1863 * Starting from @a pos, searches backward for a character not
1864 * contained in @a s within this string. If found, returns the index
1865 * where it was found. If not found, returns npos.
1868 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1870 __glibcxx_requires_string(__s);
1871 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1875 * @brief Find last position of a different character.
1876 * @param c Character to avoid.
1877 * @param pos Index of character to search from (default 0).
1878 * @return Index of first occurrence.
1880 * Starting from @a pos, searches backward for a character other than
1881 * @a c within this string. If found, returns the index where it was
1882 * found. If not found, returns npos.
1885 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1888 * @brief Get a substring.
1889 * @param pos Index of first character (default 0).
1890 * @param n Number of characters in substring (default remainder).
1891 * @return The new string.
1892 * @throw std::out_of_range If pos > size().
1894 * Construct and return a new string using the @a n characters starting
1895 * at @a pos. If the string is too short, use the remainder of the
1896 * characters. If @a pos is beyond the end of the string, out_of_range
1900 substr(size_type __pos = 0, size_type __n = npos) const
1901 { return basic_string(*this,
1902 _M_check(__pos, "basic_string::substr"), __n); }
1905 * @brief Compare to a string.
1906 * @param str String to compare against.
1907 * @return Integer < 0, 0, or > 0.
1909 * Returns an integer < 0 if this string is ordered before @a str, 0 if
1910 * their values are equivalent, or > 0 if this string is ordered after
1911 * @a str. Determines the effective length rlen of the strings to
1912 * compare as the smallest of size() and str.size(). The function
1913 * then compares the two strings by calling traits::compare(data(),
1914 * str.data(),rlen). If the result of the comparison is nonzero returns
1915 * it, otherwise the shorter one is ordered first.
1918 compare(const basic_string& __str) const
1920 const size_type __size = this->size();
1921 const size_type __osize = __str.size();
1922 const size_type __len = std::min(__size, __osize);
1924 int __r = traits_type::compare(_M_data(), __str.data(), __len);
1926 __r = __size - __osize;
1931 * @brief Compare substring to a string.
1932 * @param pos Index of first character of substring.
1933 * @param n Number of characters in substring.
1934 * @param str String to compare against.
1935 * @return Integer < 0, 0, or > 0.
1937 * Form the substring of this string from the @a n characters starting
1938 * at @a pos. Returns an integer < 0 if the substring is ordered
1939 * before @a str, 0 if their values are equivalent, or > 0 if the
1940 * substring is ordered after @a str. Determines the effective length
1941 * rlen of the strings to compare as the smallest of the length of the
1942 * substring and @a str.size(). The function then compares the two
1943 * strings by calling traits::compare(substring.data(),str.data(),rlen).
1944 * If the result of the comparison is nonzero returns it, otherwise the
1945 * shorter one is ordered first.
1948 compare(size_type __pos, size_type __n, const basic_string& __str) const;
1951 * @brief Compare substring to a substring.
1952 * @param pos1 Index of first character of substring.
1953 * @param n1 Number of characters in substring.
1954 * @param str String to compare against.
1955 * @param pos2 Index of first character of substring of str.
1956 * @param n2 Number of characters in substring of str.
1957 * @return Integer < 0, 0, or > 0.
1959 * Form the substring of this string from the @a n1 characters starting
1960 * at @a pos1. Form the substring of @a str from the @a n2 characters
1961 * starting at @a pos2. Returns an integer < 0 if this substring is
1962 * ordered before the substring of @a str, 0 if their values are
1963 * equivalent, or > 0 if this substring is ordered after the substring
1964 * of @a str. Determines the effective length rlen of the strings
1965 * to compare as the smallest of the lengths of the substrings. The
1966 * function then compares the two strings by calling
1967 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
1968 * If the result of the comparison is nonzero returns it, otherwise the
1969 * shorter one is ordered first.
1972 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1973 size_type __pos2, size_type __n2) const;
1976 * @brief Compare to a C string.
1977 * @param s C string to compare against.
1978 * @return Integer < 0, 0, or > 0.
1980 * Returns an integer < 0 if this string is ordered before @a s, 0 if
1981 * their values are equivalent, or > 0 if this string is ordered after
1982 * @a s. Determines the effective length rlen of the strings to
1983 * compare as the smallest of size() and the length of a string
1984 * constructed from @a s. The function then compares the two strings
1985 * by calling traits::compare(data(),s,rlen). If the result of the
1986 * comparison is nonzero returns it, otherwise the shorter one is
1990 compare(const _CharT* __s) const;
1992 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1993 // 5 String::compare specification questionable
1995 * @brief Compare substring to a C string.
1996 * @param pos Index of first character of substring.
1997 * @param n1 Number of characters in substring.
1998 * @param s C string to compare against.
1999 * @return Integer < 0, 0, or > 0.
2001 * Form the substring of this string from the @a n1 characters starting
2002 * at @a pos. Returns an integer < 0 if the substring is ordered
2003 * before @a s, 0 if their values are equivalent, or > 0 if the
2004 * substring is ordered after @a s. Determines the effective length
2005 * rlen of the strings to compare as the smallest of the length of the
2006 * substring and the length of a string constructed from @a s. The
2007 * function then compares the two string by calling
2008 * traits::compare(substring.data(),s,rlen). If the result of the
2009 * comparison is nonzero returns it, otherwise the shorter one is
2013 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2016 * @brief Compare substring against a character array.
2017 * @param pos1 Index of first character of substring.
2018 * @param n1 Number of characters in substring.
2019 * @param s character array to compare against.
2020 * @param n2 Number of characters of s.
2021 * @return Integer < 0, 0, or > 0.
2023 * Form the substring of this string from the @a n1 characters starting
2024 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2025 * Returns an integer < 0 if this substring is ordered before the string
2026 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2027 * is ordered after the string from @a s. Determines the effective
2028 * length rlen of the strings to compare as the smallest of the length
2029 * of the substring and @a n2. The function then compares the two
2030 * strings by calling traits::compare(substring.data(),s,rlen). If the
2031 * result of the comparison is nonzero returns it, otherwise the shorter
2032 * one is ordered first.
2034 * NB: s must have at least n2 characters, '\0' has no special
2038 compare(size_type __pos, size_type __n1, const _CharT* __s,
2039 size_type __n2) const;
2042 template<typename _CharT, typename _Traits, typename _Alloc>
2043 inline basic_string<_CharT, _Traits, _Alloc>::
2045 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2046 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2048 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2053 * @brief Concatenate two strings.
2054 * @param lhs First string.
2055 * @param rhs Last string.
2056 * @return New string with value of @a lhs followed by @a rhs.
2058 template<typename _CharT, typename _Traits, typename _Alloc>
2059 basic_string<_CharT, _Traits, _Alloc>
2060 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2061 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2063 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2064 __str.append(__rhs);
2069 * @brief Concatenate C string and string.
2070 * @param lhs First string.
2071 * @param rhs Last string.
2072 * @return New string with value of @a lhs followed by @a rhs.
2074 template<typename _CharT, typename _Traits, typename _Alloc>
2075 basic_string<_CharT,_Traits,_Alloc>
2076 operator+(const _CharT* __lhs,
2077 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2080 * @brief Concatenate character and string.
2081 * @param lhs First string.
2082 * @param rhs Last string.
2083 * @return New string with @a lhs followed by @a rhs.
2085 template<typename _CharT, typename _Traits, typename _Alloc>
2086 basic_string<_CharT,_Traits,_Alloc>
2087 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2090 * @brief Concatenate string and C string.
2091 * @param lhs First string.
2092 * @param rhs Last string.
2093 * @return New string with @a lhs followed by @a rhs.
2095 template<typename _CharT, typename _Traits, typename _Alloc>
2096 inline basic_string<_CharT, _Traits, _Alloc>
2097 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2098 const _CharT* __rhs)
2100 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2101 __str.append(__rhs);
2106 * @brief Concatenate string and character.
2107 * @param lhs First string.
2108 * @param rhs Last string.
2109 * @return New string with @a lhs followed by @a rhs.
2111 template<typename _CharT, typename _Traits, typename _Alloc>
2112 inline basic_string<_CharT, _Traits, _Alloc>
2113 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2115 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2116 typedef typename __string_type::size_type __size_type;
2117 __string_type __str(__lhs);
2118 __str.append(__size_type(1), __rhs);
2124 * @brief Test equivalence of two strings.
2125 * @param lhs First string.
2126 * @param rhs Second string.
2127 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2129 template<typename _CharT, typename _Traits, typename _Alloc>
2131 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2132 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2133 { return __lhs.compare(__rhs) == 0; }
2136 * @brief Test equivalence of C string and string.
2137 * @param lhs C string.
2138 * @param rhs String.
2139 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2141 template<typename _CharT, typename _Traits, typename _Alloc>
2143 operator==(const _CharT* __lhs,
2144 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2145 { return __rhs.compare(__lhs) == 0; }
2148 * @brief Test equivalence of string and C string.
2149 * @param lhs String.
2150 * @param rhs C string.
2151 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2153 template<typename _CharT, typename _Traits, typename _Alloc>
2155 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2156 const _CharT* __rhs)
2157 { return __lhs.compare(__rhs) == 0; }
2161 * @brief Test difference of two strings.
2162 * @param lhs First string.
2163 * @param rhs Second string.
2164 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2166 template<typename _CharT, typename _Traits, typename _Alloc>
2168 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2169 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2170 { return __rhs.compare(__lhs) != 0; }
2173 * @brief Test difference of C string and string.
2174 * @param lhs C string.
2175 * @param rhs String.
2176 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2178 template<typename _CharT, typename _Traits, typename _Alloc>
2180 operator!=(const _CharT* __lhs,
2181 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2182 { return __rhs.compare(__lhs) != 0; }
2185 * @brief Test difference of string and C string.
2186 * @param lhs String.
2187 * @param rhs C string.
2188 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2190 template<typename _CharT, typename _Traits, typename _Alloc>
2192 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2193 const _CharT* __rhs)
2194 { return __lhs.compare(__rhs) != 0; }
2198 * @brief Test if string precedes string.
2199 * @param lhs First string.
2200 * @param rhs Second string.
2201 * @return True if @a lhs precedes @a rhs. False otherwise.
2203 template<typename _CharT, typename _Traits, typename _Alloc>
2205 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2206 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2207 { return __lhs.compare(__rhs) < 0; }
2210 * @brief Test if string precedes C string.
2211 * @param lhs String.
2212 * @param rhs C string.
2213 * @return True if @a lhs precedes @a rhs. False otherwise.
2215 template<typename _CharT, typename _Traits, typename _Alloc>
2217 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2218 const _CharT* __rhs)
2219 { return __lhs.compare(__rhs) < 0; }
2222 * @brief Test if C string precedes string.
2223 * @param lhs C string.
2224 * @param rhs String.
2225 * @return True if @a lhs precedes @a rhs. False otherwise.
2227 template<typename _CharT, typename _Traits, typename _Alloc>
2229 operator<(const _CharT* __lhs,
2230 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2231 { return __rhs.compare(__lhs) > 0; }
2235 * @brief Test if string follows string.
2236 * @param lhs First string.
2237 * @param rhs Second string.
2238 * @return True if @a lhs follows @a rhs. False otherwise.
2240 template<typename _CharT, typename _Traits, typename _Alloc>
2242 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2243 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2244 { return __lhs.compare(__rhs) > 0; }
2247 * @brief Test if string follows C string.
2248 * @param lhs String.
2249 * @param rhs C string.
2250 * @return True if @a lhs follows @a rhs. False otherwise.
2252 template<typename _CharT, typename _Traits, typename _Alloc>
2254 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2255 const _CharT* __rhs)
2256 { return __lhs.compare(__rhs) > 0; }
2259 * @brief Test if C string follows string.
2260 * @param lhs C string.
2261 * @param rhs String.
2262 * @return True if @a lhs follows @a rhs. False otherwise.
2264 template<typename _CharT, typename _Traits, typename _Alloc>
2266 operator>(const _CharT* __lhs,
2267 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2268 { return __rhs.compare(__lhs) < 0; }
2272 * @brief Test if string doesn't follow string.
2273 * @param lhs First string.
2274 * @param rhs Second string.
2275 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2277 template<typename _CharT, typename _Traits, typename _Alloc>
2279 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2280 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2281 { return __lhs.compare(__rhs) <= 0; }
2284 * @brief Test if string doesn't follow C string.
2285 * @param lhs String.
2286 * @param rhs C string.
2287 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2289 template<typename _CharT, typename _Traits, typename _Alloc>
2291 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2292 const _CharT* __rhs)
2293 { return __lhs.compare(__rhs) <= 0; }
2296 * @brief Test if C string doesn't follow string.
2297 * @param lhs C string.
2298 * @param rhs String.
2299 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2301 template<typename _CharT, typename _Traits, typename _Alloc>
2303 operator<=(const _CharT* __lhs,
2304 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2305 { return __rhs.compare(__lhs) >= 0; }
2309 * @brief Test if string doesn't precede string.
2310 * @param lhs First string.
2311 * @param rhs Second string.
2312 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2314 template<typename _CharT, typename _Traits, typename _Alloc>
2316 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2317 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2318 { return __lhs.compare(__rhs) >= 0; }
2321 * @brief Test if string doesn't precede C string.
2322 * @param lhs String.
2323 * @param rhs C string.
2324 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2326 template<typename _CharT, typename _Traits, typename _Alloc>
2328 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2329 const _CharT* __rhs)
2330 { return __lhs.compare(__rhs) >= 0; }
2333 * @brief Test if C string doesn't precede string.
2334 * @param lhs C string.
2335 * @param rhs String.
2336 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2338 template<typename _CharT, typename _Traits, typename _Alloc>
2340 operator>=(const _CharT* __lhs,
2341 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2342 { return __rhs.compare(__lhs) <= 0; }
2345 * @brief Swap contents of two strings.
2346 * @param lhs First string.
2347 * @param rhs Second string.
2349 * Exchanges the contents of @a lhs and @a rhs in constant time.
2351 template<typename _CharT, typename _Traits, typename _Alloc>
2353 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2354 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2355 { __lhs.swap(__rhs); }
2358 * @brief Read stream into a string.
2359 * @param is Input stream.
2360 * @param str Buffer to store into.
2361 * @return Reference to the input stream.
2363 * Stores characters from @a is into @a str until whitespace is found, the
2364 * end of the stream is encountered, or str.max_size() is reached. If
2365 * is.width() is non-zero, that is the limit on the number of characters
2366 * stored into @a str. Any previous contents of @a str are erased.
2368 template<typename _CharT, typename _Traits, typename _Alloc>
2369 basic_istream<_CharT, _Traits>&
2370 operator>>(basic_istream<_CharT, _Traits>& __is,
2371 basic_string<_CharT, _Traits, _Alloc>& __str);
2374 * @brief Write string to a stream.
2375 * @param os Output stream.
2376 * @param str String to write out.
2377 * @return Reference to the output stream.
2379 * Output characters of @a str into os following the same rules as for
2380 * writing a C string.
2382 template<typename _CharT, typename _Traits, typename _Alloc>
2383 basic_ostream<_CharT, _Traits>&
2384 operator<<(basic_ostream<_CharT, _Traits>& __os,
2385 const basic_string<_CharT, _Traits, _Alloc>& __str);
2388 * @brief Read a line from stream into a string.
2389 * @param is Input stream.
2390 * @param str Buffer to store into.
2391 * @param delim Character marking end of line.
2392 * @return Reference to the input stream.
2394 * Stores characters from @a is into @a str until @a delim is found, the
2395 * end of the stream is encountered, or str.max_size() is reached. If
2396 * is.width() is non-zero, that is the limit on the number of characters
2397 * stored into @a str. Any previous contents of @a str are erased. If @a
2398 * delim was encountered, it is extracted but not stored into @a str.
2400 template<typename _CharT, typename _Traits, typename _Alloc>
2401 basic_istream<_CharT, _Traits>&
2402 getline(basic_istream<_CharT, _Traits>& __is,
2403 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2406 * @brief Read a line from stream into a string.
2407 * @param is Input stream.
2408 * @param str Buffer to store into.
2409 * @return Reference to the input stream.
2411 * Stores characters from is into @a str until '\n' is found, the end of
2412 * the stream is encountered, or str.max_size() is reached. If is.width()
2413 * is non-zero, that is the limit on the number of characters stored into
2414 * @a str. Any previous contents of @a str are erased. If end of line was
2415 * encountered, it is extracted but not stored into @a str.
2417 template<typename _CharT, typename _Traits, typename _Alloc>
2418 inline basic_istream<_CharT, _Traits>&
2419 getline(basic_istream<_CharT, _Traits>& __is,
2420 basic_string<_CharT, _Traits, _Alloc>& __str);
2423 basic_istream<char>&
2424 getline(basic_istream<char>& __in, basic_string<char>& __str,
2427 #ifdef _GLIBCXX_USE_WCHAR_T
2429 basic_istream<wchar_t>&
2430 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2435 #endif /* _BASIC_STRING_H */