stringstream的用法与例题(牛客“次佛锅”)

先看下例题吧:

https://ac.nowcoder.com/acm/contest/30532/Chttps://ac.nowcoder.com/acm/contest/30532/C

 

 如果你不会stringstream,可能就会想办法读入一行字符串,之后遇到空格就分类,没错,我就是这样写的,WA了一个半小时(水平不够吧),例如:

#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
using namespace std;
const int N=5e5+10;
string s;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
char aa[25],ab[15];
int main()
{
	IOS;
    getline(cin,s);
    ll n=s.length();
    ll tem=0,temp=0;
    map<string,long long>mp;
    for(ll i=0;i<n;i++)
    {	
		//cout<<s[i]<<endl; 
        if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))
		    aa[tem++]=s[i];
		//cout<<aa<<endl;
		if(s[i]==' ') continue;
		if((s[i]>='0'&&s[i]<='9'))
    	{
    		ll num=0;
    		while((s[i]>='0'&&s[i]<='9'))
    		{
    			ab[temp++]=s[i];
    			i++;
			}
			for(int i=0;i<temp;i++)
			{
				num+=(ab[i]-'0')*pow(10,temp-i-1);
			}
    		//cout<<"num "<<num<<endl;
			//cout<<aa<<"aa"<<endl;
            //cout<<mp[aa]<<endl;
    		mp[aa]+=num;
    		//cout<<mp[aa]<<endl;
    		tem=0;
    		temp=0;
    		memset(ab,0,15);
    		memset(aa,0,25);
		}
		
	}
	ll t;
	cin>>t;
	while(t--)
	{
		char ss[25];
		cin>>ss;
		//ll flag=strlen(ss);
        //cout<<ss<<"bb"<<endl;
		cout<<mp[ss]<<endl;
	}
    return 0;
}

但是你要是会stringstream,这题就是随随便便简简单单的AC了,例如:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using namespace std;
const int N=5e5+10;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int main()
{
	IOS; 
	string s,t,a;
	getline(cin,s);
	stringstream stream;
	stream<<s;
	while(stream>>t)
	{
		cout<<t<<endl;
		stream>>a;
		cout<<a<<endl;
	}
	return 0;
}

那么来说一下stringstream的简单用法

1.头文件:<sstream>

2.stringstream是负责输入和输出操作的

3.作用:

        3.1.用于数据转化:这里举一个例子(整型转字符串型)

#include <string>
#include <sstream>
#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long ll;
using namespace std;
int main()
{
	ll a=1000;//定义一个整型变量 
	stringstream s;//定义一个stringstream变量 
	s<<a;//将整型的值输入给stringstream 
	string ans;//这里要定义一个字符串型来接收stringstream的传出的值 
	s>>ans;//stringstream的值传出给字符串型 
	cout<<"stringstream: "<<ans<<endl;//输出的答案为 stringstream: 1000 
	return 0;
}

        3.2.用于读入多字符串

#include <string>
#include <sstream>
#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long ll;
using namespace std;
int main()
{
	stringstream s;//定义一个stringstream变量 
	s<<"first string "<<" "<<"is: ";
	s<<"you "<<"are a idiot!";
	cout<<s.str()<<endl;//输出的答案为first string  is: you are a idiot! 
	return 0;
}

3.3用于分隔读入的一行字符串(如例题)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using namespace std;
const int N=5e5+10;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int main()
{
	IOS; 
	string s,t;
	//这里我们保证一下输入的一行字符串是一个字符串+空格+数字+一个字符串... 
	getline(cin,s);//输入一行含多类型的字符串 
	stringstream stream; //定义一个stringstream方便读入与输出 
	stream<<s; //把读入的一行多类型字符串赋给stream 
	while(stream>>t)//遇到一个字符串循环一次 
	{
		cout<<t<<endl;// 输出一个字符串 
		ll a;//定义一个整型变量存数字 
		stream>>a;//下一个stream的值给a 
		cout<<a<<endl;//输出这个数 
	}   
	return 0;
}

4.stringstream的清空:

        4.1.s.str("");

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using namespace std;
const int N=5e5+10;
int main()
{
	stringstream s;//定义一个stringstream变量 
	s<<"first string "<<" "<<"is: ";
	s<<"you "<<"are a idiot!";
	cout<<s.str()<<endl;//输出的答案为first string  is: you are a idiot! 
    // 清空 sstream
    s.str("");
    s<<"new";
    cout<<s.str()<<endl;//输出的答案为new
	return 0;
}

        4.2.clear() ;

#include <sstream>
#include <iostream>
using namespace std;
 int main()
{
    stringstream s;
    int a,b;
    s<<"6666";//插入了字符串 
    s>>a;//转化为整型 
    cout<<a<<endl;
    //在进行多次类型转换前,必须先运行clear()
    s.clear();
    //插入bool值
    s<<false;//相当于零
    // 转换为int类型
    s>>b;
    cout<<b<<endl;
    return 0;
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值