cppreference.com -> C++文字列 -> 詳細

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
    

演算子

文法:

  ==
  >
  <
  >=
  <=
  !=
  +
  +=
  []

==, >, <, >=, <=, !=の各演算子を使用して文字列の比較を行うことができる。また、+演算子や+=演算子を用いて文字列を結合することができる。 []演算子を使用して特定の箇所の文字を参照することができる。

関連トピック
at(), compare().

append

文法:

  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!!!!!!!!!!
関連トピック
+ 演算子

assign

文法:

  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
    

at

文法:

  reference at( size_type インデックス );

at()関数はインデックス で指定された文字への参照を返す。 もしインデックス が文字列の範囲を超えていた場合にはout_of_rangeクラスのオブジェクトを範囲外アクセスのエラーを例外として報告する。

    string text = "ABCDEF";
    char ch = text.at( 2 );

文字'C'が得られる

関連トピック
[]演算子

begin

文法:

  iterator begin();

begin()関数は現在の文字列の最初の要素を指すイテレータを返す。

関連トピック
end()

c_str

文法:

  const char *c_str();

c_str()関数は、現在の文字列を指す、C言語方式の文字列のポインタを返す。

関連トピック
[]

capacity

文法:

  size_type capacity();

capacity()関数は現在の文字列がメモリ割り当てを追加で行わなくても格納可能な文字のサイズを返す。この数値はsize()の返値よりも大きいことが保証されている。

関連トピック
max_size(), reserve(), resize(), size(),

compare

文法:

  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()関数は文字列 との比較で様々な結果を返す。

返値場合
自分 < 比較対象
ゼロ自分 == 比較対象
自分 > 比較対象

また、この関数は以下のようなさまざまな形式での比較を行う。

関連トピック
演算子

copy

文法:

  size_type copy( char *文字列, size_type 文字数, size_type インデックス );

copy()関数は現在の文字列のインデックス で始まる文字数 分の文字を文字列 にコピーする。返値はコピーした文字数である。


data

文法:

  const char *data();

data()関数は現在の文字列の最初の文字を示すポインタを返す。

関連トピック
c_str()

empty

文法:

  bool empty();

empty()関数は文字列が空の時にtrue を返す。そうでない場合にはfalse を返す。


end

文法:

  iterator end();

end()関数は現在の文字列の最後を指すイテレータを返す。

関連トピック
begin()

erase

文法:

  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?'
    現在の状態は ''

find

文法:

  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;
    
    

find_first_of

文法:

  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()関数は以下の動作を行う。

関連トピック
find()

find_first_not_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()は以下のような検索を行う:

関連トピック
find()

find_last_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()関数は以下のような検索を行う:

関連トピック
find()

find_last_not_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() 関数は以下のような検索を行う:

関連トピック
find()

get_allocator

文法:

  allocator_type get_allocator();

get_allocator()関数は現在の文字列のアロケータを返す


insert

文法:

  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()は以下のようなたくさんの目的の操作を行う:

関連トピック
replace()

length

文法:

  size_type length();

length() 関数は現在の文字列の長さを返す。この関数の返値はsize()と同じである。

関連トピック
size()

max_size

文法:

  size_type max_size();

max_size()関数は文字列が保持することが可能な最大の文字数を返す。


rbegin

文法:

  const reverse_iterator rbegin();

rbegin()関数は文字列の最後を指すリバースイテレータを返す。

関連トピック
rend()

rend

文法:

  const reverse_iterator rend();

rend()関数は文字列の最初を指すリバースイテレータを返す。

関連トピック
rbegin()

replace

文法:

  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()関数は以下のように振る舞う:

以下のコードは "They say he carved it himself...find your soul-mate, Homer." と表示するサンプルである:
    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;
  

関連トピック
insert()

reserve

文法:

  void reserve( size_type 文字数 );

reserve() 関数は現在の文字列の許容量(capacity())を文字数 以上にする。

関連トピック
capacity()

resize

文法:

  void resize( size_type 文字数 );
  void resize( size_type 文字数, char 文字 );

resize() 関数は現在の文字列を文字数 のサイズに変更する、オプションで、新たにできた空間を埋める文字 を指定することができる。


rfind

文法:

  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()関数は以下のような動作をする:

以下のサンプルコードでは最初にrfind()が呼ばれたときには、ターゲットとなる言葉が9文字目にあるためにstring::nposが返される。しかし、文字列の20番目までには含まれるため、二回目に呼ばれたときには9が返る。
    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;
  

関連トピック
find()

size

文法:

  size_type size();

size() 関数は現在の文字列の文字数を返す

関連トピック
length(), max_size()

substr

文法:

  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

swap

文法:

  void swap( basic_string &文字列 );

swap()関数は文字列 と現在の文字列を入れ替える。サンプル:

    string first( "これが最初" );
    string second( "そして二番目" );
    first.swap( second );
    cout << first << endl;
    cout << second << endl;

displays

    そして二番目
    これが最初