Search code examples
c++vectorstructpush-back

c++ push_back() a struct into a vector


I have a large file with coordinates and the WayIds. Which I stored in a vector with the following struct:

struct SOne
{
    double y, x, wayId;
};

The file looks like this:

  1. 52.8774, 6.7442, 777

  2. 52.8550, 6.7449, 777

  3. 52.8496, 6.7449, 776

In my program I have already filtered the WayIds with which I would like to continue working and stored in a vector named “way”. With a for loop i can find the coordinates but i don't know how to store them in a vector with struct.

    vector<SOne> MyWays;
for (int i = 0; i < Data.size(); i++)   { // Data -> my file with all coordinates and wayIds
    for (size_t j = 0; j < way.size(); j++){
        if (Data[i].WayId == way[j])  // way[j] -> here i get the WayId i like to work with
        {

        } // if
    } // for
} // for

I tried to follow this link: push_back() a struct into a vector but it didn't work for me. Can anyone give me a hint? Thanks in advance


Solution

    1. Construct SOne.
    2. Fill the object.
    3. Insert into MyWays.

      SOne sOneObj;
      sOneObj.x = Data[i].X;
      sOneObj.y = Data[i].Y;
      sOneObj.wayId = Data[i].WayId; // = way[j]
      MyWays.push_back(sOneObj);