C++でのformat関数の使い方は何ですか?
C++には組み込みのフォーマット関数はないので、同等の機能は以下の方法で実装することができます。
- 標準的なstringstream
#include <iostream>
#include <sstream>
#include <iomanip>
int main() {
std::stringstream ss;
int num = 123;
double pi = 3.14159;
ss << "The number is: " << num << ", and the value of pi is: " << std::fixed << std::setprecision(2) << pi;
std::cout << ss.str() << std::endl;
return 0;
}
- プリント文
#include <iostream>
int main() {
int num = 123;
double pi = 3.14159;
printf("The number is: %d, and the value of pi is: %.2f\n", num, pi);
return 0;
}
これらの方法はデータのフォーマット出力を実現し、ある程度の柔軟性と制御を提供します。