新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

c++输出字符串的有哪些方法,c ++中如何输出字符串

发布时间:2024-03-02 17:26:57

c++输出字符串的有哪些方法

在C++中,输出字符串可使用std::cout还是std::printf函数来实现。

使用std::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
", str.c_str());
    return 0;
}

注意:在使用std::printf输出字符串时,需要通过c_str方法将std::string类型转换为C风格的字符串。