谁能告诉我c++里string class的用法?

 我来答
踏岸寻柳
推荐于2016-07-20 · TA获得超过6912个赞
知道小有建树答主
回答量:1776
采纳率:0%
帮助的人:637万
展开全部
Summary

A templatized class for handling sequences of character-like entities. string and wstring are specialized versions of basic_string for char抯 and wchar_t抯, respectively.

typedef basic_string <char> string;
typedef basic_string <wchar_t> wstring;

Synopsis

#include <string>
template <class charT,
class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_string;

Description

basic_string<charT, traits, Allocator> is a homogeneous collection of character-like entities that includes string functions such as compare, append, assign, insert, remove, and replace, along with various searches. basic_string also functions as an STL sequence container that provides random access iterators. This allows some of the generic algorithms to apply to strings.

Any underlying character-like type may be used as long as an appropriate char_traits class is included or the default traits class is applicable.

Interface

template <class charT,
class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_string {
public:
// Types
typedef traits traits_type;
typedef typename traits::char_type value_type;
typedef Allocator allocator_type;
typedef typename Allocator::size_type size_type;
typedef typename Allocator::difference_type difference_type;
typedef typename Allocator::reference reference;

typedef typename Allocator::const_reference const_reference;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef typename Allocator::pointer iterator;
typedef typename Allocator::const_pointer const_iterator;
typedef std::reverse_iterator<const_iterator>
const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
static const size_type npos = -1;

// Constructors/Destructors
explicit basic_string(const Allocator& = Allocator());
basic_string (const basic_string<charT, traits,
Allocator>&);
basic_string(const basic_string&, size_type,
size_type = npos,
const Allocator& a = Allocator());
basic_string(const charT*, size_type,
const Allocator& = Allocator());
basic_string(const charT*, const Allocator& = Allocator());
basic_string(size_type, charT,
const Allocator& = Allocator());

template <class InputIterator>
basic_string(InputIterator, InputIterator,
const Allocator& = Allocator());
~basic_string();
// Assignment operators
basic_string& operator=(const basic_string&);
basic_string& operator=(const charT*);
basic_string& operator=(charT);
// Iterators
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;

reverse_iterator rend();
const_reverse_iterator rend() const;
// Capacity
size_type size() const;
size_type length() const;
size_type max_size() const;
void resize(size_type, charT);
void resize(size_type);
size_type capacity() const;
void reserve(size_type = 0);
bool empty() const;
// Element access
const_reference operator[](size_type) const;
reference operator[](size_type);

const_reference at(size_type) const;
reference at(size_type);
// Modifiers
basic_string& operator+=(const basic_string&);
basic_string& operator+=(const charT*);
basic_string& operator+=(charT);

basic_string& append(const basic_string&);
basic_string& append(const basic_string&,
size_type, size_type);
basic_string& append(const charT*, size_type);
basic_string& append(const charT*);
basic_string& append(size_type, charT);

template<class InputIterator>
basic_string& append(InputIterator, InputIterator);

basic_string& assign(const basic_string&);
basic_string& assign(const basic_string&,
size_type, size_type);
basic_string& assign(const charT*, size_type);
basic_string& assign(const charT*);
basic_string& assign(size_type, charT);
template<class InputIterator>
basic_string& assign(InputIterator, InputIterator);

basic_string& insert(size_type, const basic_string&);

basic_string& insert(size_type, const basic_string&,
size_type, size_type);
basic_string& insert(size_type, const charT*, size_type);
basic_string& insert(size_type, const charT*);
basic_string& insert(size_type, size_type, charT);
iterator insert(iterator, charT = charT());
void insert(iterator, size_type, charT);
template<class InputIterator>
void insert(iterator, InputIterator, InputIterator);
basic_string& erase(size_type = 0, size_type= npos);

iterator erase(iterator);
iterator erase(iterator, iterator);
basic_string& replace(size_type, size_type,
const basic_string&);
basic_string& replace(size_type, size_type,
const basic_string&,
size_type, size_type);
basic_string& replace(size_type, size_type,
const charT*, size_type);
basic_string& replace(size_type, size_type,
const charT*);

basic_string& replace(size_type, size_type,
size_type, charT);
basic_string& replace(iterator, iterator,
const basic_string&);
basic_string& replace(iterator, iterator,
const charT*, size_type);
basic_string& replace(iterator, iterator,
const charT*);
basic_string& replace(iterator, iterator,
size_type, charT);
template<class InputIterator>

basic_string& replace(iterator, iterator,
InputIterator, InputIterator);
size_type copy(charT*, size_type, size_type = 0) const;
void swap(basic_string<charT, traits, Allocator>&);
// String operations
const charT* c_str() const;
const charT* data() const;
const allocator_type& get_allocator() const;

size_type find(const basic_string&,
size_type = 0) const;
size_type find(const charT*,
size_type, size_type) const;

size_type find(const charT*, size_type = 0) const;
size_type find(charT, size_type = 0) const;
size_type rfind(const basic_string&,
size_type = npos) const;
size_type rfind(const charT*,
size_type, size_type) const;
size_type rfind(const charT*,
size_type = npos) const;
size_type rfind(charT, size_type = npos) const;
size_type find_first_of(const basic_string&,
size_type = 0) const;

size_type find_first_of(const charT*,
size_type, size_type) const;
size_type find_first_of(const charT*,
size_type = 0) const;
size_type find_first_of(charT, size_type = 0) const;
size_type find_last_of(const basic_string&,
size_type = npos) const;
size_type find_last_of(const charT*,
size_type, size_type) const;
size_type find_last_of(const charT*, size_type = npos)

const;
size_type find_last_of(charT, size_type = npos) const;
size_type find_first_not_of(const basic_string&,
size_type = 0) const;
size_type find_first_not_of(const charT*,
size_type, size_type) const;
size_type find_first_not_of(const charT*, size_type = 0)
const;
size_type find_first_not_of(charT, size_type = 0) const;
size_type find_last_not_of(const basic_string&,

size_type = npos) const;
size_type find_last_not_of(const charT*,
size_type, size_type) const;
size_type find_last_not_of(const charT*,
size_type = npos) const;
size_type find_last_not_of(charT, size_type = npos)
const;
basic_string substr(size_type = 0, size_type = npos)
const;
int compare(const basic_string&) const;

int compare(size_type, size_type, const basic_string&)
const;
int compare(size_type, size_type, const basic_string&,
size_type, size_type) const;
int compare(size_type, size_type, charT*) const;
int compare(charT*) const;
int compare(size_type, size_type, const charT*,
size_type) const;
};
// Non-member Operators
template <class charT, class traits, class Allocator>
basic_string operator+ (const basic_string&,

const basic_string&);
template <class charT, class traits, class Allocator>
basic_string operator+ (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
basic_string operator+ (charT, const basic_string&);
template <class charT, class traits, class Allocator>
basic_string operator+ (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
basic_string operator+ (const basic_string&, charT);

template <class charT, class traits, class Allocator>
bool operator== (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>
bool operator== (const charT*, const basic_string&);
template <class charT, class traits , class Allocator>
bool operator== (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
bool operator< (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>

bool operator< (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
bool operator< (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
bool operator!= (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>
bool operator!= (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
bool operator!= (const basic_string&, const charT*);

template <class charT, class traits, class Allocator>
bool operator> (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>
bool operator> (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
bool operator> (const basic_string&, const charT*);
template <class charT, class traits, class Allocator>
bool operator<= (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>

bool operator<= (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
bool operator<= (const basic_string&, const charT*);

template <class charT, class traits, class Allocator>
bool operator>= (const basic_string&, const basic_string&);
template <class charT, class traits, class Allocator>
bool operator>= (const charT*, const basic_string&);
template <class charT, class traits, class Allocator>
bool operator>= (const basic_string&, const charT*);

template <class charT, class traits, class Allocator>
void swap(basic_string<charT,traits,Allocator>& a,
basic_string<charT,traits,Allocator>& b);
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>& operator>>
(istream&, basic_string&);
template <class charT, class traits, class Allocator>
basic_ostream<charT, traits>& operator<<
(ostream&, const basic_string&);
template <class Stream, class charT,

class traits, class Allocator>
basic_istream<charT, traits>& getline
(Stream&, basic_string&, charT);

Constructors

In all cases, the Allocator parameter is used for storage management.

explicit
basic_string (const Allocator& a = Allocator());

The default constructor. Creates a basic_string with the following effects:

data() a non-null pointer that is copyable and can have 0 added to it
size() 0
capacity() an unspecified value

basic_string (const basic_string<T, traits,
Allocator>& str);

Creates a string that is a copy of str.

basic_string (const basic_string& str, size_type pos,
size_type n= npos, const allocator&
a=allocator());

Creates a string of pos<=size() and determines length rlen of the initial string value as the smaller of n and str.size() - pos. This has the following effects:

data() points to the first element of an allocated copy of rlen elements of the string controlled by str beginning at position pos
size() rlen
capacity() a value at least as large as size()
get_allocator() str.get_allocator()

An out_of_range exception is thrown if pos>str.size().

basic_string (const charT* s, size_type n,
const Allocator& a = Allocator());

Creates a string that contains the first n characters of s. s must not be a NULL pointer. The effects of this constructor are:

data() points to the first element of an allocated copy of the array whose first element is pointed to by s
size() n
capacity() a value at least as large as size()

A length_error exception is thrown if n == npos.

basic_string (const charT * s,
const Allocator& a = Allocator());

Constructs a string containing all characters in s up to, but not including, a traits::eos() character. s must not be a null pointer. The effects of this constructor are:

data() points to the first element of an allocated copy of the array whose first element is pointed to by s
size() traits::length(s)
capacity() a value at least as large as size()

basic_string (size_type n, charT c,
const Allocator& a = Allocator());

Constructs a string containing n repetitions of c. A length_error exception is thrown if n == npos. The effects of this constructor are:

data() points to the first element of an allocated array of n elements, each storing the initial value c
size() n
capacity() a value at least as large as size()

template <class InputIterator>
basic_string (InputIterator first, InputIterator last,
const Allocator& a = Allocator());

Creates a basic_string of length last - first filled with all values obtained by dereferencing the InputIterators on the range [first, last). The effects of this constructor are:

data() points to the first element of an allocated copy of the elements in the range [first,last)
size() distance between first and last
capacity() a value at least as large as size()

Destructors

~basic_string ();

Releases any allocated memory for this basic_string.

Operators

basic_string&
operator= (const basic_string& str);

Sets the contents of this string to be the same as str. The effects of operator= are:

data() points to the first element of an allocated copy of the array whose first element is pointed to by str.size()
size() str.size()
capacity() a value at least as large as size()

basic_string&
operator= (const charT * s);

Sets the contents of this string to be the same as s up to, but not including, the traits::eos() character.

basic_string&
operator= (charT c);

Sets the contents of this string to be equal to the single charT c.

const_reference
operator[] (size_type pos) const;
reference
operator[] (size_type pos);

If pos < size(), returns the element at position pos in this string. If pos == size(), the const version returns charT(), the behavior of the non-const version is undefined. The reference returned by either version is invalidated by any call to c_str(), data(), or any non-const member function for the object.

basic_string&
operator+= (const basic_string& s);
basic_string&
operator+= (const charT* s);
basic_string&
operator+= (charT c);

Concatenates a string onto the current contents of this string. The second member operator uses traits::length() to determine the number of elements from s to add. The third member operator adds the single character c. All return a reference to this string after completion.

Iterators

iterator begin ();
const_iterator begin () const;

Returns an iterator initialized to the first element of the string.

iterator end ();
const_iterator end () const;

Returns an iterator initialized to the position after the last element of the string.

reverse_iterator rbegin ();
const_reverse_iterator rbegin () const;

Returns an iterator equivalent to reverse_iterator(end()).

reverse_iterator rend ();
const_reverse_iterator rend () const;

Returns an iterator equivalent to reverse_iterator(begin()).

Allocator

const allocator_type get_allocator () const;

Returns a copy of the allocator used by self for storage management.

Member Functions

basic_string&
append (const basic_string& s, size_type pos,
size_type npos);
basic_string&
append (const basic_string& s);
basic_string&
append (const charT* s, size_type n);
basic_string&
append (const charT* s);
basic_string&
append (size_type n, charT c );
template<class InputIterator>
basic_string&
append (InputIterator first, InputIterator last);

Append another string to the end of this string. The first two functions append the lesser of n and s.size() - pos characters of s, beginning at position pos to this string. The second member throws an out_of_range exception if pos > str.size(). The third member appends n characters of the array pointed to by s. The fourth variation appends elements from the array pointed to by s up to, but not including, a charT() character. The fifth variation appends n repetitions of c. The final append function appends the elements specified in the range [first, last).

All functions throw a length_error exception if the resulting lengths exceed max_size(). All return a reference to this string after completion.

basic_string&
assign (const basic_string& s);
basic_string&
assign (const basic_string& s,
size_type pos, size_type n);
basic_string&
assign (const charT* s, size_type n);
basic_string&
assign (const charT* s);
basic_string&
assign (size_type n, charT c );
template<class InputIterator>
basic_string&
assign (InputIterator first, InputIterator last);

Replace the value of this string with the value of another.

All versions of the function assign values to this string. The first two variations assign the lesser of n and s.size() - pos characters of s, beginning at position pos. The second variation throws an out_of_range exception if pos > str.size(). The third version of the function assigns n characters of the array pointed to by s. The fourth version assigns elements from the array pointed to by s up to, but not including, a charT() character. The fifth assigns one or n repetitions of c. The last variation assigns the members specified by the range [first, last).

All functions throw a length_error exception if the resulting lengths exceed max_size(). All return a reference to this string after completion.

const_reference
at (size_type pos) const;
reference
at (size_type pos);

If pos < size(), returns the element at position pos in this string. Otherwise, an out_of_range exception is thrown.

size_type
capacity () const;

Returns the current storage capacity of the string. This is guaranteed to be at least as large as siz
务丿蓓5176
2005-11-15 · TA获得超过363个赞
知道答主
回答量:187
采纳率:100%
帮助的人:0
展开全部
可以看MSDN,说明比较全。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式