read efficiently in c++
Moderator: Board moderators
read efficiently in c++
hello guys ! what is the best way to read a line from input and split that string into tokens by a delimiter like space ?
Re: read efficiently in c++
I don't know the best way, but one of the ways is to use the getline function in c++ (http://www.cplusplus.com/reference/stri ... g/getline/).
Once you have read the line in a string, you can split it by passing that string to a stringstream and then using the extract ( >> ) operator (http://www.cplusplus.com/reference/istr ... perator>>/
I have attached a code that reads lines of numbers from the input and then ouputs them one by one. Just a demo.
Once you have read the line in a string, you can split it by passing that string to a stringstream and then using the extract ( >> ) operator (http://www.cplusplus.com/reference/istr ... perator>>/
I have attached a code that reads lines of numbers from the input and then ouputs them one by one. Just a demo.
Code: Select all
#include <bits/stdc++.h>
using namespace std;
int main(void) {
string line;
while (getline(cin, line)) {
stringstream ss(line);
int n;
while (ss >> n) {
cout << n << endl;
}
}
return 0;
}