Tags:
create new tag
, view all tags

basic_string < charT, traits, Alloc >

Category: containers

Description

basic_string 是基于字符序列容器(Sequence)的模板类, 包含了说有序列容器的常用操作,同时也包含了字符串的标准操作,如"查找"和"合并" 。

basic_string 类是通过字符类型和类型的特征( Character Traits )来实例化的。但通常我们不直接使用 basic_string 模板,而是使用 string 和 wstring , 他们是两个typedef:

typedef basic_string <char> string;
typedef basic_string<wchar_t> wstring;
basic_string的一些成员函数的参数并不像其他容器一样指定一个特定位置和范围。除了像传统的容器使用迭代器iterator之外,basic_string的许多成员函数用单个的size_type类型值表示位置(例如:一般用begin() + pos 表示位置,basic_string的许多成员函数使用两个参数:pos和n,表示一个区间。此时pos表示区间的开始,'n'表示区间的大小,也就是说,这个区间是[begin() + pos, begin() + pos + n) .

值得注意的是,C++标准中没有对basic_string的操作复杂度做详细说明,在SGI的实现中,basic_string和vector的性能差不多: 性能曲线都是恒定的O(1), 只有copy和链接是O(N)的。相比之下,rope 有一个不同的性能曲线:绝大部分操作都是具有log(N)的复杂度。

还要注意的是,C++标准中,basic_string有自己独特的迭代器语法,在swap, reserve, insert, 和 erase操作后,迭代器可能会失效(其他和insert 或 erase函数功能类似的操作也会如此,如:clear, resize, append, replace)。另外,第一次调用non-const成员函数,也包括begin(), operator[], 也有可能导致迭代器无效。(之所以有这些机制,是为了在实现起来更加自由)。不过在SGI的实现版本中,begin(), end(), rbegin(), rend(), operator[], c_str(), 和data()并不会是迭代器无效,迭代器值会在string 内容被明显修改时无效。

Example

int main() { 
        string s(10u, ' ');           // Create a string of ten blanks.
        
        const char* A = "this is a test";
        s += A;
        cout << "s = " << (s + '\n');
        cout << "As a null-terminated sequence: " << s.c_str() << endl;
        cout << "The sixteenth character is " << s[15] << endl;
        
        reverse(s.begin(), s.end());
        s.push_back('\n');
        cout << s;
}
Definition

定义在文件 string.

Template Parameters

参数 描述 缺省
charT string 值的类型: 包含的字符类型
traits Character Traits 类型, 封装了基本的字符操作. char_traits < charT >
Alloc string 置配器:用于内部内存分配管理 alloc

Type requirements

除了需要有随机型迭代器的容器和序列容器要求外,还需要:

  • charT 是 POD ("plain 'ol data") 类型.
  • traits 是 Character Traits 类型,缺省类型是 charT

Members

Member Where defined Description
value_type Container The type of object, CharT, stored in the string.
pointer Container Pointer to CharT.
reference Container Reference to CharT

const_reference

Container Const reference to CharT
size_type

Container An unsigned integral type.
difference_type Container

A signed integral type.
static const size_type npos basic_string The largest possible value of type size_type. That is, size_type(-1).

iterator Container Iterator used to iterate through a string. A basic_string supplies Random Access Iterators.

const_iterator Container Const iterator used to iterate through a string.
reverse_iterator Reversible Container Iterator used to iterate backwards through a string.
const_reverse_iterator

Reversible Container Const iterator used to iterate backwards through a string.
iterator begin() Container

Returns an iterator pointing to the beginning of the string.
iterator end() Container

Returns an iterator pointing to the end of the string.
const_iterator begin() const Container

Returns a const_iterator pointing to the beginning of the string.
const_iterator end() const Container

Returns a const_iterator pointing to the end of the string.
reverse_iterator rbegin() Reversible Container

Returns a reverse_iterator pointing to the beginning of the reversed string.
reverse_iterator rend() Reversible Container

Returns a reverse_iterator pointing to the end of the reversed string.
const_reverse_iterator rbegin() const Reversible Container

Returns a const_reverse_iterator pointing to the beginning of the reversed string.
const_reverse_iterator rend() const Reversible Container

Returns a const_reverse_iterator pointing to the end of the reversed string.
size_type size() const Container

Returns the size of the string.
size_type length() const basic_string Synonym for size().

size_type max_size() const Container Returns the largest possible size of the string.
size_type capacity() const basic_string See below.
bool empty() const Container true if the string's size is 0.
reference operator[](size_type n)

Random Access Container Returns the n'th character.
const_reference operator[](size_type n) const Random Access Container Returns the n'th character.
const charT* c_str() const

basic_string

Returns a pointer to a null-terminated array of characters representing the string's contents.
const charT* data() const basic_string

Returns a pointer to an array of characters (not necessarily null-terminated) representing the string's contents.

basic_string() Container Creates an empty string.
basic_string(const basic_string& s, 
             size_type pos = 0, size_type n = npos)
Container, basic_string Generalization of the copy constructor.

basic_string(const charT*) basic_string Construct a string from a null-terminated character array.

basic_string(const charT* s, size_type n)

basic_string Construct a string from a character array and a length.
basic_string(size_type n, charT c)

Sequence

Create a string with n copies of c.
template <class InputIterator>

basic_string(InputIterator first, InputIterator last)
Sequence Create a string from a range.
~basic_string() Container The destructor.
basic_string& operator=(const basic_string&) Container The assignment operator
basic_string& operator=(const charT* s)

basic_string

Assign a null-terminated character array to a string.
basic_string& operator=(charT c) basic_string

Assign a single character to a string.
void reserve(size_t) basic_string See below.
void swap(basic_string&) Container Swaps the contents of two strings.
iterator insert(iterator pos,
                const T& x)
Sequence Inserts x before pos.

template <class InputIterator>
void insert(iterator pos,
            InputIterator f, InputIterator l)
[1]
Sequence

Inserts the range [first, last) before pos.
void insert(iterator pos, 
            size_type n, const T& x)

Sequence Inserts n copies of x before pos.
basic_string& insert(size_type pos, const basic_string& s) basic_string Inserts s before pos.

basic_string& insert(size_type pos, 
                     const basic_string& s, 
                     size_type pos1, size_type n)
basic_string

Inserts a substring of s before pos.

basic_string& insert(size_type pos, const charT* s) basic_string

Inserts s before pos.
basic_string& insert(size_type pos, const charT* s, size_type n) basic_string Inserts the first n characters of s before pos.
basic_string& insert(size_type pos, size_type n, charT c)

basic_string Inserts n copies of c before pos.
basic_string& append(const basic_string& s) basic_string Append s to *this.

basic_string& append(const basic_string& s, 
                     size_type pos, size_type n)
basic_string

Append a substring of s to *this.

basic_string& append(const charT* s) basic_string

Append s to *this.
basic_string& append(const charT* s, size_type n) basic_string Append the first n characters of s to *this.
basic_string& append(size_type n, charT c) basic_string Append n copies of c to *this.

template <class InputIterator>
basic_string& append(InputIterator first, InputIterator last)
basic_string Append a range to *this.
void push_back(charT c) basic_string Append a single character to *this.

basic_string& operator+=(const basic_string& s) basic_string Equivalent to append(s).

basic_string& operator+=(const charT* s) basic_string Equivalent to append(s)

basic_string& operator+=(charT c) basic_string Equivalent to push_back(c)

iterator erase(iterator p) Sequence Erases the character at position p
iterator erase(iterator first, iterator last) Sequence Erases the range [first, last)
basic_string& erase(size_type pos = 0, size_type n = npos) basic_string Erases a range.

void clear()

Sequence Erases the entire container.
void resize(size_type n, charT c = charT()) Sequence Appends characters, or erases characters from the end, as necessary to make the string's length exactly n characters.
basic_string& assign(const basic_string&)

basic_string Synonym for operator=
 
basic_string& assign(const basic_string& s, 
                     size_type pos, size_type n)

basic_string Assigns a substring of s to *this

basic_string& assign(const charT* s, size_type n)

basic_string Assigns the first n characters of s to *this.

basic_string& assign(const charT* s) basic_string Assigns a null-terminated array of characters to *this.

basic_string& assign(size_type n, charT c) Sequence Erases the existing characters and replaces them by n copies of c.

template <class InputIterator>
basic_string& assign(InputIterator first, InputIterator last)
Sequence

Erases the existing characters and replaces them by [first, last)
basic_string& replace(size_type pos, size_type n, 
                      const basic_string& s)
basic_string Replaces a substring of *this with the string s.
basic_string& replace(size_type pos, size_type n, 
                      const basic_string& s, 
                      size_type pos1, size_type n1)

basic_string Replaces a substring of *this with a substring of s.

basic_string& replace(size_type pos, size_type n, 
                      const charT* s, size_type n1)
basic_string Replaces a substring of *this with the first n1 characters of s.

basic_string& replace(size_type pos, size_type n, 
                      const charT* s)
basic_string Replaces a substring of *this with a null-terminated character array.

basic_string& replace(size_type pos, size_type n, 
                      size_type n1, charT c)
basic_string Replaces a substring of *this with n1 copies of c.

basic_string& replace(iterator first, iterator last, 
                      const basic_string& s)
basic_string

Replaces a substring of *this with the string s.

basic_string& replace(iterator first, iterator last, 
                      const charT* s, size_type n)

basic_string

Replaces a substring of *this with the first n characters of s.

basic_string& replace(iterator first, iterator last, 
                      const charT* s)
basic_string Replaces a substring of *this with a null-terminated character array.
basic_string& replace(iterator first, iterator last, 
                      size_type n, charT c)
basic_string Replaces a substring of *this with n copies of c.

template <class InputIterator>
basic_string& replace(iterator first, iterator last, 
                      InputIterator f, InputIterator l)
basic_string Replaces a substring of *this with the range [f, l)
size_type copy(charT* buf, size_type n, size_type pos = 0) const basic_string

Copies a substring of *this to a buffer.
size_type find(const basic_string& s, size_type pos = 0) const basic_string

Searches for s as a substring of *this, beginning at character pos of *this.

size_type find(const charT* s, size_type pos, size_type n) const

basic_string Searches for the first n characters of s as a substring of *this, beginning at character pos of *this.

size_type find(const charT* s, size_type pos = 0) const basic_string Searches for a null-terminated character array as a substring of *this, beginning at character pos of *this.

size_type find(charT c, size_type pos = 0) const basic_string Searches for the character c, beginning at character position pos.

size_type rfind(const basic_string& s, size_type pos = npos) const basic_string Searches backward for s as a substring of *this, beginning at character position min(pos, size())

size_type rfind(const charT* s, size_type pos, size_type n) const basic_string Searches backward for the first n characters of s as a substring of *this, beginning at character position min(pos, size())

size_type rfind(const charT* s, size_type pos = npos) const basic_string Searches backward for a null-terminated character array as a substring of *this, beginning at character min(pos, size())

size_type rfind(charT c, size_type pos = npos) const basic_string Searches backward for the character c, beginning at character position min(pos, size().

size_type find_first_of(const basic_string& s, size_type pos = 0) const basic_string Searches within *this, beginning at pos, for the first character that is equal to any character within s.

size_type find_first_of(const charT* s, size_type pos, size_type n) const basic_string Searches within *this, beginning at pos, for the first character that is equal to any character within the first n characters of s.

size_type find_first_of(const charT* s, size_type pos = 0) const basic_string Searches within *this, beginning at pos, for the first character that is equal to any character within s.

size_type find_first_of(charT c, size_type pos = 0) const basic_string Searches within *this, beginning at pos, for the first character that is equal to c.

size_type find_first_not_of(const basic_string& s, size_type pos = 0) const basic_string Searches within *this, beginning at pos, for the first character that is not equal to any character within s.

size_type find_first_not_of(const charT* s, size_type pos, size_type n) const basic_string Searches within *this, beginning at pos, for the first character that is not equal to any character within the first n characters of s.

size_type find_first_not_of(const charT* s, size_type pos = 0) const basic_string Searches within *this, beginning at pos, for the first character that is not equal to any character within s.

size_type find_first_not_of(charT c, size_type pos = 0) const basic_string Searches within *this, beginning at pos, for the first character that is not equal to c.

size_type find_last_of(const basic_string& s, size_type pos = npos) const basic_string Searches backward within *this, beginning at min(pos, size()), for the first character that is equal to any character within s.

size_type find_last_of(const charT* s, size_type pos, size_type n) const basic_string Searches backward within *this, beginning at min(pos, size()), for the first character that is equal to any character within the first n characters of s.

size_type find_last_of(const charT* s, size_type pos = npos) const basic_string Searches backward *this, beginning at min(pos, size()), for the first character that is equal to any character within s.

size_type find_last_of(charT c, size_type pos = npos) const basic_string Searches backward *this, beginning at min(pos, size()), for the first character that is equal to c.

size_type find_last_not_of(const basic_string& s, size_type pos = npos) const basic_string Searches backward within *this, beginning at min(pos, size()), for the first character that is not equal to any character within s.

size_type find_last_not_of(const charT* s, size_type pos, size_type n) const basic_string Searches backward within *this, beginning at min(pos, size()), for the first character that is not equal to any character within the first n characters of s.

size_type find_last_not_of(const charT* s, size_type pos = npos) const basic_string Searches backward *this, beginning at min(pos, size()), for the first character that is not equal to any character within s.

size_type find_last_not_of(charT c, size_type pos = npos) const basic_string Searches backward *this, beginning at min(pos, size()), for the first character that is not equal to c.

basic_string substr(size_type pos = 0, size_type n = npos) const basic_string Returns a substring of *this.
int compare(const basic_string& s) const basic_string Three-way lexicographical comparison of s and *this.

int compare(size_type pos, size_type n, const basic_string& s) const basic_string Three-way lexicographical comparison of s and a substring of *this.

int compare(size_type pos, size_type n, const basic_string& s, size_type pos1, size_type n1) const basic_string Three-way lexicographical comparison of a substring of s and a substring of *this.

int compare(const charT* s) const basic_string Three-way lexicographical comparison of s and *this.

int compare(size_type pos, size_type n, const charT* s, size_type len = npos) const basic_string Three-way lexicographical comparison of the first min(len, traits::length(s) characters of s and a substring of *this.

template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>
operator+(const basic_string<charT, traits, Alloc>& s1,
          const basic_string<charT, traits, Alloc>& s2)

basic_string String concatenation. A global function, not a member function.
template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>

operator+(const charT* s1,
          const basic_string<charT, traits, Alloc>& s2)
basic_string String concatenation. A global function, not a member function.

template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>
operator+(const basic_string<charT, traits, Alloc>& s1,
          const charT* s2)
basic_string

String concatenation. A global function, not a member function.

template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>
operator+(charT c,
          const basic_string<charT, traits, Alloc>& s2)

basic_string String concatenation. A global function, not a member function.
template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>

operator+(const basic_string<charT, traits, Alloc>& s1,
          charT c)
basic_string String concatenation. A global function, not a member function.

template <class charT, class traits, class Alloc>
bool operator==(const basic_string<charT, traits, Alloc>& s1,
                const basic_string<charT, traits, Alloc>& s2)
Container String equality. A global function, not a member function.
template <class charT, class traits, class Alloc>
bool operator==(const charT* s1, 
                const basic_string<charT, traits, Alloc>& s2)

basic_string

String equality. A global function, not a member function.
template <class charT, class traits, class Alloc>
bool operator==(const basic_string<charT, traits, Alloc>& s1,
                const charT* s2)

basic_string String equality. A global function, not a member function.
template <class charT, class traits, class Alloc>
bool operator!=(const basic_string<charT, traits, Alloc>& s1,
                const basic_string<charT, traits, Alloc>& s2)

Container String inequality. A global function, not a member function.
template <class charT, class traits, class Alloc>

bool operator!=(const charT* s1, 
                const basic_string<charT, traits, Alloc>& s2)
basic_string String inequality. A global function, not a member function.

template <class charT, class traits, class Alloc>
bool operator!=(const basic_string<charT, traits, Alloc>& s1,
                const charT* s2)
basic_string String inequality. A global function, not a member function.
template <class charT, class traits, class Alloc>
bool operator<(const basic_string<charT, traits, Alloc>& s1,
               const basic_string<charT, traits, Alloc>& s2)

Container

String comparison. A global function, not a member function.
template <class charT, class traits, class Alloc>
bool operator<(const charT* s1, 
               const basic_string<charT, traits, Alloc>& s2)

basic_string String comparison. A global function, not a member function.
template <class charT, class traits, class Alloc>
bool operator<(const basic_string<charT, traits, Alloc>& s1,
               const charT* s2)

basic_string String comparison. A global function, not a member function.
template <class charT, class traits, class Alloc>
void swap(basic_string<charT, traits, Alloc>& s1,
          basic_string<charT, traits, Alloc>& s2)

Container Swaps the contents of two strings.
template <class charT, class traits, class Alloc>

basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
           basic_string<charT, traits, Alloc>& s)
basic_string Reads s from the input stream is
template <class charT, class traits, class Alloc>
basic_ostream<charT, traits>&

operator<<(basic_istream<charT, traits>& os,
           const basic_string<charT, traits, Alloc>& s)
basic_string Writes s to the output stream os

template <class charT, class traits, class Alloc>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is,
        basic_string<charT, traits, Alloc>& s,
        charT delim)

basic_string Reads a string from the input stream is, stopping when it reaches delim

template <class charT, class traits, class Alloc>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is,
        basic_string<charT, traits, Alloc>& s)
