C++ 知识点02 输入输出

C++ 输入输出(cin /cout/printf /scanf)

一、C++ 标准输入输出头文件

必须先包含头文件:

cpp 复制代码
#include <iostream>

所有输入输出都在 std 命名空间 里:

  • std::cout :标准输出(打印)

  • std::cin :标准输入(键盘读入)


二、基础输出 cout

1. 最简单用法

cpp 复制代码
#include <iostream>
​
int main()
{
    // 完整写法,不加 using
    std::cout << "Hello C++" << std::endl;
​
    return 0;
}
  • << :输出运算符,链式拼接

  • std::endl换行 + 刷新缓冲区

2. 简化写法:using 命名空间

cpp 复制代码
#include <iostream>
using namespace std;
​
int main()
{
    cout << "你好" << endl;
    cout << 123 << endl;
    cout << 3.14 << endl;
​
    return 0;
}

3. 一行输出多个变量

cpp 复制代码
#include <iostream>
using namespace std;
​
int main()
{
    int a = 10;
    double b = 3.1415;
    
    cout << "a = " << a << "  b = " << b << endl;
    return 0;
}

4. 不换行输出

不用 endl 就行:

cpp 复制代码
cout << "我不换行";
cout << "接着在同一行";

三、基础输入 cin

1. 输入单个变量

cpp 复制代码
#include <iostream>
using namespace std;
​
int main()
{
    int a;
    cout << "请输入一个整数:";
    cin >> a;   // 从键盘读入整数给 a
​
    cout << "你输入的是:" << a << endl;
    return 0;
}

2. 一次性输入多个值

空格、回车 都能分隔输入

cpp 复制代码
#include <iostream>
using namespace std;
​
int main()
{
    int x, y;
    cout << "输入两个整数:";
    cin >> x >> y;
​
    cout << "x=" << x << " y=" << y << endl;
    return 0;
}

3. 输入浮点型、字符

cpp 复制代码
#include <iostream>
using namespace std;
​
int main()
{
    double d;
    char ch;
​
    cout << "输入小数:";
    cin >> d;
​
    cout << "输入一个字符:";
    cin >> ch;
​
    cout << "小数:" << d << " 字符:" << ch << endl;
    return 0;
}

四、cin /cout 读写字符串

1. 普通字符串 string

需要头文件 <string>

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
int main()
{
    string s;
    cout << "输入一个单词:";
    cin >> s;  
​
    cout << "你输入的单词:" << s << endl;
    return 0;
}

⚠️ cin >> s 不能读带空格的句子,遇到空格自动停止。

2. 读一整行带空格:getline

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
int main()
{
    string s;
    cout << "输入一句话(可以带空格):" << endl;
    
    getline(cin, s);
    cout << "整行内容:" << s << endl;
​
    return 0;
}

五、格式化输出(保留小数、对齐)

需要头文件:

cpp 复制代码
#include <iomanip>

1. 保留小数点位数

cpp 复制代码
#include <iostream>
#include <iomanip>
using namespace std;
​
int main()
{
    double pi = 3.1415926;
​
    // 保留2位小数
    cout << fixed << setprecision(2) << pi << endl;
    // 保留4位小数
    cout << fixed << setprecision(4) << pi << endl;
​
    return 0;
}
  • fixed :固定小数形式

  • setprecision(n) :保留 n 位小数

2. 宽度对齐、补空格

cpp 复制代码
cout << setw(8) << 123 << endl;  // 占8个字符宽度,右对齐

六、C 语言风格输入输出:printf /scanf

C++ 完全兼容,做题常用,格式化更强。

1. printf 输出

cpp 复制代码
#include <cstdio>
​
int main()
{
    int a = 10;
    double b = 3.14;
​
    printf("整数a = %d\n", a);
    printf("小数b = %.2f\n", b);  // 保留2位小数
    return 0;
}

常用格式符:

  • %d int

  • %f double/float

  • %c 字符

  • %s 字符串

2. scanf 输入

cpp 复制代码
#include <cstdio>
​
int main()
{
    int x;
    scanf("%d", &x);  // 注意要加 &
    printf("x = %d", x);
    return 0;
}

七、cin/cout 和 scanf/printf 区别

  1. cin/cout

    • 不用记格式符 %d %f

    • 自动识别类型,写法简单

    • 速度比 printf 稍慢(算法竞赛可加加速语句)

  2. scanf/printf

    • 格式化超强,控制小数、对齐很方便

    • 速度快,竞赛常用

    • 需要记格式符,要加 &

cin/cout 加速(竞赛必加)

放在 main 第一行:

cpp 复制代码
ios::sync_with_stdio(false);
cin.tie(nullptr);

八、必背总结

  1. 头文件:#include <iostream>

  2. 输出:cout << 内容

  3. 输入:cin >> 变量

  4. 换行:endl"\n"

  5. 保留小数:#include <iomanip> + fixed + setprecision

  6. 读整行带空格:getline(cin, 字符串)

  7. 老式写法:printf / scanf 格式化更强

相关推荐
JieE21213 小时前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack2021 小时前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树1 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2122 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2122 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术2 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
clint4562 天前
C++进阶(1)——前景提要
c++
用户497863050732 天前
(一)小红的数组操作
算法·编程语言