| cppreference.com -> C++文字列 -> 詳細 |
string(); string( size_type 長さ, char 文字 ); string( const char *文字列 ); string( const char *文字列, size_type 長さ ); string( string &文字列, size_type インデックス, size_type 長さ ); string( input_iterator コピー元の最初のイテレータ, input_iterator コピー元の最後のイテレータ ); |
新しい文字列を以下のような様々な方法で構築する:
string str1( 5, 'c' );
string str2( "Now is the time..." );
string str3( str2, 11, 4 );
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
表示:
ccccc
Now is the time...
time
== > < >= <= != + += [] |
==, >, <, >=, <=, !=の各演算子を使用して文字列の比較を行うことができる。また、+演算子や+=演算子を用いて文字列を結合することができる。 []演算子を使用して特定の箇所の文字を参照することができる。
関連トピックbasic_string &append( const basic_string &文字列 ); basic_string &append( const char *文字列 ); basic_string &append( const basic_string &文字列, size_type インデックス, size_type 長さ ); basic_string &append( const char *文字列, size_type 文字数 ); basic_string &append( size_type 文字数, char 文字 ); basic_string &append( input_iterator コピー元の最初のイテレータ, input_iterator コピー元の最後のイテレータ ); |
関数は以下のような様々な方法で文字列を追加する:
string str = "Hello World";
str.append( 10, '!' );
cout << str << endl;
表示
Hello World!!!!!!!!!!
関連トピックbasic_string &assign( const basic_string &文字列 ); basic_string &assign( const char *文字列 ); basic_string &assign( const char *文字列, size_type 文字数 ); basic_string &assign( const basic_string &文字列, size_type インデックス, size_type 長さ ); basic_string &assign( size_type 文字数, char 文字 ); |
この関数は以下のような様々な方法で文字列を割り当てる:
string str1, str2 = "War and Peace";
str1.assign( str2, 4, 3 );
cout << str1 << endl;
表示:
and
reference at( size_type インデックス ); |
at()関数はインデックス で指定された文字への参照を返す。 もしインデックス が文字列の範囲を超えていた場合にはout_of_rangeクラスのオブジェクトを範囲外アクセスのエラーを例外として報告する。
string text = "ABCDEF";
char ch = text.at( 2 );
文字'C'が得られる
関連トピックiterator begin(); |
begin()関数は現在の文字列の最初の要素を指すイテレータを返す。
関連トピックconst char *c_str(); |
c_str()関数は、現在の文字列を指す、C言語方式の文字列のポインタを返す。
関連トピックsize_type capacity(); |
capacity()関数は現在の文字列がメモリ割り当てを追加で行わなくても格納可能な文字のサイズを返す。この数値はsize()の返値よりも大きいことが保証されている。
関連トピックint compare( const basic_string &文字列 ); int compare( const char *文字列 ); int compare( size_type インデックス, size_type 長さ, const basic_string &文字列 ); int compare( size_type インデックス, size_type 長さ, const basic_string &文字列, size_type インデックス2, size_type 長さ2 ); int compare( size_type インデックス, size_type 長さ, const char *文字列, size_type 長さ2 ); |
compare()関数は文字列 との比較で様々な結果を返す。
| 返値 | 場合 |
|---|---|
| 負 | 自分 < 比較対象 |
| ゼロ | 自分 == 比較対象 |
| 正 | 自分 > 比較対象 |
また、この関数は以下のようなさまざまな形式での比較を行う。
size_type copy( char *文字列, size_type 文字数, size_type インデックス ); |
copy()関数は現在の文字列のインデックス で始まる文字数 分の文字を文字列 にコピーする。返値はコピーした文字数である。
const char *data(); |
data()関数は現在の文字列の最初の文字を示すポインタを返す。
関連トピックbool empty(); |
empty()関数は文字列が空の時にtrue を返す。そうでない場合にはfalse を返す。
iterator end(); |
end()関数は現在の文字列の最後を指すイテレータを返す。
関連トピックiterator erase( iterator 位置 ); iterator erase( iterator 消去開始位置, iterator 消去終了位置 ); basic_string &erase( size_type インデックス = 0, size_type 文字数 = 最後 ); |
erase()関数は以下のような動作をする:
string s("So, you like donuts, eh? Well, have all the donuts in the world!");
cout << "オリジナルの文字列は '" << s << "'" << endl;
s.erase( 50, 14 );
cout << "現在の状態は '" << s << "'" << endl;
s.erase( 24 );
cout << "現在の状態は '" << s << "'" << endl;
s.erase();
cout << "現在の状態は '" << s << "'" << endl;
このように表示される
オリジナルの文字列は 'So, you like donuts, eh? Well, have all the donuts in the world!'
現在の状態は 'So, you like donuts, eh? Well, have all the donuts'
現在の状態は 'So, you like donuts, eh?'
現在の状態は ''
size_type find( const basic_string &文字列, size_type インデックス ); size_type find( const char *文字列, size_type インデックス ); size_type find( const char *文字列, size_type インデックス, size_type 長さ ); size_type find( char 文字, size_type インデックス ); |
find()関数は以下のような様々な方法で検索を行う:
string str1( "Alpha Beta Gamma Delta" );
unsigned int loc = str1.find( "Omega", 0 );
if( loc != string::npos )
cout << "Omegaの位置は " << loc << endl;
else
cout << "Omegaは見つかりませんでした" << endl;
size_type find_first_of( const basic_string &文字列, size_type インデックス = 0 ); size_type find_first_of( const char *文字列, size_type インデックス = 0 ); size_type find_first_of( const char *文字列, size_type インデックス, size_type 文字数 ); size_type find_first_of( char 文字, size_type インデックス = 0 ); |
find_first_of()関数は以下の動作を行う。
size_type find_first_not_of( const basic_string &文字列, size_type インデックス = 0 ); size_type find_first_not_of( const char *文字列, size_type インデックス = 0 ); size_type find_first_not_of( const char *文字列, size_type インデックス, size_type 文字数 ); size_type find_first_not_of( char 文字, size_type インデックス = 0 ); |
find_first_not_of()は以下のような検索を行う:
size_type find_last_of( const basic_string &文字列, size_type インデックス = npos ); size_type find_last_of( const char *文字列, size_type インデックス = npos ); size_type find_last_of( const char *文字列, size_type インデックス, size_type 文字数 ); size_type find_last_of( char 文字, size_type インデックス = npos ); |
find_last_of()関数は以下のような検索を行う:
size_type find_last_not_of( const basic_string &文字列, size_type インデックス = npos ); size_type find_last_not_of( const char *文字列, size_type インデックス = npos); size_type find_last_not_of( const char *文字列, size_type インデックス, size_type 文字数 ); size_type find_last_not_of( char 文字, size_type インデックス = npos ); |
find_last_not_of() 関数は以下のような検索を行う:
allocator_type get_allocator(); |
get_allocator()関数は現在の文字列のアロケータを返す
iterator insert( iterator 挿入位置, const char &文字 ); basic_string &insert( size_type インデックス, const basic_string &文字列 ); basic_string &insert( size_type インデックス, const char *文字列 ); basic_string &insert( size_type インデックス1, const basic_string &文字列, size_type インデックス2, size_type 文字数 ); basic_string &insert( size_type インデックス, const char *文字列, size_type 文字数 ); basic_string &insert( size_type インデックス, size_type 文字数, char 文字 ); void insert( iterator 挿入位置, size_type 文字数, const char &ch ); void insert( iterator 挿入位置, iterator コピー元の最初, iterator コピー元の最後 ); |
insert()は以下のようなたくさんの目的の操作を行う:
size_type length(); |
length() 関数は現在の文字列の長さを返す。この関数の返値はsize()と同じである。
関連トピックsize_type max_size(); |
max_size()関数は文字列が保持することが可能な最大の文字数を返す。
const reverse_iterator rbegin(); |
rbegin()関数は文字列の最後を指すリバースイテレータを返す。
関連トピックconst reverse_iterator rend(); |
rend()関数は文字列の最初を指すリバースイテレータを返す。
関連トピックbasic_string &replace( size_type インデックス, size_type 文字数, const basic_string &文字列 ); basic_string &replace( size_type インデックス1, size_type 文字数1, const basic_string &文字列, size_type インデックス2, size_type 文字数2 ); basic_string &replace( size_type インデックス, size_type 文字数, const char *文字列 ); basic_string &replace( size_type インデックス, size_type 文字数1, const char *文字列, size_type 文字数2 ); basic_string &replace( size_type インデックス, size_type 文字数1, size_type 文字数2, char 文字 ); basic_string &replace( iterator 置換開始位置, iterator 置換終了位置, const basic_string &文字列 ); basic_string &replace( iterator 置換開始位置, iterator 置換終了位置, const char *文字列 ); basic_string &replace( iterator 置換開始位置, iterator 置換終了位置, const char *文字列, size_type 文字数 ); basic_string &replace( iterator 置換開始位置, iterator 置換終了位置, size_type 文字数, char 文字 ); |
replace()関数は以下のように振る舞う:
string s = "They say he carved it himself...from a BIGGER spoon";
string s2 = "find your soul-mate, Homer.";
s.replace( 32, s2.length(), s2 );
cout << s << endl;
関連トピックvoid reserve( size_type 文字数 ); |
reserve() 関数は現在の文字列の許容量(capacity())を文字数 以上にする。
関連トピックvoid resize( size_type 文字数 ); void resize( size_type 文字数, char 文字 ); |
resize() 関数は現在の文字列を文字数 のサイズに変更する、オプションで、新たにできた空間を埋める文字 を指定することができる。
size_type rfind( const basic_string &文字列, size_type インデックス ); size_type rfind( const char *文字列, size_type インデックス ); size_type rfind( const char *文字列, size_type インデックス, size_type 文字数 ); size_type rfind( char 文字, size_type インデックス ); |
rfind()関数は以下のような動作をする:
int loc;
string s = "My cat's breath smells like cat food.";
loc = s.rfind( "breath", 8 );
cout << "The word breath is at index " << loc << endl;
loc = s.rfind( "breath", 20 );
cout << "The word breath is at index " << loc << endl;
関連トピックsize_type size(); |
size() 関数は現在の文字列の文字数を返す
関連トピックbasic_string substr( size_type インデックス, size_type 文字数 = npos ); |
substr()関数は現在の文字列のインデックス から始まり長さ の部分文字列部分文字列を返す。 文字数が省略された場合にはデフォルトとしてstring::nposが使用され、substr()関数の戻り値はインデックスから最後まで全部になる。 サンプル:
string s("What we have here is a failure to communicate");
string sub = s.substr(21);
cout << "The original string is " << s << endl;
cout << "The substring is " << sub << endl;
表示
The original string is What we have here is a failure to communicate
The substring is a failure to communicate
void swap( basic_string &文字列 ); |
swap()関数は文字列 と現在の文字列を入れ替える。サンプル:
string first( "これが最初" );
string second( "そして二番目" );
first.swap( second );
cout << first << endl;
cout << second << endl;
displays
そして二番目
これが最初