ostringstreamクラス

文字の加工にはostringstreamが便利。

#include <iostream>
#include <sstream>
using std::cout;
using std::endl;

int main () {
    std::ostringstream ss;
    
    int i = 10;
    ss << "foo" << i << ".txt";
    
    cout << ss.str() << endl;
    return 0;
}
$ main
foo10.txt

ちなみにss.str()の戻り値はstringクラスなのでchar型が欲しければさらにc_strを呼び出す必要がある。

    // stringクラスのc_str呼び出し
    cout << ss.str().c_str() << endl;


stringstreamクラスというものありますがこちらはostringstream(出力)とistringstream(入力)の両方を兼ね備えたクラスになります。

なので文字の加工だけするのであればostringstreamクラスで十分ってこと。