basic_string

Reads a single line from the input stream is

New members

These members are not defined in the Random Access Container and Sequence: requirements, but are specific to basic_string.

Member Description
static const size_type npos The largest possible value of type size_type. That is, size_type(-1).

size_type length() const Equivalent to size().
size_type capacity() const

Number of elements for which memory has been allocated. That is, the size to which the string can grow before memory must be reallocated. capacity() is always greater than or equal to size().
const charT* c_str() const Returns a pointer to a null-terminated array of characters representing the string's contents. For any string s it is guaranteed that the first s.size() characters in the array pointed to by s.c_str()

are equal to the character in s, and that s.c_str()[s.size()] is a null character. Note, however, that it not necessarily the first null character. Characters within a string are permitted to be null.

const charT* data() const Returns a pointer to an array of characters, not necessarily null-terminated, representing the string's contents. data()

is permitted, but not required, to be identical to c_str(). The first size() characters of that array are guaranteed to be identical to the characters in *this. The return value of data() is never a null pointer, even if size() is zero.

basic_string(const basic_string& s, size_type pos = 0, size_type n = npos) Constructs a string from a substring of s. The substring begins at character position pos and terminates at character position pos + n or at the end of s, whichever comes first. This constructor throws out_of_range if pos > s.size(). Note that when pos and n

