stringstreamの使い方
stringstreamクラスの変数を使うためには、以下のように宣言します。
std::stringstream ss;
また、文字列を取得するときは、以下のようにstr関数を使います。
ss.str();
文字列や数値を追加する
stringstreamクラスの変数に、文字列や数値を変更するには以下のようにします。
ss << "samurai";
これは以下のようにいくつもつなげることができます。
ss << "samurai" << 11;
次のプログラムで確認してみましょう。
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::stringstream ss;
std::string str = "samurai, ";
int num = 2018;
ss << str << num;
std::cout << ss.str() << "n";
return 0;
}
実行結果:
samurai, 2018
文字列を抜き出す
逆に追加した文字列を取り出すときは、
std::string str
ss >> str;
のように使います。
次のプログラムで確認してみましょう。
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::stringstream ss;
std::string str = "samurai";
int num = 10;
std::string output;
ss << str << num;
ss >> output;
std::cout << output << 'n';
return 0;
}
実行結果:
samurai10
文字列を空白で区切る
stringstreamから文字列を取り出すときには、空白や改行で区切られます。
次のプログラムで確認してみましょう。
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::stringstream ss;
std::string str = "samurai engineer programmer";
std::string output;
ss << str;
ss >> output;
std::cout << "元の文字列:" << str << "n";
std::cout << "抽出:" << output << "n";
return 0;
}
実行結果:
元の文字列:samurai engineer programmer
抽出:samurai
文字列から数値を取り出す
stringstreamを使うと、簡単に文字列から数値のみを取り出すことができます。
次のプログラムで確認してみましょう。
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::stringstream ss;
std::string str = "18 years old.";
int outputNum;
ss << str;
ss >> outputNum;
std::cout << "元の文字列:" << str << "n";
std::cout << "抽出:" << outputNum << "n";
return 0;
}
実行結果:
元の文字列:18 years old.
抽出:18
getlineで1行ごとに文字列を取得
stringstreamは空白ごとに値を取り出していきます。
1行ごとに文字列を取得したい場合は、getline関数を使います。
以下のように使います。
std::getline(stringstream型の変数,文字列);
getline関数を使うには、iostreamというライブラリをインクルードします。
次のプログラムで確認してみましょう。
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::stringstream ss;
std::string str = "samurai engineer programmernhello world";
std::string output;
ss << str;
std::getline(ss, output);
std::cout << "元の文字列:n" << str << "n";
std::cout << "抽出:" << output << "n";
return 0;
}
実行結果:
元の文字列:
samurai engineer programmer
hello world
抽出:samurai engineer programmer
ignoreで文字列を読み飛ばす
文字列を1つ読み飛ばしたいときには、ignore関数を使います。
以下のように使います。
stringstream型.ignore(読み飛ばす数, 区切り文字);
また引数を入れない場合は、1文字を読み飛ばします。
次のプログラムで確認してみましょう。
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::stringstream ss;
std::string str = "2018/05";
int year;
int month;
ss << str;
ss >> year;
ss.ignore();
ss >> month;
std::cout << "元の文字列:" << str << "nn";
std::cout << "年:" << year << "n";
std::cout << "月:" << month << "n";
return 0;
}
実行結果:
元の文字列:2018/05
年:2018
月:5
0埋めなどの書式を変更する
stringstreamを使うと、書式を簡単に変更することができます。書式を変更するためには、iomanipというライブラリをインクルードする必要があります。
文字の幅を設定する
iomanipライブラリのsetw関数を使うと、数値の桁を揃えることができます。
次のプログラムで確認してみましょう。
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
int main() {
std::stringstream ss;
int year = 1999;
int month = 5;
ss << "年:" << std::setw(4) << year << "n";
ss << "月:" << std::setw(4) << month;
std::cout << ss.str() << "n";
return 0;
}
実行結果:
年:1999
月: 5
文字で埋める
setfill関数を使うことで、setw関数で指定した文字の幅に合わせ桁を揃えるために文字を埋めることができます。
これを使うと4桁に合わせ、残りを0を埋めることができます。
setfill関数で指定した場合は、その効果が続きます。
次のプログラムで確認してみましょう。
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
int main() {
std::stringstream ss;
int year = 1999;
int month = 5;
ss << "年:" << std::setw(4) << std::setfill('0') << year << "n";
ss << "月:" << std::setw(4) << month;
std::cout << ss.str() << "n";
return 0;
}
実行結果:
年:1999
月:0005
16進数で出力する
stringstreamを使うと、10進数の数値を16進数の文字列で出力することができます。
16進数を使うためにはhexという変数を使います。
次のプログラムで確認してみましょう。
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
int main() {
std::stringstream ss;
int decimal = 2018;
ss << "10進数:" << decimal << "n";
ss << "16進数:0x" << std::hex << decimal;
std::cout << ss.str() << "n";
return 0;
}
実行結果:
10進数:2018
16進数:0x7e2
まとめ
いかがだったでしょうか?
今回はstringstreamを使って文字列を操作する方法を解説しました。文字列から数値を取り出したいときやや、数値の桁を揃えたいときなどに使ってみてください。
もし、文字列から検索する方法を忘れてしまったらこの記事を確認してください。