XuPengbo
白玉不毁,孰为珪璋

程序设计思维与实践 复习随笔(一)

输入输出知识归纳总结

利用 stringstream 分割字符串

//方法1
include<sstream>      //头文件
using namespace std;
stringstream ss;
string str, st;
st = "abc,def,gh";
ss << st;
while(getline(ss, str, ','))
      cout << str << endl;
/* output:
abc
def
gh

*/
//方法2
//写进函数中,在其他函数中调用
vector<string> split(string str,char op)//把str字符串按op字符分割开
{
     vector<string> v;
     stringstream ss;
     ss<<str;
     string s;
     while(getline(ss,s,op)){
       v.push_back(s);
     }
     return v;
}

字符串的特定规则输入

//输入字符串(带空格)
#include <iostream>
#include <cstring>
using namespace std;
int main() {
	int n;string str;
	cin>>n;
	cin.ignore();
	for(int i=0;i<n;++i){
    	getline(cin,str);
    	cout<<str<<endl;
	} 
  return 0;
}
//遇到字母停止读入
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
	char str[20];
	scanf("%[^a-zA-Z]",str); 
	printf("%s\n",str);
  return 0;
}
posted @ 2020-06-26 10:46  XuPengbo  阅读(36)  评论(0)    收藏  举报