have their default values, this is just a copy constructor.

basic_string(const charT* s) Equivalent to basic_string(s, s + traits::length(s)).

basic_string(const charT* s, size_type n)

Equivalent to basic_string(s, s + n).
basic_string& operator=(const charT* s)

Equivalent to operator=(basic_string(s)).

basic_string& operator=(charT c) Assigns to *this a string whose size is 1 and whose contents is the single character c.

void reserve(size_t n) Requests that the string's capacity be changed; the postcondition for this member function is that, after it is called, capacity() >= n. You may request that a string decrease its capacity by calling reserve() with an argument less than the current capacity. (If you call reserve() with an argument less than the string's size, however, the capacity will only be reduced to size(). A string's size can never be greater than its capacity.) reserve() throws length_error if n > max_size().

basic_string& insert(size_type pos, const basic_string& s) If pos > size(), throws out_of_range. Otherwise, equivalent to insert(begin() + pos, s.begin(), s.end()).

basic_string& insert(size_type pos, 
                     const basic_string& s, 
                     size_type pos1, size_type n)
If pos > size() or pos1 > s.size(), throws out_of_range. Otherwise, equivalent to insert(begin() + pos, s.begin() + pos1, s.begin() + pos1 + min(n, s.size() - pos1)).

basic_string& insert(size_type pos, const charT* s) If pos > size(), throws out_of_range. Otherwise, equivalent to insert(begin() + pos, s, s + traits::length(s))

