C++ string(主要介绍sstream库进行字串分割)、char数组的知识点整合

ofstream、ifstream、fstream 文件操作流

ofstream

往文件中写入东西

ifstream

从文件中读出东西

fstream

查询某个文件

sstream 串流

主要组成

istringstream
ostringstream
stringstream

头文件sstream库,即#include<sstream>

最方便用的一个点:
个人认为是对字符串流的快速提取

#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	string st;
	getline(cin,st);
	istringstream in(st);
	string temstr;
	while(in>>temstr){    //将得到的字符串碎布逐个赋给temstr
		cout<<temstr<<endl;    //或者进行其他的处理或者存储操作
	}
	return 0;
}

运行结果
在这里插入图片描述
可以说是可以自动分割输入中的非空格区间,减少了对字串读取的时间(而且范围广,无论是alpha 还是digit 或是符号都可以)

其次还有比较好的点就是ostringstream对子串的改变

主要应用的是put()函数,注意括号中的参数是字符,用单引号(’ ');
put()函数按位数依次替换。

#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	ostringstream out("12       34 56");
	out.put('2');    //2222222  34 56 代替作用
	out.put('2');
	out.put('2');
	out.put('2');
	out.put('2');
	out.put('2');
	out.put('2');
	cout<<out.str()<<endl;
	return 0;
}

char[]搭配scanf()的相关使用

(先补充一点c++的getline知识,如果连着两次getline=>应该加fflush(stdin);来清空文件缓存区的东西,不然输入会出问题)

char[]使用中对于字串的输入

char st[1050];
scanf("%s",&st);
//这样就可以输入一个字串了

//还可以通过getlen(st)来得到长度
cout<<getlen(st)<<endl;

//或者通过getchar()函数来自动添加空格。让字串之间自动添加空格来便于读取
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值