トップページ >  cygwin >  C++のString
初版2012/03/08: 最終更新日2012/03/08
  C++のString
目次
C++のString
C++のString
C++の標準ライブラリでStringがあります。以下stringを使用した例です。

#include <iostream>
#include <string >

int main{
    string str1;
    str1 = "abc";
    std::cout << str << std::endl;
    return 0;
}
str1はコンストラクタなしのため、空文字列で初期化されます。stringは内部で自動的にメモリ確保されるため、文字数指定は不要です。
文字列連結などもjavaみたいに可能です。以下、サンプルです。

#include <iostream>
#include <string >

int main{
    string str1,str2;//こんな宣言が可能
    str1 = "abc";
    str2 = "def";
    
    str1 += str2;//文字列を連結
    std::cout << str1 << std::endl;
    return 0;
}