basic_string& insert(size_type pos, const charT* s, size_type n) If pos > size(), throws out_of_range. Otherwise, equivalent to insert(begin() + pos, s, s + n).

basic_string& insert(size_type pos, size_type n, charT c) If pos > size(), throws out_of_range. Otherwise, equivalent to insert(begin() + pos, n, c).

basic_string& append(const basic_string& s) Equivalent to insert(end(), s.begin(), s.end()).
basic_string& append(const basic_string& s, 
                     size_type pos, size_type n)
If pos > s.size(), throws out_of_range. Otherwise, equivalent to insert(end(), s.begin() + pos, s.begin() + pos + min(n, s.size() - pos)).

basic_string& append(const charT* s) Equivalent to insert(end(), s, s + traits::length(s)).

basic_string& append(const charT* s, size_type n)

Equivalent to insert(end(), s, s + n).
basic_string& append(size_type n, charT c)

Equivalent to insert(end(), n, c).
template <class InputIterator>
basic_string& append(InputIterator first, InputIterator last)

Equivalent to insert(end(), first, last).
void push_back(charT c) Equivalent to insert(end(), c)
basic_string& operator+=(const basic_string& s) Equivalent to append(s).
basic_string& operator+=(const charT* s) Equivalent to append(s)
basic_string& operator+=(charT c)

