C++:刷题输入

Ⅰ、规范

注意:输入是多组输入,不是一组

(1)输入预先不输入数据的组数

while(cin>>a>>b){
    cout<<a+b<<endl;
}

(2)预先知道数据组数

cin>>n;
for(int i=0; i<n; i++){
    int a,b;
    cin>>a>>b;
    cout<<a+b<<endl;
}

Ⅱ、相关输入语法

一、cin>>

用法1:输入一个数字或字符

#include <iostream>
using namespace std;
main ()
{
int a,b;
cin>>a>>b;
cout<<a+b<<endl;
}

用法2:接收一个字符串,遇“空格”、“TAB”、“回车”就结束

#include <iostream>
using namespace std;
main ()
{
char a[20];
cin>>a;
cout<<a<<endl;
}
//输入:jkljkljkl
//输出:jkljkljkl

//输入:jkljkl jkljkl       //遇空格结束
//输出:jkljkl

二、cin.getline()

用法:接收一个字符串,可以接收空格并输出

#include <iostream>
using namespace std;
main ()
{
char m[20];
cin.getline(m,5);
cout<<m<<endl;
}
//输入:jkljkljkl
//输出:jklj

//接收5个字符到m中,其中最后一个为'\0',所以只看到4个字符输出;

//如果把5改成20:
//输入:jkljkljkl
//输出:jkljkljkl

//输入:jklf fjlsjf fjsdklf
//输出:jklf fjlsjf fjsdklf

延伸:
1、cin.getline()实际上有三个参数,cin.getline(接收字符串的变量,接收字符个数,结束字符)
2、当第三个参数省略时,系统默认为’\0’
3、如果将例子中cin.getline()改为cin.getline(m,5,‘a’);当输入jlkjkljkl时输出jklj,输入jkaljkljkl时,输出jk

三、getline()

用法:接收一个字符串,可以接收空格并输出,需包含“#include”

#include<iostream>
#include<string>
using namespace std;
main ()
{
string str;
getline(cin,str);
cout<<str<<endl;
}
//输入:jkljkljkl
//输出:jkljkljkl

//输入:jkl jfksldfj jklsjfl
//输出:jkl jfksldfj jklsjfl

四、注意的问题

1、cin.getline()属于istream流,而getline()属于string流,是不一样的两个函数

2、当同时使用cin>>,getline()时,需要注意的是,在cin>>输入流完成之后,getline()之前,需要通过

str="\n";
getline(cin,str);

的方式将回车符作为输入流cin以清除缓存,如果不这样做的话,在控制台上就不会出现getline()的输入提示,而直接跳过,因为程序默认地将之前的变量作为输入流。

看下面程序:
正常运行版本:

/*-------------Basic Input/Output-------------*/
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
 
int main(){
    int age;
    //standard input(cin)
    cout<<"Please enter an integer value as your age: ";
    cin>>age;
    cout<<"Your ager is: "<<age<<".\n";
    //cin and string
    string mystr;
    cout<<"What's your name? "<<endl;
    mystr="\n";
    getline(cin,mystr);
 
    getline(cin,mystr);
    cout<<"Hello,"<<mystr<<".\n";
    char sex;
    cout<<"Please enter a F or M as your sex: ";
    cin>>sex;
    cout<<"Your sex is: "<<sex<<endl;
    cout<<"What's your favorite team? ";
    mystr="\n";
    getline(cin,mystr);
 
    getline(cin,mystr);
    cout<<"I like "<<mystr<<".\n";
 
    system("pause");
    return 0;
}
/*输出:
Please enter an integer value as your age: 16
Your ager is: 16.
What's your name?
jiaos
Hello,jiaos.
Please enter a F or M as your sex: F
Your sex is: F
What's your favorite team? programming
I like programming.
*/

非正常运行版本:

