牛客联系练习:oj常见输入输出练习
参考:
目录
int 型输入
1. 输入两个整数,空格区分数字,换行区分组
- 利用while() 循环处理多个case
- 在普通的cin输入时,遇到“空格”、“tab”会和“换行”一样,结束输入。
代码:
#include <iostream>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b) { // 注意 while 处理多个 case
cout << a + b << endl;
}
}
2. 输入数据多组(不定), 每行输入一组不定长数据。
- while() 处理每一组输入的输出
- cin.get() 通过判断输入是否为 ‘\n’,检测多组输入。
#include <iostream>
using namespace std;
int main() {
int a, sum = 0;
while (cin >> a) { // 注意 while 处理多个 case
sum += a;
if(cin.get() == '\n') { // 对每一行数据进行处理
cout << sum << endl;
sum = 0;
}
}
return 0;
}
String 类型输入
1. 不定行,不定长的字符串输入(空格相隔)
- while() 处理每一组输入的输出
- cin.get() 通过判断输入是否为 ‘\n’,检测多组输入。
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int a, b;
string s;
vector<string> st;
while (cin >> s) { // 注意 while 处理多个 case
st.push_back(s);
if(cin.get() == '\n') {
sort(st.begin(), st.end());
for(int i = 0; i < st.size() - 1; i++) {
cout << st[i] <<' ';
}
cout << st[st.size() - 1] << endl;
st.clear();
}
}
return 0;
}
2. 不定行,不定长的字符串输入(逗号’,'相隔)
- stringstream类初始化对象,字符串流
- getline(输入流,字符串,终止符),将逗号作为终止符,不断读取流输出的数据
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
int main() {
vector<string> st;
string s;
while(cin >> s) {
stringstream ss(s); // stringstream类初始化对象,字符串流
string s1;
while(getline(ss, s1, ',')) { // 利用getline按照逗号分隔读入流输入
st.push_back(s1);
}
sort(st.begin(), st.end());
for(int i = 0; i < st.size() - 1; i++) {
cout<< st[i] << ',';
}
cout<< st[st.size() - 1] << endl;
st.clear();
}
}
总结
1. while(cin >> a) 输入
while循环条件:输入运算符>>,它负责将数据从cin读到a并返回cin(<<和>>的调用结果都是返回其左侧运算对象)。为了对条件求值,cin被istream operator bool类型转换函数隐式地执行了转换,成为bool类型。如果cin的条件状态是good,则该函数返回为真,否则返回为假。
在循环输入过程中,cin在遇到文件结束符,或者遇到无效输入时(输入非整数),cin的状态会变成无效,退出循环。
2. cin.get() 的使用
- cin.get()是针对char类型的,如果想得到int等其他的类型需要进行类型转化.
#include <iostream>
using namespace std;
int main() {
// 1. 获取输入的第一个字符
char ch;
ch = cin.get(); // 或者cin.get(ch)
cout<< ch; // 若输入abcd 则输出a
// 2. 获取一组字符串
// cin.get(数组,长度)
char arr[20];
cin.get(arr, 20); // 可以输入空格
cout << arr; // 注意这里只会输出19个元素(最后一个为'\0'终止符)
// 3. cin.get()用于舍弃输入流的不需要的字符
char a[10];
char b[10];
cin.get(a,10);
cin.get(); //去除a输入完毕后的回车
//如果不加,a输入后回车就不能输入b,程序就结束了
cin.get(b,10);
cout << a << " " << b;
}
- 如果不加cin.get(),输入a后回车是无法输入b的,程序就输出结束了。或者你输入a后空格再输入b,a是从行头开始取,取满才会到b。
- 与cin.getline()不一样,cin.getline(是丢弃行尾的换行符的)
3. getline() 的使用
- 遇到以下情况会导致生成的字符串结束:
(1)到文件结束符,(2)遇到函数的定界符,(3)输入达到最大限度。
读取字符串
在的getline函数有两种重载形式:
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
#include<iostream>
using namespace std;
int main()
{
char s1[100],s2[100];
//输入的数据:aa,bb,cc
//第一种重载
cin.getline(s1,100);
cout<<s1<<endl;//输出aa,bb,cc
//第二种重载
cin.getline(s2,100,','); // 逗号作为终止标志符
cout<<s2<<endl;//输出aa
cin.getline(s2,100,',');
cout<<s2<<endl;//bb
return 0;
}
读取字符流
在中的getline函数有四种重载形式:
istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
//输入aa,bb,cc
getline(cin,str);
cout<<str<<endl;//aa,bb,cc
getline(cin, str, ',');
cout<<str<<endl;//aa
return 0;
}
4. stringstream流输入输出
字符串流(stringstream)是一种特殊的流类,它允许将字符串作为输入和输出流进行处理。
(1)数据类型转换
- 字符串转化为其他类型
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string str = "12345";
stringstream object(str); // 创建一个对象,并调用构造函数进行初始化
int num;
ss >> num;
cout << num << std::endl; // 输出 12345
return 0;
}
- 其他数据类型转化为字符串
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int num = 12345;
stringstream object;
object << num; // 将整数流向对象
string str = object.str(); // str()函数可以获取流中的字符串
cout << str << std::endl; // 输出 "12345"
return 0;
}
(2)字符串分割 / 连接
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
// eg: IP地址转换
stringstream IP("ip地址: 192.168.1.1");
char info[20];
int ipnum[4];
IP >> info; // 流入:“iP地址: ”,(流向以' '空格作为暂停)
for(int i = 0; i < 4; i++) {
char key; // 识别.
IP >> ipnum[i];
if(i != 3) {
ip >> key; // 流出.
}
}
for(int i = 0; i < 4; i++) {
cout << ipnum[i]<< "\t";
}
}
- 连接多个字符
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
stringstream ss;
ss << "one" << " two" << " three" << " four";
string str = ss.str();
cout << str << std::endl; // 输出 "one two three four"
return 0;
}
(3)和getline函数结合读取一整行
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string str = "one,two,three,four";
stringstream ss(str);
string line;
while (getline(ss, line,',')) { // 如果字符串之间非逗号,是\n,不需要添加终止符,getline遇到换行自动停止
cout << line <<endl;
}
return 0;
}
(4)使用peek函数查看下一个字符
- 指针仍停留在当前位置,并不后移。其功能是从输入流中读取一个字符,但该字符并未从输入流中删除。
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string str = "12345";
stringstream ss(str);
char ch = ss.peek();
cout << ch << endl; // 输出 '1' ,类似queue.front()
int num;
ss >> num;
cout << num << endl; // 输出 12345
return 0;
}