basic_string クラステンプレート †
クラステンプレート basic_string は、テンプレート引数? charT で指定した文字様の型を要素に持つ、可変長の列を保持することができる。列の要素をあらわす番号は 0 から始まる。文字様の型が文脈から明らかな場合、こうした列は「文字列」と呼ばれる。文字列を格納するための記憶域は、テンプレート引数?で指定した Allocator クラスが用いられる。記憶域の割り付けと解放は、メンバ関数の呼び出しによって、適宜行われる。
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_string {
typedef traits traits_type;
typedef typename traits::char_type char_type;
typedef Allocator allocator_type;
typedef typename traits::size_type size_type;
typedef typename traits::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 処理系定義の型 iterator;
typedef 処理系定義の型 const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
static const size_type npos = -1;
explicit basic_string(const Allocator& a = Allocator());
basic_string(const basic_string& str, size_type pos = 0,
size_type n = npos, const Allocator& a = Allocator());
basic_string(const charT* s,
size_type n, const Allocator& a = Allocator());
basic_string(const charT* s, const Allocator& a = Allocator());
basic_string(size_type n, charT c, const Allocator& a = Allocator());
template<class InputIterator>
basic_string(InputIterator begin, InputIterator end,
const Allocator& a = Allocator());
~basic_string();
basic_string& operator=(const basic_string& str);
basic_string& operator=(const charT* s);
basic_string& operator=(charT c);
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;
size_type size() const;
size_type length() const;
size_type max_size() const;
void resize(size_type n, charT c);
void resize(size_type n);
size_type capacity() const;
void reserve(size_type res_arg = 0) const;
void clear();
bool empty() const;
const_reference oprator[](size_type pos) const;
reference oprator[](size_type pos);
const_reference at(size_type n) const;
reference at(size_type n);
basic_string& operator+=(const basic_string& str);
basic_string& operator+=(const charT* s);
basic_string& operator+=(charT c);
basic_string& append(const basic_string& str);
basic_string& append(const basic_string& str, size_type pos, size_type n);
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(InputIteratorfirst, InputIteratorlast);
void push_back(const charT c);
basic_string& assign(const basic_string& str);
basic_string& assign(const basic_string& str, 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);
basic_string& insert(size_type pos1, const basic_string& str);
basic_string& insert(size_type pos1, const basic_string& str
size_type pos2, size_type n);
basic_string& insert(size_type pos, const charT* s, size_type n);
basic_string& insert(size_type pos, const charT* s);
basic_string& insert(size_type pos, size_type n, charT c);
iterator insert(iterator p, charT c);
void insert(iterator p, size_type n, charT c);
template<class InputIterator>
void insert(iterator p, InputIterator first, InputIterator last);
basic_string& erase(size_type pos = 0, size_type n = npos);
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
basic_string& replace(size_type pos1, const basic_string& str);
basic_string& replace(size_type pos1, size_type n1, const basic_string& str
size_type pos2, size_type n2);
basic_string& replace(size_type pos1, size_type n1, const charT* s
size_type n2);
basic_string& replace(size_type pos1, size_type n1, const charT* s);
basic_string& replace(size_type pos1, size_type n1, size_type n2,
charT c);
basic_string& replace(iterator i1, iterator i2, const basic_string& str);
basic_string& replace(iterator i1, iterator i2, const charT* s, size_type n);
basic_string& replace(iterator i1, iterator i2, const charT* s);
basic_string& replace(iterator i1, iterator i2, size_type n, charT c);
template<class InputIterator>
basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2);
size_type copy(charT* s, size_type n, size_type pos = 0) const;
void swap(basic_string<charT, traits, Allocator>& str);
const charT* c_str() const;
const charT* data() const;
allocator_type get_allocator() const;
size_type find(const basic_string& str, size_type pos = 0) const;
size_type find(const charT* s, size_type pos, size_type n) const;
size_type find(const charT* s, size_type pos = 0) const;
size_type find(charT c, size_type pos = 0) const;
size_type rfind(const basic_string& str, size_type pos = npos) const;
size_type rfind(const charT* s, size_type pos, size_type n) const;
size_type rfind(const charT* s, size_type pos = npos) const;
size_type rfind(charT c, size_type pos = npos) const;
size_type find_first_of(const basic_string& str, size_type pos = 0) const;
size_type find_first_of(const charT* s, size_type pos, size_type n) const;
size_type find_first_of(const charT* s, size_type pos = 0) const;
size_type find_first_of(charT c, size_type pos = 0) const;
size_type find_last_of(const basic_string& str, size_type pos = npos) const;
size_type find_last_of(const charT* s, size_type pos, size_type n) const;
size_type find_last_of(const charT* s, size_type pos = npos) const;
size_type find_last_of(charT c, size_type pos = npos) const;
size_type find_first_not_of(const basic_string& str, size_type pos = 0) const;
size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;
size_type find_first_not_of(const charT* s, size_type pos = 0) const;
size_type find_first_not_of(charT c, size_type pos = 0) const;
size_type find_last_not_of(const basic_string& str, size_type pos = npos) const;
size_type find_last_not_of(const charT* s, size_type pos, size_type n) const;
size_type find_last_not_of(const charT* s, size_type pos = npos) const;
size_type find_last_not_of(charT c, size_type pos = npos) const;
basic_string substr(size_type pos = 0, size_type n = npos) const;
int compare(const basic_string& str) const;
int compare(size_type pos1, size_type n1, const basic_string& str) const;
int compare(size_type pos1, size_type n1, const basic_string& str,
size_type pos2, size_type n2) const;
int compare(const charT* s) const;
int compare(size_type pos1, size_type n1, const charT* s,
size_type pos2, size_type n2 = npos) const;
};
traits_type †
文字特性
形式
typedef traits traits_type;
解説
第 2 テンプレート引数?の typedef 名?。この型は char_traits 構造体と同様の文字特性に対する要求?を満たす必要がある。
参照
→ char_traits
char_type †
文字様型
形式
typedef typename traits::char_type char_type;
解説
第 1 テンプレート引数?の typedef 名?。文字列の要素となる文字または文字に類する型。char_type は Allocator::value_type と等価でなければならない。
allocator_type †
アロケータ型
形式
typedef Allocator allocator_type;
解説
文字列を格納する記憶域の割り付けと解放を行うクラスの型。
size_type †
サイズ型
形式
typedef typename traits::size_type size_type;
解説
文字列の長さおよび要素の位置指定に用いる型。
difference_type †
差異型
形式
typedef typename traits::difference_type difference_type;
reference †
形式
typedef typename Allocator::reference reference;
解説
文字列の要素を参照する型。
const_reference †
形式
typedef typename Allocator::const_reference const_reference;
解説
文字列の要素を参照する型。この型による参照を用いて、要素を更新することはできない。
pointer †
形式
typedef typename Allocator::pointer pointer;
解説
文字の配列を指すポインタ型。
const_pointer †
形式
typedef typename Allocator::const_pointer const_pointer;
解説
文字の配列を指すポインタ型。このポインタ?を用いて文字を更新することはできない。
iterator †
形式
typedef 処理系定義の型 iterator;
解説
文字列の要素を指す、処理系定義のランダムアクセス反復子。
const_iterator †
形式
typedef 処理系定義の型 const_iterator;
解説
文字列の要素を指す、処理系定義のランダムアクセス反復子。この反復子を用いて、文字列を更新することはできない。
reverse_iterator †
逆進反復子?
形式
typedef std::reverse_iterator<iterator> reverse_iterator;
解説
文字列の要素を指す逆進反復子。この反復子もまた、ランダムアクセス反復子である。
const_reverse_iterator †
形式
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
解説
文字列の要素を指す逆進反復子。この反復子もまた、ランダムアクセス反復子である。
npos †
形式
static const size_type npos = -1;
コンストラクタ †
文字列の構築
形式
explicit basic_string(const Allocator& a = Allocator()); // 1
basic_string(const basic_string& str); // 2
basic_string(const basic_string& str, size_type pos,
size_type n = npos, const Allocator& a = Allocator()); // 3
basic_string(const charT* s,
size_type n, const Allocator& a = Allocator()); // 4
basic_string(const charT* s, const Allocator& a = Allocator()); // 5
basic_string(size_type n, charT c, const Allocator& a = Allocator()); // 6
template<class InputIterator>
basic_string(InputIterator begin, InputIterator end,
const Allocator& a = Allocator()); // 7
引数
a: アロケータ
str: 初期値にする文字列
s: 初期値にする文字配列?
c: 初期値にする文字様のオブジェクト
pos: str のコピー開始位置
n: 要素数
begin: 区間の先頭を指す反復子
end: 区間の終端の次を指す反復子
解説
basic_string クラスを構築する。
最初の形式では、空の文字列が構築される。この形式で構築した直後に data() を呼び出した場合、コピー可能な非空ポインタ?が返される。なお、capacity() の値は未規定。
2 番目の形式はコピーコンストラクタ?であり、str と同等の文字列が構築される。ただし、構築直後の capacity() の呼び出しでは、str.size() 以上の値を返し、必ずしも str.capacity() と同じ値にはならない。Allocator には str.get_allocator() が使用される。
3 番目の形式では、str の pos 番目から、n 個または str.size() - pos のうち小さい方の個数の要素が文字列の初期値となる。ここで、str.size() < pos の場合、out_of_range? 例外が送出される。
4 番目の形式では、s が指す配列の最初の n 要素が文字列の初期値となる。ここで、s は空ポインタ?であってはならない。また、n < npos でなければならない。
5 番目の形式では、s が指す配列の最初から traits::length(s) 個の要素が文字列の初期値となる。ここで、s は空ポインタ?であってはならない。
7 番目の形式では、InputIterator が汎整数型の場合、6 番目の形式と同等に振舞う。それ以外の場合、区間 [begin, end) が文字列の初期値となる。
デストラクタ †
文字列の解体
形式
~basic_string();
operator= †
代入演算子
形式
basic_string& operator=(const basic_string& str);
basic_string& operator=(const charT* s);
basic_string& operator=(charT c);
引数
str: 文字列
s: 配列の先頭要素へのポインタ
c: 文字様オブジェクト
返却値
*this を返す。
解説
引数の内容を代入する。
最初の形式では、str の内容がコピーされる。ただし、代入直後の capacity() の呼び出しでは、str.size() 以上の値を返し、必ずしも str.capacity() と同じ値にはならない。なお、str と *this が同じ場合は何も起こらない。
2 番目の形式では、basic_string(s) が代入される。
3 番目の形式では、basic_string(1, c) が代入される。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、代入演算子を呼び出した後は無効になる。
begin †
先頭文字を指す反復子
形式
iterator begin();
const_iterator begin() const;
注意
文字列の要素への参照?、ポインタ?、または反復子を無効にする操作 (反復子を返す insert および erase を除く) を行った後、最初に 1 番目の (定値性でない) 形式の begin を呼び出した場合も、文字列の要素への参照?、ポインタ?、および反復子は無効になる。
end †
末尾文字の次を指す反復子
形式
iterator end();
const_iterator end() const;
注意
文字列の要素への参照?、ポインタ?、または反復子を無効にする操作 (反復子を返す insert および erase を除く) を行った後、最初に 1 番目の (定値性でない) 形式の end を呼び出した場合も、文字列の要素への参照?、ポインタ?、および反復子は無効になる。
rbegin †
末尾文字を指す逆進反復子
形式
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
注意
文字列の要素への参照?、ポインタ?、または反復子を無効にする操作 (反復子を返す insert および erase を除く) を行った後、最初に 1 番目の (定値性でない) 形式の rbegin を呼び出した場合も、文字列の要素への参照?、ポインタ?、および反復子は無効になる。
rend †
先頭文字の前を指す逆進反復子
形式
reverse_iterator rend();
const_reverse_iterator rend() const;
返却値
文字列の先頭文字の前 (逆進反復子の進行方向では後) を指す逆進反復子を返す。
注意
文字列の要素への参照?、ポインタ?、または反復子を無効にする操作 (反復子を返す insert および erase を除く) を行った後、最初に 1 番目の (定値性でない) 形式の rend を呼び出した場合も、文字列の要素への参照?、ポインタ?、および反復子は無効になる。
size †
文字列が保持する要素数
形式
size_type size() const;
返却値
文字列がその時点で保持している要素数を返す。
参照
→ length, max_size, capacity
length †
文字列の長さ
形式
size_type length() const;
参照
→ size
max_size †
文字列の最大長
形式
size_type max_size() const;
返却値
文字列の最大長を返す。
resize †
保持する文字数の変更
形式
void resize(size_type n, charT c);
void resize(size_type n);
引数
n: 変更後の文字数
c: 追加される文字の値
capacity †
記憶域の容量
形式
size_type capacity() const;
返却値
保持している記憶域の容量を返す。
reserve †
記憶域の容量変更
形式
void reserve(size_type res_arg = 0) const;
引数
res_arg: 変更する容量
解説
保持している記憶域を変更する。res_arg が capacity() の値より大きい場合、保持している記憶域を res_arg 以上にする。res_arg が capacity() よ小さい場合は記憶域の縮小要求になる。また、res_arg <= size の場合は文字列を縮小する。なお、res_arg > max_size の場合、length_error? 例外が送出される。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、reserve メンバ関数を呼び出した後は無効になる。
clear †
文字列のクリア
形式
void clear();
解説
erase(begin(), end()) と同等である。
empty †
文字列が空かどうかの判別
形式
bool empty() const;
参考
size を使用して文字列が空かどうかを調べるより、empty を用いた方が効率がよい場合が多い。
operator[] †
添え字演算子
形式
const_reference oprator[](size_type pos) const;
reference oprator[](size_type pos);
引数
pos: 添え字
返却値
pos <= size() の場合、data(pos) を返す。pos == size() の場合、最初の形式 (const 版) は charT() を返す。それ以外の場合の動作は未定義である。
参照
→ at
at †
指定要素の参照
形式
const_reference at(size_type n) const;
reference at(size_type n);
引数
pos: 添え字
返却値
opreator[](pos) を返す。
解説
pos 番目の要素を返す。pos >= size() の場合、out_of_range? 例外が送出される。
operator+= †
加算代入演算子
形式
basic_string& operator+=(const basic_string& str);
basic_string& operator+=(const charT* s);
basic_string& operator+=(charT c);
引数
str: 文字列
s: 配列の先頭要素へのポインタ
c: 文字様オブジェクト
返却値
*this を返す。
解説
文字列の末尾に要素を追加する。
最初の形式は、append(str) と同等である。
2 番目の形式では、basic_string(s) が追加される。
3 番目の形式では、basic_string(1, c) が追加される。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、加算代入演算子を呼び出した後は無効になる。
append †
文字列への追加
形式
basic_string& append(const basic_string& str);
basic_string& append(const basic_string& str, size_type pos, size_type n);
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);
引数
str: 追加する文字列
s: 追加する文字配列?
c: 追加する文字様のオブジェクト
pos: str のコピー開始位置
n: 要素数
first: 区間の先頭を指す反復子
last: 区間の終端の次を指す反復子
返却値
*this を返す。
解説
文字列の末尾に要素を追加する。
最初と 2 番目の形式では、str の pos 番目から、 n または str.size() - pos のうち小さい方の個数の要素を追加する。最初の形式は append(str, 0, npos) と同等である。pos > str.size() の場合、out_of_range? 例外が送出される。ここで、追加後の文字列の長さが npos 以上になる場合は length_error? 例外が送出される。
3 番目の形式は、append(basic_string<charT, traits, Allocator>(s, n)) と同等である。
4 番目の形式は、append(basic_string<charT, traits, Allocator>(s)) と同等である。
5 番目の形式は、append(basic_string<charT, traits, Allocator>(n, c)) と同等である。
6 番目の形式は、append(basic_string<charT, traits, Allocator>(first, last)) と同等である。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、append を呼び出した後は無効になる。
push_back †
末尾への1 文字追加
形式
void push_back(const charT c);
引数
c: 追加する文字
解説
append(1, c) と同等。
assign †
文字列への代入
形式
basic_string& assign(const basic_string& str);
basic_string& assign(const basic_string& str, 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);
引数
str: 代入する文字列
s: 代入する文字配列?
c: 代入する文字様のオブジェクト
pos: str のコピー開始位置
n: 要素数
first: 区間の先頭を指す反復子
last: 区間の終端の次を指す反復子
返却値
*this を返す。
解説
文字列に代入する。
最初と 2 番目の形式では、str の pos 番目から、 n または str.size() - pos のうち小さい方の個数の要素を追加する。最初の形式は assign(str, 0, npos) と同等である。pos > str.size() の場合、out_of_range? 例外が送出される。
3 番目の形式は、assign(basic_string<charT, traits, Allocator>(s, n)) と同等である。
4 番目の形式は、assign(basic_string<charT, traits, Allocator>(s)) と同等である。
5 番目の形式は、assign(basic_string<charT, traits, Allocator>(n, c)) と同等である。
6 番目の形式は、assign(basic_string<charT, traits, Allocator>(first, last)) と同等である。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、assign を呼び出した後は無効になる。
insert †
文字列への挿入
形式
basic_string& insert(size_type pos1, const basic_string& str); // 1
basic_string& insert(size_type pos1, const basic_string& str
size_type pos2, size_type n); // 2
basic_string& insert(size_type pos, const charT* s, size_type n); // 3
basic_string& insert(size_type pos, const charT* s); // 4
basic_string& insert(size_type pos, size_type n, charT c); // 5
iterator insert(iterator p, charT c); // 6
void insert(iterator p, size_type n, charT c); // 7
template<class InputIterator>
void insert(iterator p, InputIterator first, InputIterator last); // 8
引数
str: 挿入する文字列
s: 挿入する文字配列?
c: 挿入する文字様のオブジェクト
pos: 挿入位置
pos1: 挿入位置
p: 挿入位置
pos2: str のコピー開始位置
n: 要素数
first: 区間の先頭を指す反復子
last: 区間の終端の次を指す反復子
返却値
1 番目から 5 番目の形式は *this を返す。6 番目の形式は挿入された文字の位置を指す反復子を返す。その他の形式に返却値はない。
解説
文字列の指定位置に要素を挿入する。
最初と 2 番目の形式では、文字列の pos1 番目の位置に、str の pos2 番目から、 n または size() - pos2 のうち小さい方の個数の要素を追加する。最初の形式は insert(pos1, str, 0, npos) と同等である。pos1 > size() または pos2 > str.size() の場合、out_of_range? 例外が送出される。ここで、挿入後の文字列の長さが npos 以上になる場合は length_error? 例外が送出される。
3 番目の形式は、insert(pos, basic_string<charT, traits, Allocator>(s, n)) と同等である。
4 番目の形式は、insert(pos, basic_string<charT, traits, Allocator>(s)) と同等である。
5 番目の形式は、insert(pos, basic_string<charT, traits, Allocator>(n, c)) と同等である。
6 番目の形式は、p が指す位置に c のコピーを挿入する。p は *this に対する有効な反復子でなければならない。
7 番目の形式は、p が指す位置に c のコピーを n 個挿入する。p は *this に対する有効な反復子でなければならない。
8 番目の形式は、insert(p - begin(), basic_string<charT, traits, Allocator>(first, last)) と同等である。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、insert を呼び出した後は無効になる。
erase †
文字列の部分削除
形式
basic_string& erase(size_type pos = 0, size_type n = npos);
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
引数
pos: 削除開始位置
n: 要素数
position: 削除位置
first: 区間の先頭を指す反復子
last: 区間の終端の次を指す反復子
返却値
最初の形式では *this を返す。2 番目の 3 番目の形式では、削除前に削除要素の直後にあった文字の削除後の位置を指す反復子を返す。
解説
文字列の指定要素を削除する。
最初の形式では、文字列の pos 番目の位置から、n または size() - pos のうち小さい方の個数の要素を削除する。pos > size() の場合は out_of_range? 例外が送出される。
2 番目の形式では、position が指す位置の文字を削除する。position は *this に対する有効な反復子でなければならない。
3 番目の形式では、区間 [first, last) が指す部分文字列を削除する。first および last は *this に対する有効な反復子かつ [first, last) は有効な区間でなければならない。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、erase を呼び出した後は無効になる。
replace †
文字列の部分置換
形式
basic_string& replace(size_type pos1, const basic_string& str); // 1
basic_string& replace(size_type pos1, size_type n1, const basic_string& str
size_type pos2, size_type n2); // 2
basic_string& replace(size_type pos1, size_type n1, const charT* s
size_type n2); // 3
basic_string& replace(size_type pos1, size_type n1, const charT* s); // 4
basic_string& replace(size_type pos1, size_type n1, size_type n2,
charT c); // 5
basic_string& replace(iterator i1, iterator i2, const basic_string& str); // 6
basic_string& replace(iterator i1, iterator i2, const charT* s, size_type n); // 7
basic_string& replace(iterator i1, iterator i2, const charT* s); // 8
basic_string& replace(iterator i1, iterator i2, size_type n, charT c); // 9
template<class InputIterator>
basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2); // 10
引数
str: コピーする文字列
s: コピーする文字配列?
c: コピーする文字様のオブジェクト
pos1: 置換位置
n1: 置換要素数
i1: 置換する区間の先頭を指す反復子
i2: 置換する区間の終端の次を指す反復子
pos2: str のコピー開始位置
n2: コピー要素数
n: コピー要素数
j1: コピーする区間の先頭を指す反復子
j2: コピーする区間の終端の次を指す反復子
返却値
*this を返す。
解説
文字列の指定位置に要素を置換する。
最初と 2 番目の形式では、文字列の pos1 番目から n1 個の要素を、str の pos2 番目から、 n2 または size() - pos2 のうち小さい方の個数の要素と置換する。最初の形式は replace(pos1, n1, str, 0, npos) と同等である。pos1 > size() または pos2 > str.size() の場合、out_of_range? 例外が送出される。ここで、置換後の文字列の長さが npos 以上になる場合は length_error? 例外が送出される。
3 番目の形式は、replace(pos1, n1, basic_string<charT, traits, Allocator>(s, n)) と同等である。
4 番目の形式は、replace(pos1, n1, basic_string<charT, traits, Allocator>(s)) と同等である。
5 番目の形式は、replace(pos1, n1, basic_string<charT, traits, Allocator>(n2, c)) と同等である。
6 番目の形式は、区間 [i1, i2) の部分文字列を str で置換する。i1 および i2 は *this に対する有効な反復子かつ [i1, i2) は有効な区間でなければならない。
7 番目の形式は、replace(i1, i2, basic_string<charT, traits, Allocator>(s, n)) と同等である。
8 番目の形式は、replace(i1, i2, basic_string<charT, traits, Allocator>(s)) と同等である。
9 番目の形式は、replace(i1, i2, basic_string<charT, traits, Allocator>(n, c)) と同等である。
10 番目の形式は、replace(i1, i2, basic_string<charT, traits, Allocator>(j1, j2)) と同等である。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、replace を呼び出した後は無効になる。
copy †
文字列のコピー
形式
size_type copy(charT* s, size_type n, size_type pos = 0) const;
引数
s: コピー先の配列
n: 要素数
pos: コピー開始位置
返却値
実際にコピーされた要素数を返す。
解説
pos 番目の位置から、n または size() - pos のうち小さい方の個数を、s が指す配列にコピーする。ここで、末尾のナル文字は追加されない。pos > size() の場合は out_of_range? 例外が送出される。また、s が指す配列は十分な大きさを持たなければならない。
swap †
文字列の交換
形式
void swap(basic_string<charT, traits, Allocator>& str);
引数
str: 交換対象の文字列
解説
*this が保持する文字列と 'str が指す文字列を交換する。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、文字列を swap メンバ関数を呼び出した場合、またはその実引数として指定した場合は無効になる。
計算量
一定時間
c_str †
ナル終端文字列の取得
形式
const charT* c_str() const;
解説
末尾に charT() で表されるナル文字が格納された、size() + 1 個の要素を持つ配列の先頭要素を指すポインタを取得する。得られた配列の最初の size() 個の要素は、*this 文字列と同じ内容である。c_str で得られた配列の内容は変更してはならない。また、定値性?でないメンバ関数を呼び出した場合、以前に c_str で取得していた配列の内容は無効になる。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、文字列を c_str メンバ関数の実引数として指定すると無効になる。
data †
文字列が保持しているデータ列の参照
形式
const charT* data() const;
返却値
size() > 0 の場合、文字列と同内容の配列を指すポインタ?を返す。そうでなければ、コピー可能なな非空ポインタ?を返す。
解説
size() 個の要素を持つ配列の先頭要素を指すポインタを取得する。得られた配列は、*this 文字列と同じ内容である。Ddata で得られた配列の内容は変更してはならない。また、定値性?でないメンバ関数を呼び出した場合、以前に data で取得していた配列の内容は無効になる。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、文字列を data メンバ関数の実引数として指定すると無効になる。
get_allocator †
Allocator オブジェクトの取得
形式
allocator_type get_allocator() const;
返却値
文字列の構築に使用される Allocator オブジェクト?のコピーを返す。
find †
文字列内の探索
形式
size_type find(const basic_string& str, size_type pos = 0) const;
size_type find(const charT* s, size_type pos, size_type n) const;
size_type find(const charT* s, size_type pos = 0) const;
size_type find(charT c, size_type pos = 0) const;
rfind †
文字列内の逆方向探索
形式
size_type rfind(const basic_string& str, size_type pos = npos) const;
size_type rfind(const charT* s, size_type pos, size_type n) const;
size_type rfind(const charT* s, size_type pos = npos) const;
size_type rfind(charT c, size_type pos = npos) const;
find_first_of †
条件に一致する最初の位置の探索
形式
size_type find_first_of(const basic_string& str, size_type pos = 0) const;
size_type find_first_of(const charT* s, size_type pos, size_type n) const;
size_type find_first_of(const charT* s, size_type pos = 0) const;
size_type find_first_of(charT c, size_type pos = 0) const;
find_last_of †
条件に一致する最後の位置の探索
形式
size_type find_last_of(const basic_string& str, size_type pos = npos) const;
size_type find_last_of(const charT* s, size_type pos, size_type n) const;
size_type find_last_of(const charT* s, size_type pos = npos) const;
size_type find_last_of(charT c, size_type pos = npos) const;
find_first_not_of †
条件に一致しない最初の位置の探索
形式
size_type find_first_not_of(const basic_string& str, size_type pos = 0) const;
size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;
size_type find_first_not_of(const charT* s, size_type pos = 0) const;
size_type find_first_not_of(charT c, size_type pos = 0) const;
find_last_not_of †
条件に一致しない最後の位置の探索
形式
size_type find_last_not_of(const basic_string& str, size_type pos = npos) const;
size_type find_last_not_of(const charT* s, size_type pos, size_type n) const;
size_type find_last_not_of(const charT* s, size_type pos = npos) const;
size_type find_last_not_of(charT c, size_type pos = npos) const;
substr †
部分文字列
形式
basic_string substr(size_type pos = 0, size_type n = npos) const;
compare †
文字列の比較
形式
int compare(const basic_string& str) const;
int compare(size_type pos1, size_type n1, const basic_string& str) const;
int compare(size_type pos1, size_type n1, const basic_string& str,
size_type pos2, size_type n2) const;
int compare(const charT* s) const;
int compare(size_type pos1, size_type n1, const charT* s,
size_type pos2, size_type n2 = npos) const;
参照
→ operator==, operator!=, operator<, operator>, operator<=, operator>=, strcmp, strncmp, wcscmp, wcsncmp
演算子 †
operator+ †
文字列の連結
形式
template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
operator+(const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
operator+(const charT* lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
operator+(charT lhs, const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
operator+(const basic_string<charT, traits, Allocator>& lhs,
const charT* rhs);
template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
引数
lhs: 左辺
rhs: 右辺
返却値
連結結果の文字列を返す。
解説
lhs と rhs を連結する。具体的には、lhs が "abcd"、rhs が "efg" の場合、結果は "abcdefg" となる。
operator== †
文字列の等価判定
形式
template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator==(const char* lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT, traits, Allocator>& lhs,
const charT* rhs);
引数
lhs: 左辺
rhs: 右辺
返却値
両辺の文字列が等しい場合は true を、それ以外は false を返す。
解説
文字列の等価判定を行う。具体的には、lhs.compare == 0 が成立するかどうかを判定する。lhs が basic_string クラスでない場合、いったん lhs を basic_string クラスに変換した上で compare メンバ関数を呼び出す。
参照
→ 等価演算子?, operator!=
operator!= †
文字列の非等価判定
形式
template<class charT, class traits, class Allocator>
bool operator!=(const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator!=(const char* lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator!=(const basic_string<charT, traits, Allocator>& lhs,
const charT* rhs);
引数
lhs: 左辺
rhs: 右辺
返却値
両辺の文字列が等しくない場合は true を、それ以外は false を返す。
解説
文字列の等価判定を行う。具体的には、lhs.compare != 0 が成立するかどうかを判定する。lhs が basic_string クラスでない場合、いったん lhs を basic_string クラスに変換した上で compare メンバ関数を呼び出す。
参照
→ 等価演算子?, operator==
operator< †
文字列の小なり判定
形式
template<class charT, class traits, class Allocator>
bool operator< (const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator< (const charT* lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator< (const basic_string<charT, traits, Allocator>& lhs,
const charT* rhs);
引数
lhs: 左辺
rhs: 右辺
返却値
左辺が右辺より小さい場合は true を、それ以外は false を返す。
解説
左辺の文字列が、辞書順比較において、右辺より小さいかどうかを判定する。具体的には、lhs.compare < 0 が成立するかどうかを判定する。lhs が basic_string クラスでない場合、いったん lhs を basic_string クラスに変換した上で compare メンバ関数を呼び出す。
参照
→ 関係演算子?, operator>?, operator<=?
operator> †
文字列の大なり判定
形式
template<class charT, class traits, class Allocator>
bool operator> (const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator> (const charT* lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator> (const basic_string<charT, traits, Allocator>& lhs,
const charT* rhs);
引数
lhs: 左辺
rhs: 右辺
返却値
左辺が右辺より大きい場合は true を、それ以外は false を返す。
解説
左辺の文字列が、辞書順比較において、右辺より大きいかどうかを判定する。具体的には、lhs.compare > 0 が成立するかどうかを判定する。lhs が basic_string クラスでない場合、いったん lhs を basic_string クラスに変換した上で compare メンバ関数を呼び出す。
参照
→ 関係演算子?, operator<?, operator>=?
operator<= †
文字列の以下なり判定
形式
template<class charT, class traits, class Allocator>
bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator<=(const charT* lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
const charT* rhs);
template<class charT, class traits, class Allocator>
引数
lhs: 左辺
rhs: 右辺
返却値
左辺が右辺より小さいか等しい場合は true を、それ以外は false を返す。
解説
左辺の文字列が、辞書順比較において、右辺以下かどうかを判定する。具体的には、lhs.compare <= 0 が成立するかどうかを判定する。lhs が basic_string クラスでない場合、いったん lhs を basic_string クラスに変換した上で compare メンバ関数を呼び出す。
参照
→ 関係演算子?, operator>=?, operator<?
operator>= †
文字列の以上なり判定
形式
bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator>=(const charT* lhs,
const basic_string<charT, traits, Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
const charT* rhs);
引数
lhs: 左辺
rhs: 右辺
返却値
左辺が右辺より大きいか等しい場合は true を、それ以外は false を返す。
解説
左辺の文字列が、辞書順比較において、右辺以上かどうかを判定する。具体的には、lhs.compare >= 0 が成立するかどうかを判定する。lhs が basic_string クラスでない場合、いったん lhs を basic_string クラスに変換した上で compare メンバ関数を呼び出す。
参照
→ 関係演算子?, operator<=?, operator>?
operator>> †
入力ストリーム?からの文字列抽出
形式
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
basic_string<charT, traits, Allocator>& str);
引数
is: 入力ストリーム?
str: 抽出した文字列の格納先
返却値
is を返す。
解説
次に示す手順で、入力ストリームから文字列を抽出する。
- sentry? クラスのオブジェクト k を次の形式で構築する。
typename basic_istream<charT, traits>::sentry k(is);
- bool(k) が真の場合、str.erase() によって元の文字列がクリアされた後、is から文字が抽出され、その文字が str.append(1, 文字) によって追加される。
- is.width() が 0 より大きい場合、追加される最大の文字数 n は is.width() となる。それ以外の場合、n は str.max_size() となる。次の条件のいずれかが成立するまで、文字の抽出と追加が繰り返される。
- 最後の文字が読み込まれた後、is.width(0) が呼び出され、sentry クラスのオブジェクト k が解体される。
1 文字も読み込まれなかった場合、is.setstate?(ios_base?::failbit?) が呼び出される。これにより、ios_base::failure? が送出される場合がある。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、文字列を >> 演算子の演算対象?として指定すると無効になる。
参照
→ operator<<
operator<< †
出力ストリーム?への文字列挿入
形式
template<class charT, class traits, class Allocator>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
const basic_string<charT, traits, Allocator>& str);
返却値
os を返す。
解説
次に示す手順で、出力ストリーム?に文字列を挿入する。
- sentry? クラスのオブジェクト k を次の形式で構築する。
typename basic_istream<charT, traits>::sentry k(os);
- bool(k) が真の場合、os.rdbuf()->sputn(str.data(), n) を呼び出した場合と等価の方法で、文字列が挿入される。ただし、n は os.width() または str.size() のうち大きい方の値である。
- 文字が挿入された後、os.width(0) が呼び出される。
- sputn の呼び出しに失敗した場合、is.setstate?(ios_base?::failbit?) が呼び出される。
- sentry クラスのオブジェクト k が解体される。
参照
→ operator>>
関数 †
swap †
文字列の交換
形式
template<class charT, class traits, class Allocator>
void swap(basic_string<charT, traits, Allocator>& lhs,
basic_string<charT, traits, Allocator>& rhs);
引数
lhs: 左辺
rhs: 右辺
返却値
なし
解説
両辺の文字列の値を交換する。lhs.swap(rhs) の呼び出しと等価である。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、文字列を swap 関数の実引数として指定すると無効になる。
参照
→ swap (メンバ関数), swap (<algorithm>)
getline †
形式
入力ストリーム?からの 1 行抽出
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is,
basic_string<charT, traits, Allocator>& str,
charT delim);
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is,
basic_string<charT, traits, Allocator>& str);
引数
is: 入力ストリーム?
str: 抽出した文字列の格納先
delim: 区切り文字
返却値
is を返す。
解説
次に示す手順で、入力ストリームから文字列を抽出する。
- sentry? クラスのオブジェクト k を次の形式で構築する。
typename basic_istream<charT, traits>::sentry k(is, true);
- bool(k) が真の場合、str.erase() によって元の文字列がクリアされた後、is から文字が抽出され、その文字が str.append(1, 文字) によって追加される。
- 次の条件のいずれかが成立するまで、文字の抽出と追加が繰り返される。
- 最後の文字が読み込まれた後、sentry クラスのオブジェクト k が解体される。
1 文字も読み込まれなかった場合、is.setstate(ios_base::failbit) が呼び出される。これにより、ios_base::failure が送出される場合がある。
2 番目の形式では、delim に改行文字、すなわち is.widen('\n') が指定された場合と等価である。
注意
文字列の要素に対する参照?、ポインタ?、および反復子は、文字列を getline 関数の実引数として指定すると無効になる。
参照
→ operator>>
型 †
namespace std {
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
}
Last-modified: Wed, 07 Dec 2005 20:25:13 JST (5558d)