CPP Cin 常见I/O方法总结+合并转

本文详细介绍了 C++ 中的 IO 流操作,包括 cin 的使用方法、cin.ignore 的功能、EOF 的处理方式等。同时对比了 cin.get 和 getline 的不同应用场景,并提供了 cin.eof 的解释与示例代码。

cin 用法详解:https://blog.youkuaiyun.com/bravedence/article/details/77282039

cin.ignore 详解:https://blog.youkuaiyun.com/imkelt/article/details/52202002

EOF for cin: https://blog.youkuaiyun.com/foxpeter/article/details/6072130

cin.get() and EOF: https://blog.youkuaiyun.com/qq_34686440/article/details/55005165

how to use eof(): https://blog.youkuaiyun.com/kmno400/article/details/5724577

C I/O: https://blog.youkuaiyun.com/baidu_41560343/article/details/90719093

find newline: https://bbs.youkuaiyun.com/topics/50041255

CPP StringStream: https://lavi-liu.blog.youkuaiyun.com/article/details/89070296

C/CPP string数据类型转换及分隔:https://www.cnblogs.com/loveprogramme/p/11000421.html

Cpp fstream: https://blog.youkuaiyun.com/Andy710660541/article/details/51045385; https://blog.youkuaiyun.com/xunmengpiaoyun/article/details/17054849?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=1328769.40256.16175124726219943&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

'\0' vs. "\0" https://bbs.youkuaiyun.com/topics/390615761

input processing:

  • cin>>
    • return the input stream buffer: istream& ==> can be checked in a conditional clause ==> false if EOF/nothing to read while trying to get cin>>a ==> !!!! not checking 1/0 of the value read
    • since it returns istream& ==> we can chain it by cin>>a>>b>>.....
    • use ' ' , '\t' or '\n' as delimiter ==> cin>> read words/chunks not just 1 byte/char
    • “若缓冲区中第一个字符是空格、tab或换行这些分隔符时,cin>>会将其忽略并清除,继续读取下一个字符,若缓冲区为空,则继续等待。但是如果读取成功,字符后面的分隔符是残留在缓冲区的,cin>>不做处理。”
  • cin.get
    • cin.get() returns int ==> ascii val of the char, and EOF with the value of -1 
    • cin.get(dest_char/char*, num_char, delimiter char)  returns istream& ==> we can chain it by cin.get(a).get(b)..... ===> read a single char or a specified chunk of size num_char before reaching delimiter char each time.
    • "遇到换行符时结束读取,但是不对换行符进行处理,换行符仍然残留在输入缓冲区。"
    • "cin.get(str,size);读取一行时,只能将字符串读入C风格的字符串中,即char*"
  • cin.getline
    • "getline读取一行字符时,默认遇到’\n’时终止,并且将’\n’直接从输入缓冲区中删除掉,不会影响下面的输入处理。"
    • "getline函数可以将字符串读入C++风格的字符串中,即string类型。"
    • istream& getline(char* s, streamsize count, char delim);
  • string::getline
    • "getline遇到结束符时,会将结束符一并读入指定的string中,再将结束符替换为空字符。" ==> similar result to cin.getline()
    • istream& getline ( istream& is, string& str, char delim);
    • the buffer for string::getline can grow dynamically by the power of cpp_string.
    • ==> if the delimitor is not found, getline will simply copy everything.
    • ==> when the istream is not valid (say only EOF), getline will not change the string at all.
  • <stdio.h> gets
    • "从标准输入设备读字符串,可以无限读取,不会判断上限,以回车结束或者EOF时停止读取"
    • char *gets( char *buffer );
    • suppressed use in CPP

 

other functionals

  • cin.ignore(int N, char delim) ==> ignore up to N char/bytes before encountering "delim" char
    • istream::ignore(char x) is not implemented ==> ignore(' ') will convert ' ' to an int and ignore that many char
    • ignore() ignores 1 char
    • !!! ignore (len, delim) will ignore the delim char as well !!!
      • see test code
      • #include <iostream>
        #include <sstream>
        #include <string>
        
        using namespace std;
        
        int main()
        {
        	string s = "1234567,67653562,25346457,324234\n";
        	istringstream ss;
        	ss.str(s);
        	ss.ignore(100, ',');
        	getline(ss, s, ',');
        	cout << s << endl;
        	ss.ignore(100, ',');
        	ss.ignore();
        	getline(ss,s,',');
        	cout << s;
        
        	cin.ignore(100, ','); //ignore(len, delimiter) ignores the delimiter char.!!!!
        	cin >> s;
        	cout << s;
        	return 0;
        }

         

  • cin.eof() ==> return the value of eofbit ==> set to 1 when encountering a "proper" EOF 

examples for string::getline and istream::ignore()

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
	string t1 = "res";
	string t2 = "res board";
	istringstream ss;

	string s1, s2;
	s1 = s2 = "temp";
	ss.str(t1);
	
	getline(ss, s1, ' ');
	cout << "string::getline(stream, string, delim) will copy everything is delim is not found.\n";
	cout << s1 << endl;
	getline(ss, s2);
	cout << "when the stream is invalid (with only EOF), string::getline will not change the string.\n";
	cout << (s2 == "") << " the string is not set to \"\"." << endl;
	cout << "the string still is: \n";
	cout << s2 << endl;

	ss.clear();
	ss.str(t2);
	ss.ignore(' ');
	getline(ss, s1);
	cout << s1 << endl;

	return 0;
}

s1 is not changed, since ss is empty

