1

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

user8123891
  • 21
  • 1
  • 3
  • 5
    What do you mean by "it didn't work for me"? Isn't `MyWays.push_back(way[j]);` what you are looking for? – freakish Jul 18 '17 at 09:51
  • 3
    You don't show the code that "did not work". It would be a good idea to show exactly that piece of code too. And "did not work" is too vague. What happened? An compile time or runtime error? If so, please show the exact text of the message. Add all this information to your question. – Rudy Velthuis Jul 18 '17 at 09:51
  • 1
    What's the type of `Data`? – Holt Jul 18 '17 at 09:52
  • 1
    Please post the whole loop including the code that does not work so we can try to see what you did wrong. – Galik Jul 18 '17 at 09:57
  • 2
    Not directly related, but `double` as type for the `wayId` seems a pretty bad idea. – Jabberwocky Jul 18 '17 at 10:00

4 Answers4

4
  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);
    
CinCout
  • 9,486
  • 12
  • 49
  • 67
2

You can use std::vector::emplace_back to insert elements from your Data vector, assuming it has x,y, as well

struct SOne
{
    SOne( double y_in, double x_in, double wayId_in ):
     y(y_in), x(x_in), wayId(wayId_in)
     { }

    ~SOne() { }

     SOne& SOne=(const SOne& other) = default;
     SOne( const SOne& other ) =default;

    double y, x, wayId;
};

// Inside the if check simply do :
MyWays.emplace_back( Data[i].X, Data[i].Y, Data[i].WayId ) ;
P0W
  • 46,614
  • 9
  • 72
  • 119
0

Your struct should already have a default copy constructor to do the job so you can just use MyWays.push_back(SOne(Data[i])); with no changes to other parts of your code

IlBeldus
  • 1,040
  • 6
  • 14
0

A bit late to the party, but I'd probably insert the data like this:

MyWays.push_back({Data[i].X, Data[i].Y, Data[i].WayId});

This should invoke the move variant of push_back(), which cuts down on copying. Also, I'd probably use an integer for WayId, but that's more a matter of preference.

marcbf
  • 139
  • 1
  • 2
  • 10