C++ 中流操作控制


C++中把数据之间的传输操作称为流。C++的流是通过重载运算符 “<<” 和 “>>” 执行输入和输出操作。输出操作是向流中插入一个字符序列,因此,在流操作中,将左移运算符 “<<” 称为插入运算符。输入操作是从流中提取一个字符序列,因此,将右移运算符“>>” 称为提取运算符。

1、cout

cout 代表显示器,执行 cout << x 操作就相当于把 x 的值输出到显示器。

2、cin

cin 代表键盘,执行 cin >> x 就相当于把键盘输入的数据赋值给变量。

3、流操作的控制

C++在头文件 iomanip.h 中定义了一些控制流输出格式的函数,默认情况下整型数按十进制形式输出,也可以通过hex将其设置为十六进制输出。

常用的流操作控制函数:
(1) long setf(long f);
(2) long unself(long f);
(3) int width();
(4) int width(int w);
(5) setiosflags(long f);
(6) resetiosflags(long f);
(7) setfill(int c);
(8) setprecision(int n);
(9) setw(int w);

常用的数据输入/输出的格式控制操作符:
(1) dec:转换为十进制输出整数,也是默认格式。
(2) oct: 转换为八进制输出整数。
(3) hex: 转换为十六进制输出整数。
(4) ws: 从输出流中读取空白字符。
(5) endl: 输出换行符 \n 并刷新流。
(6) ends: 输出一个空字符\0。
(7) flush: 只刷新一个输出流。

4、示例

(1) 输出大写的十六进制

#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
    int i = 0x2F, j = 255;
    cout << i << endl;
    cout << hex << i << endl;
    cout << hex << j << endl;
    cout << hex << setiosflags(ios::uppercase) << j << endl;

    return 0;
}

输出结果:
在这里插入图片描述

(2) 控制打印格式程序

#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
    double a = 123.456789012345;
    cout << a << endl;
    cout << setprecision(9) << a << endl;
    cout << setprecision(6); //恢复默认格式(精度为6)
    cout << setiosflags(ios::fixed);
    cout << setiosflags(ios::fixed) << setprecision(8) << a << endl;
    cout << setiosflags(ios::scientific) << a << endl;
    cout << setiosflags(ios::scientific) << setprecision(4) << a << endl;

    return 0;
}

输出结果:
在这里插入图片描述
(3) 控制输出精确度

#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
    int x = 123;
    double y = -3.1415;
    cout << "x=";
    cout.width(10);
    cout << x << endl;
    cout << "y=";
    cout.width(10);
    cout << y << endl;
    cout.setf(ios::left);
    cout << "x=";
    cout.width(10);
    cout << x << endl;
    cout << "y=";
    cout << y << endl;
    cout.fill('*');
    cout.precision(4);
    cout.setf(ios::showpos);
    cout << "x=";
    cout.width(10);
    cout << x << endl;
    cout << "y=";
    cout.width(10);
    cout << y << endl;

    return 0;
}

输出结果:
在这里插入图片描述
(4) 流输出小数控制

#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
    float x = 20, y = -400.00;
    cout << x << ' ' << y << endl;
    cout.setf(ios::showpoint); //强制显示小数点和无效0
    cout << x << ' ' << y << endl;
    cout.unsetf(ios::showpoint);
    cout.setf(ios::scientific); //设置按科学表示法输出
    cout << x << ' ' << y << endl;
    cout.setf(ios::fixed); //设置按定点表示法输出
    cout << x << ' ' << y << endl;

    return 0;
}

输出结果:
在这里插入图片描述

5、printf

c++ 中还保留着C 语言中的屏幕输出函数 printf。使用方法一样

### C++ 中流(istream, ostream)的概念及其作用 #### 1. 基本概念 在 C++ 中,是一种抽象化的数据传输机制,用于描述程序与外部设备之间的数据交换过程。通过,可以方便地实现输入/输出操作。`istream` 表示输入,负责从数据源读取信息;而 `ostream` 表示输出,负责向目标位置发送信息[^2]。 --- #### 2. 输入 (`istream`) `istream` 是 C++ 标准库中定义的一个类,主要用于处理输入操作。它是许多输入类的基础,例如标准输入 `cin` 就是从 `istream` 类派生而来。以下是关于 `istream` 的一些重要特性: - **主要用途**:从各种数据源(如键盘、文件等)获取数据。 - **常见操作**: - 使用运算符 `>>` 提取数据。 - 支持多种数据类型的提取,包括整数、浮点数、字符串等。 以下是一个简单的例子展示如何使用 `istream`: ```cpp #include <iostream> using namespace std; int main() { int num; cin >> num; // 使用 istream (cin) 从标准输入读取一个整数 cout << "You entered: " << num << endl; return 0; } ``` 在此代码中,`cin` 是 `istream` 的实例,用于捕获用户的输入[^2]。 --- #### 3. 输出 (`ostream`) `ostream` 同样是 C++ 标准库中的一个重要类,专注于输出操作。像 `cout` 这样的标准输出也是基于 `ostream` 构建的。以下是有关 `ostream` 的关键点: - **主要用途**:将数据发送到指定的目标(如屏幕、文件等)。 - **常见操作**: - 使用运算符 `<<` 插入数据。 - 可以设置格式化选项(如十进制、十六进制、八进制等),以便控制输出样式。 下面的例子展示了如何利用 `ostream` 执行格式化输出: ```cpp #include <iostream> using namespace std; class Person { public: string name; int age; Person(string n, int a) : name(n), age(a) {} friend ostream& operator<<(ostream& os, const Person& p); }; ostream& operator<<(ostream& os, const Person& p) { os << "Name: " << p.name << ", Age: " << p.age; return os; } int main() { Person person("Alice", 30); cout << person << endl; // 使用 ostream (cout) 输出对象的内容 return 0; } ``` 在这个案例里,自定义了 `Person` 类,并重载了 `operator<<` 函数,使得能够轻松打印该类的对象属性[^2]。 --- #### 4. 的作用总结 无论是 `istream` 还是 `ostream`,它们都提供了统一且灵活的方式来管理不同形式的数据动。这种设计不仅简化了开发程,还增强了代码的可移植性和扩展性。具体来说: - **灵活性**:支持多种数据类型和多种形式的输入输出。 - **易用性**:借助插入器(`<<`)和提取器(`>>`)操作符,使语法直观简洁。 - **模块化**:允许用户创建自己的类或修改现有行为,满足特定需求[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值