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) |
strのpos文字目からn文字分を追加し、自分自身の参照を返します。 |
append |
string& append(size_type n, char c) |
1文字cをn個追加し、自分自身の参照を返します。 |
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番めの文字の前に、文字cをn個分挿入し、自分自身を返します。 |
insert |
iterator insert(iterator itr, char c) |
itrが指す文字の前に、文字cを挿入し、挿入した文字を指すイタレーターを返します。 |
insert |
void insert(iterator itr, size_type n, char c) |
itrが指す文字の前に、文字cをn個分挿入します。 |
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) |
strのpos文字目からn文字分の文字列を自分自身に代入し、自分自身を返します。 |
assign |
basic_string& assign(size_type n, char c) |
文字cをn個分を自分自身に代入し、自分自身を返します。 |
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文字分の部分文字列を、文字列strのpos文字目からn2文字分の部分文字列に置換します。 |
replace |
string& replace(size_type pos1, size_type n1, size_type n2, char c) |
pos1文字目からn1文字分の部分文字列を、文字cがn1個から形成される文字列に置換します。 |
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の部分文字列を、文字cがn個から形成される文字列に置換します。 |
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文字分の部分文字列とstrのpos2文字目から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で終わる文字列sのpos2文字目から始まる部分文字列を辞書順で比較し、結果を返します。 |
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文字目から、文字列sのn文字目までの部分文字列を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数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文字目から、文字列sのn文字目までの部分文字列内に含まれる文字を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数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文字目から、文字列sのn文字目までの部分文字列内に含まれない文字を検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数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文字目から、文字列sのn文字目までの部分文字列を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数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文字目から、文字列sのn文字目までの部分文字列内に含まれる文字を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数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文字目から、文字列sのn文字目までの部分文字列内に含まれない文字を逆順に検索し、最初に見つかった文字のインデックスを返します。見つからない場合はメンバー変数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関数は正しく文字数を
返さなくなります。