HW机试输入输出格式(C++)

加速C++输入

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

std::ios::sync_with_stdio(false) 可以减少C++的I/O流与C的I/O流之间的同步开销。

std::cin.tie(nullptr) 可以减少每次输入操作时刷新输出缓冲区的开销。

C++的I/O流(如 std::cin 和 std::cout)和C的I/O流(如 scanf 和 printf)是两种不同的输入输出机制。

C++的I/O流是基于对象的,提供了类型安全和格式化功能,但相对复杂;C的I/O流是基于函数的,简单高效,但缺乏类型安全。

为了保证C++的I/O流和C的I/O流之间的兼容性,C++标准库默认将两者同步。

这意味着当你使用 std::cin 或 std::cout 时,程序会确保C的I/O流状态与C++的I/O流状态一致。这种同步机制虽然保证了兼容性,但会带来额外的开销。

输入不定长数组

在机试中存在输入数据是一个不定长的数组,即不会给出长度而是直接给出数组内容

bash 复制代码
3 4 6 8 3 2 6

这个需要 std::istringstream 字符串流类,先读入原始数据流再进行解析。

cpp 复制代码
#include <iostream>
#include <sstream>
#include <string>
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    std::string input;
    std::vector<int> numbers;
    std::getline(std::cin, input); // 读取整行输入

    // 使用字符串流解析输入
    std::istringstream iss(input);
    int num;
    while (iss >> num) {
        numbers.push_back(num); // 将解析的数字存入vector
    }

    std::cout << "输入的数组为:";
    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    return 0;
}

输入特定分隔符的数据

bash 复制代码
1627845600,client1,factorA,10
cpp 复制代码
#include <iostream>
#include <sstream>
#include <string>

int main() {
    // 输入字符串
    std::string input;
    std::getline(cin, input);

    // 使用istringstream进行解析
    std::istringstream iss(input);

    // 定义变量来存储解析后的数据
    int timestamp;
    std::string client;
    std::string factor;
    int value;

    // 使用getline和逗号分隔符进行解析
    std::string token;
    std::getline(iss, token, ','); // 解析时间戳
    timestamp = std::stoi(token); // 转换为int
    std::getline(iss, client, ','); // 解析客户端
    std::getline(iss, factor, ','); // 解析因子
    std::getline(iss, token, ','); // 解析值
    value = std::stoi(token); // 转换为int

    std::cout << "Timestamp: " << timestamp << std::endl;
    std::cout << "Client: " << client << std::endl;
    std::cout << "Factor: " << factor << std::endl;
    std::cout << "Value: " << value << std::endl;

    return 0;
}

std::getline的第三个参数是分隔符,这里指定为逗号(,)。

这个函数会从输入流iss中读取字符,直到遇到分隔符(逗号)为止,并将读取的内容存储到字符串token中。

遇到分隔符后,分隔符本身不会被存储到token中,而是被跳过,流的读取位置会移动到分隔符之后。

输出格式

设置宽度

cpp 复制代码
#include <iostream>
#include <iomanip> // 包含 setw 的头文件
int main() {
    int a = 123, b = 4567;
    std::cout << std::setw(10) << a << std::endl;
    std::cout << std::setw(10) << b << std::endl;
    return 0;
}
bash 复制代码
       123
      4567

std::setw(10) 表示接下来的输出宽度为 10 个字符,如果数据不足 10 个字符,会在前面补空格。

设置输出精度

对于浮点数的输出,我们通常需要控制小数点后的精度。可以使用 std::setprecision 操纵符来设置小数点后的位数。例如:

cpp 复制代码
#include <iostream>
#include <iomanip>
int main() {
    double pi = 3.141592653589793;
    std::cout << std::setprecision(5) << pi << std::endl;
    std::cout << std::setprecision(10) << pi << std::endl;
    return 0;
}
bash 复制代码
3.14159
3.141592654

std::setprecision(5) 表示小数点后保留 5 位数字,std::setprecision(10) 表示小数点后保留 10 位数字。

固定小数点格式

在上面基础上加上fixed

cpp 复制代码
#include <iostream>
#include <iomanip>
int main() {
	double pi = 3.14;
	std::cout <<std::fixed <<std::setprecision(5) << pi << std::endl;
	std::cout <<std::fixed <<std::setprecision(10) << pi << std::endl;
	return 0;
}
bash 复制代码
3.14000
3.1400000000

设置输出对齐方式

默认情况下,输出是右对齐的,但可以通过 std::left 和 std::right 操纵符来改变对齐方式。例如:

cpp 复制代码
#include <iostream>
#include <iomanip>
int main() {
    std::cout << std::setw(10) << std::left << "Hello" << std::endl;
    std::cout << std::setw(10) << std::right << "World" << std::endl;
    return 0;
}
bash 复制代码
Hello     
     World

设置填充字符

在设置输出宽度时,如果数据不足指定宽度,会在前面填充空格。但我们也可以通过 std::setfill 操纵符来指定填充字符。例如:

cpp 复制代码
#include <iostream>
#include <iomanip>
int main() {
    std::cout << std::setw(10) << std::setfill('*') << 123 << std::endl;
    return 0;
}
bash 复制代码
*******123

设置数值格式

C++ 还允许我们控制数值的输出格式,例如十进制、十六进制、八进制等。可以使用 std::dec、std::hex 和 std::oct 操纵符来设置。例如:

cpp 复制代码
#include <iostream>
int main() {
    int num = 255;
    std::cout << std::dec << num << std::endl; // 十进制
    std::cout << std::hex << num << std::endl; // 十六进制
    std::cout << std::oct << num << std::endl; // 八进制
    return 0;
}
相关推荐
君鼎18 分钟前
C++设计模式——单例模式
c++·单例模式·设计模式
风逸hhh2 小时前
python打卡day25@浙大疏锦行
开发语言·python
刚入门的大一新生2 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
小冯的编程学习之路2 小时前
【软件测试】:推荐一些接口与自动化测试学习练习网站(API测试与自动化学习全攻略)
c++·selenium·测试工具·jmeter·自动化·测试用例·postman
chxii3 小时前
5java集合框架
java·开发语言
老衲有点帅3 小时前
C#多线程Thread
开发语言·c#
C++ 老炮儿的技术栈4 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
IsPrisoner4 小时前
Go语言安装proto并且使用gRPC服务(2025最新WINDOWS系统)
开发语言·后端·golang
猪八戒1.04 小时前
C++ 回调函数和Lambda表达式
c++