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

相关推荐
qcx231 小时前
深度解析Deepseek V4:1M 上下文不是军备竞赛,是养 Agent 的人才知道的痛
java·开发语言
小此方1 小时前
Re:思考·重建·记录 现代C++ C++11篇(六) 从 shared_ptr 到 weak_ptr:起底智能指针的引用计数与循环引用之痛
开发语言·c++·c++11·现代c++
字节高级特工1 小时前
MySQL数据库基础与实战指南
数据库·c++·人工智能·后端·mysql·adb
郝学胜-神的一滴1 小时前
中级OpenGL教程 004:为几何体注入法线灵魂
c++·unity·游戏引擎·godot·图形渲染·opengl·unreal
晨非辰1 小时前
吃透C++两大默认成员函数:const成员函数、 & 取地址运算符重载
java·大数据·开发语言·c++·人工智能·后端·面试
学会去珍惜1 小时前
8天学会C语言编程第2天:变量、数据类型和输入/输出,3分钟上手
c语言·实战·变量·编程入门·输入输出
我滴老baby1 小时前
多智能体协作系统设计当AI学会团队合作效率翻十倍
android·开发语言·人工智能
落雪寒窗-1 小时前
Python进阶核心路线(工程向)
开发语言·python
humcomm1 小时前
全栈开发技术栈的最新进展(2026年视角)
开发语言·架构