トップ->C++入門

あなたは

人目のC++(C)言語入門受講生です。

C++入門内検索

目次
C++入門〜トップ
C言語入門〜トップ
0. はじめに

1. オブジェクト指向とは?
   1. オブジェクト指向とクラス
   2. 継承
   3. カプセル化
   4. ポリモーフィズム

2. ストリーム
   1. 出力
   2. マニピュレータ
   3. 入力
   4. ファイル
   5. 練習問題1
   6. 文字列
   7. 練習問題2

3. C++の新しい文法
   1. 新しい型bool
   2. デフォルト引数
   3. newとdelete
   4. 参照型
   5. const
   6. 変数の宣言
   7. 例外
   8. オーバーロード
   9. テンプレート関数
   10. 名前空間

4. クラス
   1. クラスとは
   2. クラスの宣言
   3. クラスの実装
   4. コンストラクタとデストラクタ
   5. クラスの使用法
   6. 例題)スタッククラス
   7. テンプレートクラス
   8. 練習問題
   9. 参照型
   10. 代入演算子
   11. コピーコンストラクタ
   12. 構造体
   13. メンバー変数の初期化
   14. 内部クラス
   15. 無名クラス
   16. 無名共用体
   17. 演算子の作り方
   18. friend
   19. 練習問題
   20. クラス変数(静的変数)
   21. 静的関数
   22. クラスと関数ポインタ

5. クラスの包含
   1. 包含とは
   2. クラスの作成・破壊
   3. メンバーイニシャライザ
   4. ポインタによる包含
   5. 参照による包含
   6. 練習問題

6. 継承
   1. 継承とは
   2. スーパークラスのコンストラクタ
   3. 継承とキャスト
   4. スコープ
   5. クラスの作成・破壊
   6. 派生の種類
   7. 仮装関数
   8. 純粋仮装関数
   9. 仮装デストラクタ
   10. 例題)例外クラス
   11. V-table(VF-table)
   12. 例題)お絵かきソフト
   13. 継承と包含
   14. 多重継承
   15. 多重継承の用途
   16. 仮想クラス
   17. 実行時型情報(RTTI)
   18. dynamic_cast

7. STL
   1. STLとは
   2. STLの歴史
   3. STLの構成
   4. コンテナ
   5. vector
   6. イタレーター
   7. クラスとSTL
   8. list
   9. queue
   10. deque
   11. priority_queue
   12. stack
   13. map
   14. mutimap
   15. set
   16. multiset
   17. bitset
   18. アルゴリズム
   19. basic_string
   20. コンテナを作ろう
   21. アルゴリズムを作ろう
   22. 配列とアルゴリズム

8. その他
   1. 変数名について

9. その後は
   1. ヒューマンアカデミー C言語講座
   2. el school C言語講座


・ トップページに戻る



・ トップページに戻る

トップ-> C++入門:7章 STL-> basic_string

←前ページへ :  トップへ :  次ページへ→

  stringクラスにはさまざまなメンバー関数が用意されています。たくさんありますが、 簡単に言いますと、イタレーター関係、文字数を調べる関数、文字列や文字の代入、追加、 挿入を行う関数、比較関数、置換や検索を行う関数などがあります。

