0

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++?

Community
  • 1
  • 1
Piyush
  • 565
  • 14
  • 32
  • 1
    Unless the questions are related, one question per stackoverflow question, please. Additionally, "The first line is the number of testcases and each line has inputs for that usecase" makes no sense. The shown sample data has "10" on the first line. How that relates to all the data that follows, is unclear. The second line has six values, and four lines over follow the first line. – Sam Varshavchik Sep 14 '16 at 23:35
  • do you want to read numbers as: 1 is a number and 2 is another or all in one single line? 1 2 3 4 5 6 = 123456? – Raindrop7 Sep 14 '16 at 23:35
  • @Raindrop7: Read each number and push it to a vector. – Piyush Sep 14 '16 at 23:37
  • just read any line as string then remove spaces then convert into integer then push to a vector/array – Raindrop7 Sep 14 '16 at 23:38
  • @RawN What is unclear about the question? There are some values in the file and I want to process them. I know how to process them but I am not sure about the most efficient way of reading them. I have 10k+ lines in the file and it doesn't make sense to enter them individually on std input – Piyush Sep 14 '16 at 23:39
  • 2
    There's no such thing as "the most efficient way of reading them" that's true for every application in the world. What's efficient for one application, that uses the read data for one particular purpose is not going to be very efficient for another application that does something else with them. It is your responsibility to analyze your application's requirements, and what it needs to do with the read data, then pick the best way to read it. – Sam Varshavchik Sep 15 '16 at 00:42

0 Answers0