栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++中使用stringstream与getline处理一行被空格(逗号)隔开的数据

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++中使用stringstream与getline处理一行被空格(逗号)隔开的数据

要求:
处理一行以空格隔开的整数,例如用数组存放一行数据:1 2 3 4 5 6 7 8 9
处理一行以逗号隔开的整数,例如用数组存放一行数据:1,2,3,4,5,6,7,8,9

下面先简单介绍stringstream和getline函数的用法,然后再根据要求写代码。

stringstream用法

sstream库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。

1.使用stringstream将int类型转换为string类型

cpp代码:

#include 
#include 
#include 
#include 	//stringstream 的头文件

using namespace std;

// 使用stringstream将int类型转换为string类型
void IntToString() {
	stringstream sstream;
	string strResult;
	int nValue = 1000;

	// 将int类型的值放入输入流中
	sstream << nValue;

	// 从sstream中抽取前面插入的int类型的值,赋给string
	sstream >> strResult;

	cout << "[cout]strResult is: " << strResult << endl;
	printf("[printf]strResult is: %sn", strResult.c_str());
}

int main() {
	IntToString();
	return 0;
}

结果:

2.使用stringstream实现多个字符串的拼接

cpp代码:

#include 
#include 
#include 
#include 

using namespace std;

// 使用stringstream实现多个字符串的拼接
void strCat() {
	stringstream sstream;

	// 将多个字符串放入sstream中
	sstream << "first" << " " << "string,";
	sstream << " second string";
	cout << "strResult is: " << sstream.str() << endl;

	// 清空 sstream
	sstream.str("");	//清空操作
	sstream << "third string" << " " << "helloWorld";
	cout << "After clear,strResult is: " << sstream.str() << endl;
}

int main() {
	strCat();
	return 0;
}

结果:

getline函数的用法 1.使用getline函数读取一行数据

cpp代码:

#include 
#include 
#include 
#include 

using namespace std;

//使用getline函数读取一行数据到字符串str中
void getLine() {
	string str;
	cout << "输入一行数据:" << endl;
	getline(cin, str);	//读取终端输入的一行数据到字符串str中
	cout << "str : " << str << endl;
}

int main() {
	getLine();
	return 0;
}

结果:

2.getline函数与cin的对比

如果把上面的代码使用cin作为输入的话,则不能达到想要的效果
代码如下

#include 
#include 
#include 
#include 

using namespace std;

//使用getline函数读取一行数据到字符串str中
void getLine() {
	string str;
	cout << "输入一行数据:" << endl;
	//getline(cin, str);	//读取终端输入的一行数据到字符串str中
	cin>>str;
	cout << "str : " << str << endl;
}

int main() {
	getLine();
	return 0;
}

结果:

可以发现,输入的一行数据:1 2 3 4 5 6 7 8 9
之后,打印的 str = 1
这是因为当 cin 读取数据时,一旦它接触到第一个非空格字符即开始阅读,当它读取到下一个空格时,它将停止读取。
也就是说,cin不能读取空格,遇到空格就结束。所以这就体现了getline函数的优势。

stringstream与getline函数的配合使用 求一行被空格隔开的整数的和

代码:

#include 
#include 
#include 
#include 

using namespace std;

// 求一行被空格隔开的整数的和
void Sum() {
	string line;
	cout << "输入一行数据: " << endl;

	getline(cin, line);	//getline函数将输入的一行数据用字符串line接收

	int sum = 0, x;
	stringstream ss(line);//将字符串line放入到输入输出流ss中
	cout <<"输入输出流中的数据: "<< ss.str() << endl;

	while (ss >> x) sum += x;//求和

	cout <<"和为: "<< sum << endl;
}

int main() {
	Sum();
	return 0;
}

结果:

将一行以逗号分割的整数存放到数组中

代码:

#include 
#include 
#include 
#include 
#include 

using namespace std;

// 将一行以逗号分割的整数存放到数组中
void test() {
	vector ve;
	string word;
	//因为输入是一行以逗号分割的字符串,所以这里也可以使用cin
	//cin >> word;//输入:1,2,3,4,5,6,7,8,9
	getline(cin, word);//输入一行字符串,用word接收
	cout << "word: " << word << endl;
	string str;
	stringstream ss(word);//将输入的字符串放入到流ss中
	while (getline(ss, str, ',')) {//使用getline函数从流中读取字符串到str中,并以','作为分割,即遇到一个','函数返回
		ve.push_back(stoi(str));	//将读取到的字符串先进行类型转换,再添加到数组中
	}

	// 清空 ss
	ss.clear();
	cout << "打印数组: " << endl;
	for (auto x : ve) {
		cout << x << " ";//1 2 3 4 5 6 7 8 9
	}
	cout << endl;
}

int main() {
	test();
	return 0;
}

结果:

将一行以空格分割的整数存放到数组中

代码:

#include 
#include 
#include 
#include 
#include 

using namespace std;

// 将一行以逗号分割的整数存放到数组中
void test() {
	vector ve;
	string word;
	//因为输入是一行以空格分割的字符串,所以这里不能使用cin
	//cin >> word;//输入:1 2 3 4 5 6 7 8 9
	getline(cin, word);//输入一行字符串,用word接收
	cout << "word: " << word << endl;
	string str;
	stringstream ss(word);//将输入的字符串放入到流ss中
	while (getline(ss, str, ' ')) {//使用getline函数从流中读取字符串到str中,并以' '作为分割,即遇到一个' '函数返回
		ve.push_back(stoi(str));	//将读取到的字符串先进行类型转换,再添加到数组中
	}

	// 清空 ss
	ss.clear();
	cout << "打印数组: " << endl;
	for (auto x : ve) {
		cout << x << " ";//1 2 3 4 5 6 7 8 9
	}
	cout << endl;
}

int main() {
	test();
	return 0;
}

结果:

参考:

C++编程语言中stringstream类介绍
c++ cin整数以,(逗号)分割读取

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/951657.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号