Search code examples
c++visual-studiolemon-graph-library

Connexion between points (type) and an integer (without struct )


I read points frome a file, I use the library Lemon (because i want to use graph later) therefore each point is represented by the type : dim2 :: Point . so I used the library lemon/dim2.h

My problem is that each point there have a number of the frame of the video, so i used this code to put variables from file in a vectors:

std::ifstream file("file1.txt");
std::vector<dim2::Point<int>> pointTable;
std::vector<int> frame;

int temp, temp2,temp3;
while (file >> temp >> temp2 >> temp3)
{
    pointTable.push_back(dim2::Point<int>(temp, temp2));

     frame.push_back(temp3);
}
//int tailleFmax = frame.max_size;

 for (int i = 0; i < (36) ;i++)
 //cout << frame[i] <<endl;
// cout << trajectoire[i].x << endl;
 cout << trajectoire[i].y << endl;

My question : i dont know how to represent in c++ the connection betwen each point and his frame number and name this variable Trajectory.

Example of file :
155 // that is x
168 // that is y
0 // that is the frame number
364
245
20
546
156


Solution

  • I suspect that a map<int, dim2::Point<int>> is what you're looking for.

    You could also simplify your code by reading in the point directly using dim2::Point's extraction operator: http://lemon.cs.elte.hu/pub/doc/latest-svn/a00862.html#g2dd3eccf5dece76c03bc6d1c2f348643

    Your final code should look something like this:

    ifstream file("file1.txt");
    map<int, dim2::Point<int>> frame2PointTable;
    pair<int, dim2::Point<int>> temp;
    
    while(file >> temp.second >> temp.first) frame2PointTable.insert(temp);
    

    To output this you could do something like:

    for(const auto& i : frame2PointTable) cout << i.first << ": (" << i.second.x << ", " << i.second.y << ")\n";
    

    Important notes:

    1. Your example file contains 3 points but only 2 frame numbers, in this case only the 2 frame number-point combinations would be inserted
    2. If you ever have multiple identical frame numbers in a file, only the first instance will be accepted by frame2PointTable

    I've written you a live example using pair<int, int> instead of dim2::Point here: http://ideone.com/qtCZ8L