The purpose of istringstream: split the string with spaces as separators
Implementation:
#include <iostream> #include <sstream>//Header files required by istringstream using namespace std; intmain() { string s,ss;//s is the input string ss used to store the separated string while(getline(cin,s))//Multiple groups of input strings with spaces, receive a line of strings from the terminal, and put them into the string s { istringstream stream(s);//Put s into the stream stream while(stream>>ss)//Read one word at a time (bounded by spaces) and store it in ss cout<<ss<<endl;//Output the string stored in ss this time } return 0; }
For example, the input is: zhang yi xin
Then the output is: zhang
yi
ask for
example:
sort
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 69353 Accepted Submission(s): 21356
Problem Description
Enter a line of numbers, if we regard the '5' in this line of numbers as spaces, then we get a line of non-negative integers separated by spaces (maybe some integers start with '0', the '0' in these headers should be It is ignored, unless the integer is composed of several '0's, in which case the integer is 0).
Your task is to sort and output the integers obtained from these divisions in ascending order.
Your task is to sort and output the integers obtained from these divisions in ascending order.
Input
The input contains multiple sets of test cases, each set of input data has only one line of numbers (no spaces between numbers), and the length of this line of numbers is not greater than 1000.
Input data guarantee: the non-negative integer obtained by division will not be greater than 100000000; the input data cannot be all composed of '5'.
Input data guarantee: the non-negative integer obtained by division will not be greater than 100000000; the input data cannot be all composed of '5'.
Output
For each test case, output the result of integer sorting obtained by division, separate two adjacent integers with a space, and each group of output occupies one line.
Sample Input
0051231232050775
Sample Output
0 77 12312320
Source
AC code:
#include <iostream> #include <sstream> #include <vector> //Unordered allows duplicate elements #include <algorithm> using namespace std; intmain() { string s; while(cin>>s) { for(int i=0;i!=s.size();i++) { if(s[i]=='5') s[i]=' '; } istringstream in(s); //Split the header file sstream with spaces as separators vector <int> v; string ss; while(in>>ss) { int a; //c_str(): Generate a const char* pointer to a null-terminated array //The c_str() function returns a constant pointer to a regular C string with the same content as this ss string. a=atof(ss.c_str()); //Convert string to number atof header file algorithm v.push_back(a); } sort(v.begin(),v.end()); int i; for(i=0;i<v.size()-1;i++) cout<<v[i]<<' '; cout<<v[i]<<endl; } return 0; }