题目描述 输入: n n 个人、 m m 分钟的总时间、牛牛需连续睡眠 k k 分钟。 每个人 i i 有参数 a i a i ​ (吵闹开始时间)和 b i b i ​ (吵闹持续时长),以及一个长度为 m m 的吵闹值序列 c i , j c i,j ​ (第 j j 分钟的吵闹值)。 约束:睡眠期间若被吵闹,总吵闹值为所有人在该时段内吵闹值之和。 目标:找到睡眠起始时间 j j,使得 总吵闹值最小。 特殊条件:时间是一个环(即第 m m 分钟后回到第 1 1 分钟)。若 k + b i > m k+b i ​ >m,输出 -1(无解)14。 算法思路 1. 问题化 对每个睡眠起始时间 j j 的计算: 当牛牛从时间 j j 开始睡眠时,第 i i 个人的吵闹区间为 [ j , j + k − 1 ] [j,j+k−1]。但因其自身吵闹区间为 [ a i , a i + b i − 1 ] [a i ​ ,a i ​ +b i ​ −1](可跨天),实际影响区间需取交集,即 [ j , j + k − 1 ] ∩ [ a i , a i + b i − 1 ] [j,j+k−1]∩[a i ​ ,a i ​ +b i ​ −1]14。 关键观察: 对第 i i 个人,其影响睡眠的 有效吵闹区间 在环上对应一个固定长度 l e n i = m − k − b i + 1 len i ​ =m−k−b i ​ +1 的滑动窗口。问题化为: 对每个 i i,预处理其序列中所有长度为 l e n i len i ​ 的窗口的最小值,再枚举 j j 求所有人最小值之和14。 2. 单调队列优化(RMQ) 作用:对每个人 i i,在 O ( m ) O(m) 时间内求出其序列所有窗口的最小值。 步骤: 序列倍长:将 c i [ 1.. m ] c i ​ [1..m] 复制到 c i [ m + 1..2 m ] c i ​ [m+1..2m] 以处理环14。 单调队列维护: 初始化空双端队列。 遍历序列的每个位置 j j(从 1 1 到 2 m 2m): 出队尾:若队尾值 ≥ c i , j ≥c i,j ​ ,则弹出(保证队列单调递增)。 入队:将 ( j , c i , j ) (j,c i,j ​ ) 加入队尾。 出队头:若队头位置 < j − l e n i + 1 <j−len i ​ +1(超出窗口),则弹出。 记录最小值:窗口 [ j − l e n i + 1 , j ] [j−len i ​ +1,j] 的最小值为队头值14。 复杂度:每个 i i 为 O ( m ) O(m),总计 O ( n m ) O(nm)。 3. 环的处理与答案合并 枚举睡眠起始时间 j j( 1 ≤ j ≤ m 1≤j≤m): 对第 i i 个人,其对应的窗口结束位置为: ed i = j − b i (若 ed i ≤ 0 则需 + m ) ed i ​ =j−b i ​ (若 ed i ​ ≤0 则需 +m) 总吵闹值为所有人在位置 ed i ed i ​ 的最小值之和14。 答案更新:取所有 j j 对应的总吵闹值的最小值。 代码实现(C++cpp #include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN = 5010; const ll INF = 1e18; int n, m, k, a[MAXN], b[MAXN], c[MAXN][MAXN * 2]; // c[i] 开两倍空间 ll min_val[MAXN][MAXN * 2], ans = INF; // min_val[i][j] 为第 i 个人在位置 j 的窗口最小值 int main() { // 读入数据 cin >> n >> m >> k; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; if (k + b[i] > m) { // 无解条件 cout << -1; return 0; } } // 读入序列并倍长 for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> c[i][j]; c[i][j + m] = c[i][j]; // 环处理 } } // 单调队列预处理每个人的窗口最小值 for (int i = 1; i <= n; i++) { int len = m - k - b[i] + 1; // 窗口长度 deque<int> dq; // 双端队列存下标 for (int j = 1; j <= 2 * m; j++) { // 移除超出窗口的元素 while (!dq.empty() && dq.front() < j - len + 1) dq.pop_front(); // 移除队尾大于当前值的元素 while (!dq.empty() && c[i][dq.back()] >= c[i][j]) dq.pop_back(); dq.push_back(j); // 记录窗口最小值(从 len 开始有效) if (j >= len) min_val[i][j] = c[i][dq.front()]; } } // 枚举睡眠起始时间 j,求总吵闹值 for (int j = 1; j <= m; j++) { ll total = 0; for (int i = 1; i <= n; i++) { int ed = j - b[i]; // 计算第 i 个人的窗口结束位置 if (ed <= 0) ed += m; // 环调整 total += min_val[i][ed]; } ans = min(ans, total); } cout << ans; return 0; } 关键点分析 无解判断:当 k + b i > m k+b i ​ >m 时,直接输出 -1。 环的处理:序列倍长是通用技巧,避免模运算。 窗口位置计算: 第 i i 个人的窗口长度固定为 l e n i = m − k − b i + 1 len i ​ =m−k−b i ​ +1。 睡眠起始时间 j j 对应的结束位置为 j − b i j−b i ​ (需调整负数)。 单调队列核心操作: 队尾维护单调性(递增)。 队头维护窗口有效性。 此解法时间复杂度 O ( n m ) O(nm),空间复杂度 O ( n m ) O(nm),可通过全部测试点。练习时需注意输入数据规模(需快读)和边界条件14。 使用multiset重构代码
06-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值