您的位置:首页 > 编程语言 > C语言/C++

C++获取屏幕输入

2017-09-10 11:43 357 查看
1. c++获取屏幕上一行数字的输入,以任意字符分隔,以回车结束(不需要提前知道有多少数据)

#include<iostream>
#include<vector>
#include<sstream>
#include<stdlib.h>
using namespace std;

void split(const  string& s, const string& delim,vector<string> &elems)
{
size_t pos = 0;
size_t len = s.length();
size_t delim_len = delim.length();
if (delim_len == 0) return;
while (pos < len)
{
int find_pos = s.find(delim, pos);
if (find_pos < 0)
{
elems.push_back(s.substr(pos, len - pos));
break;
}
elems.push_back(s.substr(pos, find_pos - pos));
pos = find_pos + delim_len;
}
return;
}

int main() {
char a[4096];
scanf("%[^\n]",a);
string s(a);
vector<string> s_s;
split(s," ",s_s);
for(int i=0;i<s_s.size();i++)
cout<<s_s[i]<<endl;
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: