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 格式化更强

相关推荐
土司大王6 小时前
LeetCode hot100——除了自身以外数组的乘积
数据结构·算法·leetcode
IvanCodes6 小时前
RAG 实战教程(三):向量数据库检索算法,KNN、IVF、HNSW 与 Faiss 实战
人工智能·算法·agent
傻啦嘿哟6 小时前
王者荣耀爬虫:爬取全英雄皮肤数据,用Python做可视化分析
开发语言·爬虫·python
阿无,6 小时前
布隆过滤器
java·算法·哈希算法
AI直播技术杂谈6 小时前
直播画面突然模糊了,排查了三个小时
开发语言·php
徒慕风流7 小时前
激光雷达回波信号滤波与时刻鉴别算法:原理、代码实现与仿真验证
python·算法·激光雷达
菜冻鱼7 小时前
Python-pytorch-数据加载
开发语言·人工智能·pytorch·python·深度学习·机器学习
这料鬼有毒7 小时前
二刷hot100-70.爬楼梯
算法
heimeiyingwang7 小时前
【架构实战】可观测性三支柱实战:Metrics、Logging、Tracing 如何统一落地
开发语言·架构·php
caimouse7 小时前
ReactOS 图形系统分析(53):系统库存对象 — stockobj.c
c语言·开发语言·spring