C++ 文字列出力の基本:cout, printf, puts徹底解説
C++で文字列を出力するには、std::coutやstd::printf関数を使用できる。
coutを使用して文字列を出力する方法は次のとおりです。
#include <iostream>
int main() {
std::string str = "Hello, World!";
std::cout << str << std::endl;
return 0;
}
std::printfを使用して文字列を出力する方法は次のとおりです:
#include <iostream>
int main() {
std::string str = "Hello, World!";
std::printf("%s\n", str.c_str());
return 0;
}
注意:std::printfで文字列を出力する際は、std::string型をCスタイルの文字列に変換するためにc_strメソッドを使用する必要があります。