Equivalent to push_back(c)
basic_string& erase(size_type pos = 0, size_type n = npos) If pos > size(), throws out_of_range. Otherwise, equivalent to erase(begin() + pos, begin() + pos + min(n, size() - pos)).

basic_string& assign(const basic_string& s) Synonym for operator=
basic_string& assign(const basic_string& s, 
                     size_type pos, size_type n)
Equivalent to (but probably faster than) clear() followed by insert(0, s, pos, n).

basic_string& assign(const charT* s, size_type n) Equivalent to (but probably faster than) clear() followed by insert(0, s, n).
basic_string& assign(const charT* s) Equivalent to (but probably faster than) clear() followed by insert(0, s).
basic_string& replace(size_type pos, size_type n, 
                      const basic_string& s)
Equivalent to erase(pos, n) followed by insert(pos, s).

basic_string& replace(size_type pos, size_type n, 
                      const basic_string& s, size_type pos1, size_type n1)
Equivalent to erase(pos, n) followed by insert(pos, s, pos1, n1).

basic_string& replace(size_type pos, size_type n, 
                      const charT* s, size_type n1)
Equivalent to erase(pos, n) followed by insert(pos, s, n1).

basic_string& replace(size_type pos, size_type n, 
                      const charT* s)
Equivalent to erase(pos, n) followed by insert(pos, s).

