📄 basic_string.h
字号:
*/ explicit basic_string(const _Alloc& __a); // NB: per LWG issue 42, semantics different from IS: /** * @brief Construct string with copy of value of @a str. * @param str Source string. */ basic_string(const basic_string& __str); /** * @brief Construct string as copy of a substring. * @param str Source string. * @param pos Index of first character to copy from. * @param n Number of characters to copy (default remainder). */ basic_string(const basic_string& __str, size_type __pos, size_type __n = npos); /** * @brief Construct string as copy of a substring. * @param str Source string. * @param pos Index of first character to copy from. * @param n Number of characters to copy. * @param a Allocator to use. */ basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Alloc& __a); /** * @brief Construct string initialized by a character array. * @param s Source character array. * @param n Number of characters to copy. * @param a Allocator to use (default is default allocator). * * NB: s must have at least n characters, '\0' has no special * meaning. */ basic_string(const _CharT* __s, size_type __n, const _Alloc& __a = _Alloc()); /** * @brief Construct string as copy of a C string. * @param s Source C string. * @param a Allocator to use (default is default allocator). */ basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()); /** * @brief Construct string as multiple characters. * @param n Number of characters. * @param c Character to use. * @param a Allocator to use (default is default allocator). */ basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()); /** * @brief Construct string as copy of a range. * @param beg Start of range. * @param end End of range. * @param a Allocator to use (default is default allocator). */ template<class _InputIterator> basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a = _Alloc()); /** * @brief Destroy the string instance. */ ~basic_string() { _M_rep()->_M_dispose(this->get_allocator()); } /** * @brief Assign the value of @a str to this string. * @param str Source string. */ basic_string& operator=(const basic_string& __str) { this->assign(__str); return *this; } /** * @brief Copy contents of @a s into this string. * @param s Source null-terminated string. */ basic_string& operator=(const _CharT* __s) { this->assign(__s); return *this; } /** * @brief Set value to string of length 1. * @param c Source character. * * Assigning to a character makes this string length 1 and * (*this)[0] == @a c. */ basic_string& operator=(_CharT __c) { this->assign(1, __c); return *this; } // Iterators: /** * Returns a read/write iterator that points to the first character in * the %string. Unshares the string. */ iterator begin() { _M_leak(); return iterator(_M_data()); } /** * Returns a read-only (constant) iterator that points to the first * character in the %string. */ const_iterator begin() const { return const_iterator(_M_data()); } /** * Returns a read/write iterator that points one past the last * character in the %string. Unshares the string. */ iterator end() { _M_leak(); return iterator(_M_data() + this->size()); } /** * Returns a read-only (constant) iterator that points one past the * last character in the %string. */ const_iterator end() const { return const_iterator(_M_data() + this->size()); } /** * Returns a read/write reverse iterator that points to the last * character in the %string. Iteration is done in reverse element * order. Unshares the string. */ reverse_iterator rbegin() { return reverse_iterator(this->end()); } /** * Returns a read-only (constant) reverse iterator that points * to the last character in the %string. Iteration is done in * reverse element order. */ const_reverse_iterator rbegin() const { return const_reverse_iterator(this->end()); } /** * Returns a read/write reverse iterator that points to one before the * first character in the %string. Iteration is done in reverse * element order. Unshares the string. */ reverse_iterator rend() { return reverse_iterator(this->begin()); } /** * Returns a read-only (constant) reverse iterator that points * to one before the first character in the %string. Iteration * is done in reverse element order. */ const_reverse_iterator rend() const { return const_reverse_iterator(this->begin()); } public: // Capacity: /// Returns the number of characters in the string, not including any /// null-termination. size_type size() const { return _M_rep()->_M_length; } /// Returns the number of characters in the string, not including any /// null-termination. size_type length() const { return _M_rep()->_M_length; } /// Returns the size() of the largest possible %string. size_type max_size() const { return _Rep::_S_max_size; } /** * @brief Resizes the %string to the specified number of characters. * @param n Number of characters the %string should contain. * @param c Character to fill any new elements. * * This function will %resize the %string to the specified * number of characters. If the number is smaller than the * %string's current size the %string is truncated, otherwise * the %string is extended and new elements are set to @a c. */ void resize(size_type __n, _CharT __c); /** * @brief Resizes the %string to the specified number of characters. * @param n Number of characters the %string should contain. * * This function will resize the %string to the specified length. If * the new size is smaller than the %string's current size the %string * is truncated, otherwise the %string is extended and new characters * are default-constructed. For basic types such as char, this means * setting them to 0. */ void resize(size_type __n) { this->resize(__n, _CharT()); } /** * Returns the total number of characters that the %string can hold * before needing to allocate more memory. */ size_type capacity() const { return _M_rep()->_M_capacity; } /** * @brief Attempt to preallocate enough memory for specified number of * characters. * @param n Number of characters required. * @throw std::length_error If @a n exceeds @c max_size(). * * This function attempts to reserve enough memory for the * %string to hold the specified number of characters. If the * number requested is more than max_size(), length_error is * thrown. * * The advantage of this function is that if optimal code is a * necessity and the user can determine the string length that will be * required, the user can reserve the memory in %advance, and thus * prevent a possible reallocation of memory and copying of %string * data. */ void reserve(size_type __res_arg = 0); /** * Erases the string, making it empty. */ void clear() { _M_mutate(0, this->size(), 0); } /** * Returns true if the %string is empty. Equivalent to *this == "". */ bool empty() const { return this->size() == 0; } // Element access: /** * @brief Subscript access to the data contained in the %string. * @param n The index of the character to access. * @return Read-only (constant) reference to the character. * * This operator allows for easy, array-style, data access. * Note that data access with this operator is unchecked and * out_of_range lookups are not defined. (For checked lookups * see at().) */ const_reference operator[] (size_type __pos) const { _GLIBCXX_DEBUG_ASSERT(__pos <= size()); return _M_data()[__pos]; } /** * @brief Subscript access to the data contained in the %string. * @param n The index of the character to access. * @return Read/write reference to the character. * * This operator allows for easy, array-style, data access. * Note that data access with this operator is unchecked and * out_of_range lookups are not defined. (For checked lookups * see at().) Unshares the string. */ reference operator[](size_type __pos) { _GLIBCXX_DEBUG_ASSERT(__pos < size()); _M_leak(); return _M_data()[__pos]; } /** * @brief Provides access to the data contained in the %string. * @param n The index of the character to access. * @return Read-only (const) reference to the character. * @throw std::out_of_range If @a n is an invalid index. * * This function provides for safer data access. The parameter is * first checked that it is in the range of the string. The function * throws out_of_range if the check fails. */ const_reference at(size_type __n) const { if (__n >= this->size()) __throw_out_of_range(__N("basic_string::at")); return _M_data()[__n]; } /** * @brief Provides access to the data contained in the %string. * @param n The index of the character to access. * @return Read/write reference to the character. * @throw std::out_of_range If @a n is an invalid index. * * This function provides for safer data access. The parameter is * first checked that it is in the range of the string. The function * throws out_of_range if the check fails. Success results in * unsharing the string. */ reference at(size_type __n) { if (__n >= size()) __throw_out_of_range(__N("basic_string::at")); _M_leak(); return _M_data()[__n]; } // Modifiers: /** * @brief Append a string to this string. * @param str The string to append. * @return Reference to this string. */ basic_string& operator+=(const basic_string& __str) { return this->append(__str); } /** * @brief Append a C string. * @param s The C string to append. * @return Reference to this string. */ basic_string&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -