I know this question has been asked a lot, and I have gone through the most popular answer on stackoverflow.
I have a bunch of numbers in the following format:
4 // number of testcases
1 2 3 4 5 6 // Each line contains numbers for that particular testcase
1 4 5 6 7 8 9 19
12 3 5
1 4 9
The first line is the number of testcases. Each line following that contains numbers for that particular testcase.
I want to read line by line, process the numbers, output the solution and only then move to read to the second line.
In another stackoverflow question, the suggested solution is leading to a lot of duplication of data in the vector all_integers.
std::string line;
std::vector< std::vector<int> > all_integers;
while ( getline( std::cin, line ) ) {
std::istringstream is( line );
all_integers.push_back(
std::vector<int>( std::istream_iterator<int>(is),
std::istream_iterator<int>() ) );
for(auto it = all_integers.begin();it!=all_integers.end();it++) {
for(auto j = it->begin();j!=it->end();j++) {
cout << *j << " " ;
}
cout << endl;
}
What is the correct way of reading this from a file?
Follow up question:
In another file, I have input in the following format:
5 2
abc
bcd
eee
zyc
abv
abc bcd
zyc anv
The first line contains two numbers: m,n.
m : Number of words in the dictionary n: Number of queries.
Then m number of words follow and last n lines read the number of queries that are to be made to it.
How can I read this efficiently? Is there any way to read the last K lines of a file efficiently in C++?