C++中的所有输出方式
  • 板块学术版
  • 楼主Cat_Jack
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/7/30 18:55
  • 上次更新2024/7/30 20:06:54
查看原帖
C++中的所有输出方式
1408662
Cat_Jack楼主2024/7/30 18:55

1.使用标准输出流std::cout:

#include <iostream>
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

2.使用printf函数(来自C语言):

#include <cstdio>
int main() {
    printf("Hello, World!\n");
    return 0;
}

3.使用字符串流std::stringstream:

#include <sstream>
#include <iostream>
int main() {
    std::stringstream ss;
    ss << "Hello, World!";
    std::cout << ss.str() << std::endl;
    return 0;
}

4.使用std::ofstream写入文件:


#include <fstream>
int main() {
    std::ofstream outFile("output.txt");
    if (outFile) {
        outFile << "Hello, World!" << std::endl;
    }
    return 0;
}

大抵是太久没有编程了,近夜来手指愈发冰凉,只有在键盘上敲击,才能...... 以上便是我所知的所有输出方式了,虽然我通常只用前两种......

2024/7/30 18:55
加载中...