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:
52.8774, 6.7442, 777
52.8550, 6.7449, 777
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
SOne
. 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);
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 ) ;
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.