/*-------------Basic Input/Output-------------*/
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main() {
	int age;
	//standard input(cin)
	cout << "Please enter an integer value as your age: ";
	cin >> age;
	cout << "Your ager is: " << age << ".\n";
	//cin and string
	string mystr;
	cout << "What's your name? " << endl;
	/*mystr = "\n";
	getline(cin, mystr);*/

	getline(cin, mystr);
	cout << "Hello," << mystr << ".\n";
	char sex;
	cout << "Please enter a F or M as your sex: ";
	cin >> sex;
	cout << "Your sex is: " << sex << endl;
	cout << "What's your favorite team? ";
	/*mystr = "\n";
	getline(cin, mystr);*/

	getline(cin, mystr);
	cout << "I like " << mystr << ".\n";

	system("pause");
	return 0;
}
/*输出:
Please enter an integer value as your age: 16
Your ager is: 16.
What's your name?
Hello,.
Please enter a F or M as your sex: F
Your sex is: F
What's your favorite team? I like .
*/

Ⅲ ostringstream、istringstream、stringstream

C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含#include <sstream>头文件。

  • istringstream类用于执行C++风格的串流的输入操作。

  • ostringstream类用于执行C风格的串流的输出操作。

  • stringstream类同时可以支持C风格的串流的输入输出操作。

下图详细描述了几种类之间的继承关系:
在这里插入图片描述

  • istringstream是由一个string对象构造而来,从一个string对象读取字符。
  • ostringstream同样是有一个string对象构造而来,向一个string对象插入字符。
  • stringstream则是用于C++风格的字符串的输入输出的。

一、 istringstream 的用法

C++提供了一个类 istringstream ,其构造函数原形如下:

istringstream::istringstream(string str);

它的作用是从 string 对象 str 中读取字符。

1、例子1

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(){
	string s = "ab,cd,e,fg,h";
	int n = s.size();
	for (int i = 0; i < n; ++i){
		if (s[i] == ','){
			s[i] = ' ';
		}
	}
	istringstream out(s);
	string str;
	while (out >> str){
		cout << str <<' ';
	}
	cout << endl;
}

那么我们可以利用这个类来解决问题,方法如下:
第一步:接收字符串 s ;
第二步:遍历字符串 s ,把 s 中的逗号换成空格;
第三步:通过 istringstream 类重新读取字符串 s ;
注意, istringstream 这个类包含在库 < sstream > 中,所以头文件必须包含这个库。

1.1、结果

在这里插入图片描述

2、例子2

#include<iostream>
#include <sstream> 
using namespace std; 
int main() {
	string test = "-123 9.87 welcome to, 989, test!";
	istringstream iss;//istringstream提供读 string 的功能
	iss.str(test);//将 string 类型的 test 复制给 iss,返回 void 
	string s;
	cout << "按照空格读取字符串:" << endl;
	while (iss >> s) {
		cout << s << endl;//按空格读取string
	}
	cout << "*********************" << endl;

	istringstream strm(test);
	//创建存储 test 的副本的 stringstream 对象
	int i;
	float f;
	char c;
	char buff[1024];

	strm >> i;
	cout << "读取int类型:" << i << endl;

	strm >> f;
	cout << "读取float类型:" << f << endl;

	strm >> c;
	cout << "读取char类型:" << c << endl;


	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;

	string s1;
	strm >> s1;
	cout << "读取string类型1: " << s1 << endl;

	int j;
	strm >> j;
	cout << "读取int类型:" << j << endl;

	strm.ignore(100, ',');
	string s2;
	strm >> s2;
	cout << "读取string类型2:" << s2 << endl;

	system("pause");
	return 0;
}

2.1、结果

在这里插入图片描述

2.2、注意

1)在istringstream类中,构造字符串流时,空格会成为字符串参数的内部分界
2)istringstream类可以用作string与各种类型的转换途径
3)ignore函数参数:需要读取字符串的最大长度,需要忽略的字符

cin.ignore(int intExp, char chExp);

其中intExp 是一个整型表达式,也可以是一个整型数值,这个数值表示在一行中忽略的字符的最大数目,比如说intExp=100;还有一个参数chExp,是一个字符表达式。表示如果遇到一个字符值等于chEXP,那么就停止ignore(),如果ignore100个字符之后还没遇到值等于chEXP的字符,那也得停止ignore(),所以100是ignore()所忽略的最大字符数。
例子:

#include<iostream>
#include<cstdlib>
using namespace std; 

int main()
{
  int ival1 = 0, ival2 = 0;
  std::cin >> ival1;
  std::cin.ignore(100, '\n');
  std::cin >> ival2;
  std::cout << "ival1 = " << ival1 << std::endl;
  std::cout << "ival2 = " << ival2 << std::endl;
  system("pause");
  return 0;
}

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

