0

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

all.west
  • 71
  • 1
  • 1
  • 11
  • At the moment it seems that the connection between frame number and point is "they have the same index". Of course what representation you need for this connection depends on how you need to use it. – Francesco Dondi Jul 08 '16 at 09:50
  • yes, but then I want to give some functions trajectories (point and frame number of each point ) in a single variable – all.west Jul 08 '16 at 09:54
  • Would making a single vector with a struct having `int frame` and `dim2::Point point` as members be a solution? – Francesco Dondi Jul 08 '16 at 09:57
  • Yes, but i want to know if there is other method, because i'm new in c++ – all.west Jul 08 '16 at 10:00
  • I don't think this problem is particularly C++ specific. Which languages do you know? – Francesco Dondi Jul 08 '16 at 11:51
  • i know langage C, and im new in c++ – all.west Jul 08 '16 at 12:13
  • In this case, the STL library is probably the most useful new thing; I doubt polymorphic classes would be of any use. But them again, a `map` is only useful if you have to search for a specific point or frame; if you use them sequentially, plain vector is still the best. – Francesco Dondi Jul 08 '16 at 12:38

1 Answers1

0

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

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • That's good i will teste it now, the file example is not complete, there are 36 points, for each points there is a number frame. – all.west Jul 08 '16 at 12:09
  • @all.west I'f it solves your problem don't forget to accept, if it needs more work leave me a comment here and we'll try to buff out the issues. – Jonathan Mee Jul 08 '16 at 12:34