By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Vector of non-primitive objects : structs

P: 1
Hi, I am a beginner in C++ so I hope you can be patient on me since my purpose is to learn.

I am now involved in a relatively complicated Data Analysis exercise.

I have a series of frames (vehicle data: trajectory) and I need to extract from them given informations.

For that, I put the info of each frame in a struct.
And I want to store the structs into a first vector grouped based on 1 info: time (ps: all the structs/frames of this vector have the same time).
This vector will be a part of another struct frame_time.
and this mother struct will be stored in itself based on increasing time (another attribute of the frame).

After that, I can sort the frames in the minor vector (location) based on the location info:

I created the following code (this is only part of the program):
Expand|Select|Wrap|Line Numbers
  1. FrameTimeData frame_time_data;
  3. for (int i = 0; i<NumberOfFrames; i++)
  4. {
  5.     VehFrameData veh_frame_data;
  6.     veh_frame_data = FrameLocationInitial[i]; //vector of frames already arranged 
  7. //based on time and sorted accordingly: just need to read them and then 
  8. //classify them in different mother structs stored themselves in a vector
  9.     frame_time_data.FrameLocation.push_back(veh_frame_data);
  10.     if (FrameLocationInitial[i].time != FrameLocationInitial[i+1].time)
  11.     {
  12.         FrameTime.push_back(frame_time_data);
  13.         FrameTimeData frame_time_data;
  14.     }
  16. }
The problem I am having is that each frame_time_data struct is taking the whole vector of FrameLocation from the first term and not just from the frame where time has changed. Any ideas or easy suggestions.

Mar 22 '07 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
Consider a vector of vectors:

vector< vector<MyStruct> > v;

Each element of v it itself a vector<MyClass>. These vector<MyClass> elements are the vectors whose elements have the same time. Therefore all you need do is sort v to have everything in time sequence.

Write a comparision function as a binary predicate to compare your vector<MyClass> elements and call the std::sort algorithm.

This avoids the "mother of all structs" approach.
Mar 26 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.