Created
September 28, 2014 05:45
-
-
Save yangl1996/1f85361ec17002cd9f4f to your computer and use it in GitHub Desktop.
auto split the input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the input is provided in one line, containing commas and integrals. the following code split the input and read them into an array. | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
using namespace std; | |
int main() | |
{ | |
string input; | |
cin >> input; | |
replace(input.begin(), input.end(), ',', ' '); | |
stringstream ss; | |
ss.str(input); | |
int data[1000]; | |
int count = 0; | |
string temp; | |
while (ss >> data[count]) | |
{ | |
count++; | |
} | |
// above is the reading and storing section | |
for (int i = 0; i < count; i++) | |
{ | |
cout << data[i]; | |
} | |
// output | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
没有必要这么复杂。直接用一个char存符号,之后使用cin >> a >> char >> b即可。