stringクラスの主要な関数
関数名 書 式 説 明
begin iterator begin() 1文字目を指すイタレーターを返します。
end iterator end() 末尾を指すイタレーターを返します。
rbegin reverse_iterator rbegin() 末端を指す逆方向イタレーター(逆シーケンスの先頭を指すイタレーター)を返します。
rend reverse_iterator rend(>) 先頭を指す逆方向イタレーター(逆シーケンスの末尾を指すイタレーター)を返します。
size size_type size() const 文字数を返します。(lengthと同じです)
length size_type length() const 文字数を返します。(sizeと同じです)
empty bool empty() const バッファーサイズが0の時にtrueを返します。(文字数ではありません。)
at reference at(size_type pos) pos番目の文字の参照を返します。posが無効な場合は例外(out_of_range)を投げます。
append string& append(const char* s) NULLで終わる文字列sを追加し、自分自身の参照を返します。
append string& append(const char* s, size_type n) NULLで終わる文字列sの最初からn文字分を追加し、自分自身の参照を返します。
append string& append(const string& str) strを追加し、自分自身の参照を返します。
append string& append(const string& str, size_type pos, size_type n) strpos文字目からn文字分を追加し、自分自身の参照を返します。
append string& append(size_type n, char c) 1文字cn個追加し、自分自身の参照を返します。
append string& append(const_iterator first, const_iterator last) firstからlastの文字を追加し、自分自身の参照を返します。
insert basic_string& insert(size_t pos, const char* s) pos番めの文字の前に、NULLで終わる文字列sを挿入し、自分自身を返します。
insert basic_string& insert(size_t pos, const char* s, syze_type n) pos番めの文字の前に、sの最初からn文字分を挿入し、自分自身を返します。
insert basic_string& insert(size_t pos, const basic& str) pos番めの文字の前に、strを挿入し、自分自身を返します。
insert basic_string& insert(size_t pos, const basic& str, size_t n) pos番めの文字の前に、strの最初からn文字分を挿入し、自分自身を返します。
insert basic_string& insert(size_t pos, size_t n, char c) pos番めの文字の前に、文字cn個分挿入し、自分自身を返します。
insert iterator insert(iterator itr, char c) itrが指す文字の前に、文字cを挿入し、挿入した文字を指すイタレーターを返します。
insert void insert(iterator itr, size_type n, char c) itrが指す文字の前に、文字cn個分挿入します。
insert void insert(iterator itr, const_iterator first, const_iterator last) itrが指す文字の前に、firstからlastで表される文字列を挿入します。
erase iterator erase(iterator& itr) itrが指す文字を削除します。戻り値は削除した部分の次の文字を指すイタレーターを返します。
erase iterator erase(iterator& first, iterator& last) firstからlastまでの文字列を削除します。戻り値は削除した部分の次の文字を指すイタレーターを返します。
erase string& erase(size_type& pos = 0, size_type& n = npos) pos文字目からn文字分の部分文字列を削除し、自分自身を返します。
assign basic_string& assign(const char* s) NULLで終わる文字列sを自分自身に代入し、自分自身を返します。
assign basic_string& assign(const char* s, size_type n) NULLで終わる文字列sの最初からn文字分を自分自身に代入し、自分自身を返します。
assign basic_string& assign(const string& str) strを自分自身に代入し、自分自身を返します。
assign basic_string& assign(const string& str, size_type pos, size_type n) strpos文字目からn文字分の文字列を自分自身に代入し、自分自身を返します。
assign basic_string& assign(size_type n, char c) 文字cn個分を自分自身に代入し、自分自身を返します。
assign basic_string& assign(const_iterator first, const_iterator last) firstからlastで表される文字列を自分自身に代入し、自分自身を返します。
swap void swap(string& str) 自分自身と、strの内容を交換します。
substr string substr(size_tyoe pos = 0, size_type n = npos) const pos文字目からn文字分の部分文字列を作り、それを返します。
replace string& replace(size_type pos, size_type n, const char* s) pos文字目からn文字分の部分文字列を、NULLで終わる文字列sに置換します。
replace string& replace(size_type pos, size_type n1, const char* s, size_type n2) pos文字目からn1文字分の部分文字列を、NULLで終わる文字列sの最初からn2文字分の部分文字列に置換します。
replace string& replace(size_type pos, size_type n, const string& str) pos文字目からn文字分の部分文字列を、文字列strに置換します。
replace string& replace(size_type pos1, size_type n1, const string& str, size_type pos2, size_type n2) pos1文字目からn1文字分の部分文字列を、文字列strpos文字目からn2文字分の部分文字列に置換します。
replace string& replace(size_type pos1, size_type n1, size_type n2, char c) pos1文字目からn1文字分の部分文字列を、文字cn1個から形成される文字列に置換します。
replace string& replace(iterator first, iterator last, const char* s) firstからlastの部分文字列を、NULLで終わる文字列sに置換します。
replace string& replace(iterator first, iterator last, const char* s, size_type n) firstからlastの部分文字列を、NULLで終わる文字列sの最初からn文字分の部分文字列に置換します。
replace string& replace(iterator first, iterator last, const string& str) firstからlastの部分文字列を、文字列strに置換します。
replace string& replace(iterator first, iterator last, size_type n, char c) firstからlastの部分文字列を、文字cn個から形成される文字列に置換します。
replace string& replace(iterator first, iterator last, const_iterator first2, const_iterator last2) first1からlast1の部分文字列を、first2からlast2の部分文字列に置換します。
compare int compare(const basic_string& str) const strと辞書順で比較し、結果を返します。
strcmp(this->c_str(), str.c_str())とほぼ同様です。
compare int compare(size_type pos, size_type n, const string& str) const pos文字目からn文字分の部分文字列とstrを辞書順で比較し、結果を返します。
compare int compare(size_type pos1, size_type n1, const string& str, size_type pos2, size_type n2) const pos1文字目からn1文字分の部分文字列とstrpos2文字目からn2文字分の部分文字列を辞書順で比較し、結果を返します。
compare int compare(const char* s) const NULLで終わる文字列sと辞書順で比較し、結果を返します。
strcmp(this->c_str(), s)とほぼ同様です。
compare int compare(size_type pos, size_type n, const char* s) const pos文字目から、n文字分の部分文字列と、NULLで終わる文字列sを辞書順で比較し、結果を返します。
compare int compare(size_type pos1, size_type n1, const char* s, size_type pos2) const pos1文字目から、n1文字分の部分文字列と、NULLで終わる文字列spos2文字目から始まる部分文字列を辞書順で比較し、結果を返します。
find size_type find(char c, size_type pos = 0) const pos文字目から、文字cを検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find size_type find(const char* s, size_type pos = 0) const pos文字目から、文字列sを検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find size_type find(const char* s, size_type pos, size_type n) const pos文字目から、文字列sn文字目までの部分文字列を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find size_type find(const string& str, size_type pos = 0) const pos文字目から、文字列strを検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_first_of size_type find_first_of(char c, size_type pos = 0) const pos文字目から、文字cを検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_first_of size_type find_first_of(const char* s, size_type pos = 0) const pos文字目から、文字列s内に含まれる文字を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_first_of size_type find_first_of(const char* s, size_type pos, size_type n) const pos文字目から、文字列sn文字目までの部分文字列内に含まれる文字を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_first_of size_type find_first_of(const string& str, size_type pos = 0) const pos文字目から、文字列str内に含まれる文字を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_first_not_of size_type find_first_not_of(char c, size_type pos = 0) const pos文字目から、文字cではない文字を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_first_not_of size_type find_first_not_of(const char* s, size_type pos = 0) const pos文字目から、文字列s内に含まれない文字を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_first_not_of size_type find_first_not_of(const char* s, size_type pos, size_type n) const pos文字目から、文字列sn文字目までの部分文字列内に含まれない文字を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_first_not_of size_type find_first_not_of(const string& str, size_type pos = 0) const pos文字目から、文字列str内に含まれない文字を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
rfind size_type rfind(char c, size_type pos = npos) const pos文字目から、文字cを逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
rfind size_type rfind(const char* s, size_type pos = npos) const pos文字目から、文字列sを逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
rfind size_type rfind(const char* s, size_type pos, size_type n = npos) const pos文字目から、文字列sn文字目までの部分文字列を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
rfind size_type rfind(const string& str, size_type pos = npos) const pos文字目から、文字列strを逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_last_of size_type find_last_of(char c, size_type pos = npos) const pos文字目から、文字cを逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_last_of size_type find_last_of(const char* s, size_type pos = npos) const pos文字目から、文字列s内に含まれる文字を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_last_of size_type find_last_of(const char* s, size_type pos, size_type n = npos) const pos文字目から、文字列sn文字目までの部分文字列内に含まれる文字を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_last_of size_type find_last_of(const string& str, size_type pos = npos) const pos文字目から、文字列str内に含まれる文字を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_last_not_of size_type find_last_not_of(char c, size_type pos = npos) const pos文字目から、文字cではない文字を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_last_not_of size_type find_last_not_of(const char* s, size_type pos = npos) const pos文字目から、文字列s内に含まれない文字を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_last_not_of size_type find_last_not_of(const char* s, size_type pos, size_type n = npos) const pos文字目から、文字列sn文字目までの部分文字列内に含まれない文字を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
find_last_not_of size_type find_last_not_of(const string& str, size_type pos = npos) const pos文字目から、文字列str内に含まれない文字を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数nposを返します。
c_str const char* c_str() const コンテナの最後がNULL文字でなければ、NULL文字を付加して文字列の最初の文字を表すポインタを返します。C言語で定義される文字列へのキャストに使います。
data const char* data() const 文字列の最初の文字を表すポインタを返します。
max_size size_type max_size() const 生成できる文字列の最大文字数を返します。
operator= basic_string& operator=(char& c) 自分自身に文字cを代入します。
operator= basic_string& operator=(const char* s) 自分自身に文字列sを代入します。
operator= basic_string& operator=(const string& str) 自分自身に文字列strを代入します。
operator+= basic_string& operator+=(char& c) 自分自身の最後に文字cを追加します。
operator+= basic_string& operator+=(const char* s) 自分自身に最後に文字列sを追加します。
operator+= basic_string& operator+=(const string& str) 自分自身に最後に文字列strを追加します。
operator[] char& operator[](size_type pos) pos番目の文字の参照を返します。
posが無効な場合の動作は未定義です。
※ここに紹介した関数以外のメンバー関数を使うと、size関数、length関数は正しく文字数を 返さなくなります。

  max_size()関数で表される長さよりも長い文字を作ろうとした場合、例外(length_error)が 投げられます。

  上記の関数内で、posなどの範囲が無効な場合、例外(out_of_range)が投げられます。


←前ページへ :  トップへ :  次ページへ→