basic_string& replace(size_type pos, size_type n, 
                      size_type n1, charT c)
Equivalent to erase(pos, n) followed by insert(pos, n1, c).

basic_string& replace(iterator first, iterator last, 
                      const basic_string& s)
Equivalent to insert(erase(first, last), s.begin(), s.end()).
basic_string& replace(iterator first, iterator last, 
                      const charT* s, size_type n)
Equivalent to insert(erase(first, last), s, s + n).

basic_string& replace(iterator first, iterator last, 
                      const charT* s)
Equivalent to insert(erase(first, last), s, s + traits::length(s)).
basic_string& replace(iterator first, iterator last, 
                      size_type n, charT c)

Equivalent to insert(erase(first, last), n, c).
template <class InputIterator>
basic_string& replace(iterator first, iterator last, 
                      InputIterator f, InputIterator l)

Equivalent to insert(erase(first, last), f, l).
size_type copy(charT* buf, size_type n, size_type pos = 0) const Copies at most n characters from *this to a character array. Throws out_of_range if pos > size(). Otherwise, equivalent to copy(begin() + pos, begin() + pos + min(n, size()), buf). Note that this member function does nothing other than copy characters from *this to buf; in particular, it does not terminate buf with a null character.

size_type find(const basic_string& s, size_type pos = 0) const Searches for s as a substring of *this, beginning at character position pos. It is almost the same as search, except that search tests elements for equality using operator== or a user-provided function object, while this member function uses traits::eq. Returns the lowest character position N such that pos <= N and pos + s.size() <= size() and such that, for every i less than s.size(), (*this)[N + i] compares equal to s[i]. Returns npos if no such position N exists. Note that it is legal to call this member function with arguments such that s.size() > size() - pos, but such a search will always fail.

size_type find(const charT* s, size_type pos, size_type n) const Searches for the first n characters of s as a substring of *this, beginning at character pos of *this. This is equivalent to find(basic_string(s, n), pos).

size_type find(const charT* s, size_type pos = 0) const Searches for a null-terminated character array as a substring of *this, beginning at character pos of *this. This is equivalent to find(basic_string(s), pos).

size_type find(charT c, size_type pos = 0) const Searches for the character c, beginning at character position pos. That is, returns the first character position N greater than or equal to pos, and less than size(), such that (*this)[N] compares equal to c. Returns npos if no such character position N exists.

size_type rfind(const basic_string& s, size_type pos = npos) const Searches backward for s as a substring of *this. It is almost the same as find_end, except that find_end tests elements for equality using operator== or a user-provided function object, while this member function uses traits::eq. This member function returns the largest character position N such that N <= pos and N + s.size() <= size(), and such that, for every i less than s.size(), (*this)[N + i]

compares equal to s[i]. Returns npos if no such position N exists. Note that it is legal to call this member function with arguments such that s.size() > size(), but such a search will always fail.

size_type rfind(const charT* s, size_type pos, size_type n) const Searches backward for the first n characters of s as a substring of *this. Equivalent to rfind(basic_string(s, n), pos).
size_type rfind(const charT* s, size_type pos = npos) const Searches backward for a null-terminated character array as a substring of *this. Equivalent to rfind(basic_string(s), pos).
size_type rfind(charT c, size_type pos = npos) const

