C++获取屏幕输入

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("%[^
]",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;
}

  

原文地址:https://www.cnblogs.com/yyh1993/p/7500624.html