0

I have a vector and a .txt file with numbers. There is a "\n" at the end of each line. The text file looks like this:

Example:

2 10 529 680 817 865 406 422 827 517 727 667 
4 8 722 682 965 22 341 65 663 687 
6 6 874 416 903 125 942 746 
8 9 424 269 532 807 319 908 603 308 482 
10 10 631 137 557 115 810 294 85 291 997 153 
12 10 249 346 709 115 492 440 713 132 959 723 
14 9 53 270 996 424 239 480 919 867 660 
16 10 634 463 487 197 23 159 147 393 38 926 

I need a really fast way to input the data and fill the vector with it, as I am participating in a competition. The vector after filling it would look like this:

vector<vector<int> > students{
{2, 10, 529, 680, 817, 865, 406, 422, 827, 517, 727, 667 }, 
{4, 8, 722, 682, 965, 22, 341, 65, 663, 687 }, 
{6, 6, 874, 416, 903, 125, 942, 746 }, 
{8, 9, 424, 269, 532, 807, 319, 908, 603, 308, 482 }, 
{10, 10, 631, 137, 557, 115, 810, 294, 85, 291, 997, 153}, 
{12, 10, 249, 346, 709, 115, 492, 440, 713, 132, 959, 723 }, 
{14, 9, 53, 270, 996, 424, 239, 480, 919, 867, 660 }, 
{16, 10, 634, 463, 487, 197, 23, 159, 147, 393, 38, 926}
 };

I have tried using mmap but with no success... Thanks in advance.

P.S: I am a rookie so please so some understanding if I don't get what you say.

Edit: This is the code I'm using right now:


for (i = 0; i < numOfStuds; i++)
{
   fscanf(input, "%d %d ", &grade, &univs);
   students[i].push_back(grade);
   backup.push_back(grade);
   students[i].push_back(univs);
   for (j = 0; j < univs; j++)
   {
      fscanf(input, "%d ", &temp);
      students[i].push_back(temp);
   }

}
Orestis
  • 1
  • 1
  • So you want to assign those numbers to a vector array as you showed? – Yunus Temurlenk Jan 17 '20 at 17:12
  • @LeoE I'm not asking you to write my code, the whole point of the first two phases is to find information online – Orestis Jan 17 '20 at 18:07
  • @YunusTemurlenk Yeah – Orestis Jan 17 '20 at 18:07
  • 1
    ***I have tried using mmap but with no success*** Maybe you should show what you tried. And also show some release mode timings so someone can replicate the situation. – drescherjm Jan 17 '20 at 18:37
  • Maybe reading the file the fastest is not even the problem. If this is competitive programming, I would say its likely that your file reading is not the source of a TLE. Many new users try lots of tricks to improve file reading speed but then their algorithm that processes the data is O(n^3) instead of O(n log n) and that is the real reason for a TLE – drescherjm Jan 17 '20 at 18:40
  • @drescherjm I have timed every part of my program and 80% is when reading – Orestis Jan 17 '20 at 19:00
  • @drescherjm It is fundamentally flawed, there was no reason to show you what I did when I didn't know how to use it correctly. Even if you had it, you would completely ignore everything I had written – Orestis Jan 17 '20 at 19:02
  • 1
    You make your question difficult to get the answer you want when there is nothing to compare. People answer and then you complain that their answer did not help or is slower. However they have no way to compare their solution to yours. – drescherjm Jan 17 '20 at 19:03
  • @drescherjm in my current code the second number in the vector shows how many numbers there are afterwards. I want to discard this necessity. Also I'm 13 so please cope with me... – Orestis Jan 17 '20 at 19:46
  • 1
    @drescherjm I added the code I am using right now as an edit. I have also tried to use mmap for more speed, but something goes wrong. This is the link with the txt file: https://easyupload.io/vkhji3 – Orestis Jan 17 '20 at 21:52
  • I can't read the attached file / it could be a firewall issue since I am at work (some sites are off limits) How do you know how many students there are? `numOfStuds` is set somewhere. If you know this in advance you can size your vector accordingly at the start and avoid reallocaton. – drescherjm Jan 17 '20 at 22:07
  • Also if you are timing how long something is taking make sure you are running a release mode / optimized binary. In Visual Studio I have seen cases where an algorithm took 100 times as long to run in debug mode as it does in release. – drescherjm Jan 17 '20 at 22:09
  • @drescherjm Ok! – Orestis Jan 18 '20 at 05:05

1 Answers1

2

You can simply read every line and assign to a temporary row. Then assign the row array to your students matrice. Here is my approach:

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{

    ifstream file;
    file.open("/home/cayirova/Documents/example.txt",ios_base::app);
    string lines;
    vector<vector<int> > students;
    while (getline(file,lines)) {

        string element = "";   
        vector <int> newRow;

        for(int i=0;i<lines.length();i++)
        {
            if(!isspace(lines[i]))
            {
                element += lines[i];
            }
            if(isspace(lines[i]))
            {
               int assigned_element = stoi(element);
               newRow.push_back(assigned_element);
               element = "";
            }

        }

        students.push_back(newRow);

    }

    for(int i=0; i<8;i++)
    {
        for(int j=0;j<students[i].size();j++)
        {
            cout<<students[i][j]<<" ";
        }

        cout<<endl;
    }

    return 0;

}
Yunus Temurlenk
  • 2,826
  • 3
  • 10
  • 28
  • 1
    I only point this out because its extremely easy to write in a better style: `while(!file.eof())` [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Jan 17 '20 at 17:49
  • Ahh I didnt know it, I ll check and I ll edit my answer. Thank you – Yunus Temurlenk Jan 17 '20 at 17:52
  • @drescherjm When I tried that it gives me all the integers properly but how the way I can understand it is end of the line. Can you edit my post if you can? – Yunus Temurlenk Jan 17 '20 at 18:00
  • 1
    I meant to change `while(!file.eof()){` to `while (getline(file,lines)) {` and remove the `getline(file,lines);` – drescherjm Jan 17 '20 at 18:01
  • @drescherjm I edited, thanks. I misunderstood before. – Yunus Temurlenk Jan 17 '20 at 18:03
  • @YunusTemurlenk I don't know why but this is actually a bit slower than my other code. Do you have any suggestions to make it more efficient? If you have knowledge on mmap can you explain to me how I can use mmap for this, if it is genuinely faster? – Orestis Jan 17 '20 at 18:31
  • 1
    @Orestis I assume you deleted last for loop part because I am just using it to show the result. On the other hand, you should give me a reference code to be able to compare. – Yunus Temurlenk Jan 17 '20 at 19:48