Searches backward for the character c. That is, returns the largest character position N such that N <= pos and N < size(), and such that (*this)[N] compares equal to c. Returns npos if no such character position exists.

size_type find_first_of(const basic_string& s, size_type pos = 0) const Searches within *this, beginning at pos, for the first character that is equal to any character within s. This is similar to the standard algorithm find_first_of, but differs because find_first_of compares characters using operator== or a user-provided function object, while this member function uses traits::eq. Returns the smallest character position N such that pos <= N < size(), and such that (*this)[N]

compares equal to some character within s. Returns npos if no such character position exists.

size_type find_first_of(const charT* s, size_type pos, size_type n) const Searches within *this, beginning at pos, for the first character that is equal to any character within the range [s, s+n). That is, returns the smallest character position N such that pos <= N < size(), and such that (*this)[N]

compares equal to some character in [s, s+n). Returns npos if no such character position exists.

size_type find_first_of(const charT* s, size_type pos = 0) const Equivalent to find_first_of(s, pos, traits::length(s)).

size_type find_first_of(charT c, size_type pos = 0) const Equivalent to find(c, pos).
size_type find_first_not_of(const basic_string& s, size_type pos = 0) const

Searches within *this, beginning at pos, for the first character that is not equal to any character within s. Returns the smallest character position N such that pos <= N < size(), and such that (*this)[N]

does not compare equal to any character within s. Returns npos if no such character position exists.

size_type find_first_not_of(const charT* s, size_type pos, size_type n) const Searches within *this, beginning at pos, for the first character that is not equal to any character within the range [s, s+n). That is, returns the smallest character position N such that pos <= N < size(), and such that (*this)[N]

does not compare equal to any character in [s, s+n). Returns npos if no such character position exists.

size_type find_first_not_of(const charT* s, size_type pos = 0) const Equivalent to find_first_not_of(s, pos, traits::length(s)).

size_type find_first_not_of(charT c, size_type pos = 0) const Returns the smallest character position N such that pos <= N < size(), and such that (*this)[N]

does not compare equal to c. Returns npos if no such character position exists.

size_type find_last_of(const basic_string& s, size_type pos = npos) const

Searches backward within *this for the first character that is equal to any character within s. That is, returns the largest character position N such that N <= pos and N < size(), and such that (*this)[N] compares equal to some character within s. Returns npos if no such character position exists.

size_type find_last_of(const charT* s, size_type pos, size_type n) const Searches backward within *this for the first character that is equal to any character within the range [s, s+n). That is, returns the largest character position N such that N <= pos and N < size(), and such that (*this)[N] compares equal to some character within [s, s+n). Returns npos if no such character position exists.

size_type find_last_of(const charT* s, size_type pos = npos) const Equivalent to find_last_of(s, pos, traits::length(s)).
size_type find_last_of(charT c, size_type pos = npos) const

Equivalent to rfind(c, pos).
size_type find_last_not_of(const basic_string& s, size_type pos = npos) const Searches backward within *this for the first character that is not equal to any character within s. That is, returns the largest character position N such that N <= pos and N < size(), and such that (*this)[N] does not compare equal to any character within s. Returns npos if no such character position exists.

size_type find_last_not_of(const charT* s, size_type pos, size_type n) const Searches backward within *this for the first character that is not equal to any character within [s, s+n). That is, returns the largest character position N such that N <= pos and N < size(), and such that (*this)[N] does not compare equal to any character within [s, s+n). Returns npos if no such character position exists.

size_type find_last_not_of(const charT* s, size_type pos = npos) const Equivalent to find_last_of(s, pos, traits::length(s)).
size_type find_last_not_of(charT c, size_type pos = npos) const

Searches backward *this for the first character that is not equal to c. That is, returns the largest character position N such that N <= pos and N < size(), and such that (*this)[N] does not compare equal to c.

basic_string substr(size_type pos = 0, size_type n = npos) const Equivalent to basic_string(*this, pos, n).
int compare(const basic_string& s) const

Three-way lexicographical comparison of s and *this, much like strcmp. If traits::compare(data, s.data(), min(size(), s.size())) is nonzero, then it returns that nonzero value. Otherwise returns a negative number if size() < s.size(), a positive number if size() > s.size(), and zero if the two are equal.

