getline使用

阅读 60

2022-04-05

getline可以读入整行,以回车作为结尾,故如果输入形式是一个整数n,之后n行含有空格的串,要先进行getline(cin,s);读取完整数n后的空格。

头文件是sstream,string

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
map<string,int>m;
int main()
{
	getline(cin,s);
	string str;
	stringstream newcin(s); 
	//以空格为分隔,隔出每个string
	while (newcin>>str){
		m[str]++;
	}
	cout<<m.size();
	return 0;
}

精彩评论(0)

0 0 举报