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;
}
相关推荐
cwtlw几秒前
mybatisPlus介绍
java·开发语言·spring boot·学习
qq_433554546 分钟前
C++ 顺序表
c++
Ase5gqe26 分钟前
关于maven的java面试题汇总
java·开发语言·maven
suuijbd1 小时前
单例模式和单例Bean
java·开发语言·单例模式
天堂的恶魔9461 小时前
C++设计模式 —— 工厂模式
javascript·c++·设计模式
华梦岚1 小时前
Perl语言的语法糖
开发语言·后端·golang
天堂的恶魔9461 小时前
C++设计模式 —— 单例模式
c++·单例模式·设计模式
厉君韵1 小时前
Scala语言的数据类型
开发语言·后端·golang
霜雪殇璃2 小时前
2025.1.8(qt图形化界面之消息框)
开发语言·qt