int compare(size_type pos, size_type n, const basic_string& s) const Three-way lexicographical comparison of s and a substring of *this. Equivalent to basic_string(*this, pos, n).compare(s).

int compare(size_type pos, size_type n, const basic_string& s, size_type pos1, size_type n1) const Three-way lexicographical comparison of a substring of s and a substring of *this. Equivalent to basic_string(*this, pos, n).compare(basic_string(s, pos1, n1)).

int compare(const charT* s) const Three-way lexicographical comparison of s and *this. Equivalent to compare(basic_string(s)).
int compare(size_type pos, size_type n, const charT* s, size_type len = npos) const Three-way lexicographical comparison of the first min(len, traits::length(s) characters of s and a substring of *this. Equivalent to basic_string(*this, pos, n).compare(basic_string(s, min(len, traits::length(s)))).

template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>
operator+(const basic_string<charT, traits, Alloc>& s1,
          const basic_string<charT, traits, Alloc>& s2)

String concatenation. Equivalent to creating a temporary copy of s, appending s2, and then returning the temporary copy.
template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>

operator+(const charT* s1,
          const basic_string<charT, traits, Alloc>& s2)
String concatenation. Equivalent to creating a temporary basic_string object from s1, appending s2, and then returning the temporary object.
template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>
operator+(const basic_string<charT, traits, Alloc>& s1,
          const charT* s2)
String concatenation. Equivalent to creating a temporary copy of s, appending s2, and then returning the temporary copy.

template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>
operator+(charT c,
          const basic_string<charT, traits, Alloc>& s2)
String concatenation. Equivalent to creating a temporary object with the constructor basic_string(1, c), appending s2, and then returning the temporary object.
template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>

operator+(const basic_string<charT, traits, Alloc>& s1,
          charT c)
String concatenation. Equivalent to creating a temporary object, appending c with push_back, and then returning the temporary object.

template <class charT, class traits, class Alloc>
bool operator==(const charT* s1, 
                const basic_string<charT, traits, Alloc>& s2)
String equality. Equivalent to basic_string(s1).compare(s2) == 0.
template <class charT, class traits, class Alloc>
bool operator==(const basic_string<charT, traits, Alloc>& s1,
                const charT* s2)
String equality. Equivalent to basic_string(s1).compare(s2) == 0.
template <class charT, class traits, class Alloc>
bool operator!=(const charT* s1, 
                const basic_string<charT, traits, Alloc>& s2)
String inequality. Equivalent to basic_string(s1).compare(s2) == 0.
template <class charT, class traits, class Alloc>
bool operator!=(const basic_string<charT, traits, Alloc>& s1,
                const charT* s2)
String inequality. Equivalent to !(s1 == s2).

template <class charT, class traits, class Alloc>
bool operator<(const charT* s1, 
               const basic_string<charT, traits, Alloc>& s2)
String comparison. Equivalent to !(s1 == s2).

template <class charT, class traits, class Alloc>
bool operator<(const basic_string<charT, traits, Alloc>& s1,
               const charT* s2)
String comparison. Equivalent to !(s1 == s2).

template <class charT, class traits, class Alloc>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
           basic_string<charT, traits, Alloc>& s)

Reads s from the input stream is. Specifically, it skips whitespace, and then replaces the contents of s with characters read from the input stream. It continues reading characters until it encounters a whitespace character (in which case that character is not extracted), or until end-of-file, or, if is.width() is nonzero, until it has read is.width() characters. This member function resets is.width() to zero.

template <class charT, class traits, class Alloc>
basic_ostream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
           const basic_string<charT, traits, Alloc>& s)

Writes s to the output stream is. It writes max(s.size(), is.width()) characters, padding as necessary. This member function resets is.width() to zero.
template <class charT, class traits, class Alloc>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is,
        basic_string<charT, traits, Alloc>& s,
        charT delim)

Replaces the contents of s with characters read from the input stream. It continues reading characters until it encounters the character delim (in which case that character is extracted but not stored in s), or until end of file. Note that getline, unlike operator>>, does not skip whitespace. As the name suggests, it is most commonly used to read an entire line of text precisely as the line appears in an input file.
template <class charT, class traits, class Alloc>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is,
        basic_string<charT, traits, Alloc>& s)

Equivalent to getline(is, s, is.widen('\n\)).

-- WinterWen - 03 Jul 2005

Topic revision: r2 - 2005-07-03 - WinterWen
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback 京ICP备05049167号