二、 ostringstream 的用法

1、例子

#include<iostream>
#include <sstream> 
using namespace std; 

int main() {
	ostringstream out;
	out.put('t');//插入字符
	out.put('e');
	out << "st"<<endl;
	//cout << out << endl;错误用法
	string res = out.str();//提取字符串;
	cout << res << endl;
	system("pause");
	return 0;
}

2、结果

在这里插入图片描述
注:如果一开始初始化ostringstream,例如ostringstream out(“abcd”),那么之后put或者<<时的字符串会覆盖原来的字符,超过的部分在原始基础上增加。
例子

#include<iostream>
#include <sstream> 
using namespace std; 

int main() {
	ostringstream out("abcd");
	out.put('t');//插入字符
	out.put('e');
	out << "st"<<endl;
	//cout << out << endl;错误用法
	string res = out.str();//提取字符串;
	cout << res << endl;
	system("pause");
	return 0;
}

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

三、 stringstream 的用法

stringstream同理,三类都可以用来字符串和不同类型转换。
之后找到好资料再整理。

Ⅳ、血泪实战

例子一:读取列表格式数据

  • 输入:
[2,3,1,1,4]
  • 代码
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

int main() {

	vector<int> array; 
	string s; 
	getline(cin, s); 
	istringstream is(s.substr(1)); 
	int inter;
	char ch; 
	while (is >> inter)
	{
		array.push_back(inter);
		is >> ch;
	}
	for (auto x:array)
	{
		cout << x << " ";
	}

	system("pause");
	return 0;
}
  • 输出:
    在这里插入图片描述

例子二:读取定行不定数目

  • 输入:
1 2 3 4
1 1
  • 代码:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

int main() {

	vector<int>  yuangong;
	vector<int>  renwu;

	string str;
	int num;
	getline(cin, str);
	istringstream ist(str);
	while (ist>>num)
	{
		yuangong.push_back(num);
	}
//可以共用一个num和str
	getline(cin, str);
	istringstream ist1(str);
	while (ist1 >> num)
	{
		renwu.push_back(num);
	}
	
	cout << "输出如下:" << endl;
	for (auto a:yuangong)
	{
		cout << a << " ";
	}
	cout << endl;
	for (auto a : renwu)
	{
		cout << a << " ";
	}
	
    system("pause");
	return 0;
}
  • 输出
    在这里插入图片描述

例子三:读取不定行数不定数目的数据

方法一:利用istringstream遇到空格停顿的特性

  • 代码:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    string str;
    int num;

    while (getline(cin, str))//读取输入的一行数据
    {
        if (str.size() == 0) break;//如果读取的是空,则读取结束
        istringstream ist(str);//把读取的str送入流中
        while (ist >> num) cout << num << endl;//逐个读取流中的int并打印
    }


    system("pause");
    return 0;
}
  • 输出:
    在这里插入图片描述
1 2 3 4(回车)
1
2
3
4
222(回车)
222
(回车)
请按任意键继续. . .

方法二:利用getline(待分割字符串流,承接字符串,分割符)

  • 代码
#include <iostream>
#include <string>
#include <sstream>
#include<vector>

using namespace std;

int main()
{
	string str;
	vector<vector<string>> num;
	int i = 0;//标记行数

	while (getline(cin, str))//读取输入的一行数据
	{
		if (str.size() == 0) break;//如果读取的是空,则读取结束

		istringstream ist(str);//把读取的str送入流中
		string temp;//接收分割的字符串
		vector<string>   smallnum;
		while (getline(ist, temp, ' '))//将ist按' '分割,放到temp,之后循环放入一维vector
		{
			smallnum.push_back(temp);
		}

		num.push_back(smallnum);//将一维vector放入二维数组
	}

	for (auto a:num)
	{
		for (auto y:a)
		{
			cout << y << " ";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}
  • 输出
    在这里插入图片描述
1 2 3 4 5
3 4 5
2 3
3 4 6 88 4 2 5 7 84 2
33333333333333(回车)
(回车)
1 2 3 4 5
3 4 5
2 3
3 4 6 88 4 2 5 7 84 2
33333333333333

参考博客:
参考1
参考2
参考3
参考4
参考5
侵删

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值