I have this method:
route_departure_const_iterator departure_at(platform_route_const_reference pr, packed_time tm)
{
std::vector<route_departure_object> v;
for (auto&& e : pr.departures) {
int i = 0;
while (true)
{
if (pr.tt->timetable_route_departures.at(i).platform_name == pr.platform_name && pr.tt->timetable_route_departures.at(i).route_name == pr.route_name && pr.tt->timetable_route_departures.at(i).departure_time >= e)
{
route_departure_const_reference rd = pr.tt->timetable_route_departures.at(i);
v.push_back(rd);
break;
}
i++;
}
}
return v.begin();
}
My question is, if I initialized new vector of objects in this method, when I return the iterator, will it point at this vector or will this vector be deleted after exiting the method?
Then, pr.tt->timetable_route_departures is vector of route_departure_object like vector v.
route_departure_const_reference rd is a reference to route_departure_object. Will my vector v consists of the original objects by references or they will be references on new objects?
With this method, when I will use the returned iterator outside this method, on vector of references to the same objects, will it work?
I mean, I have this method:
route_departures_const_reference departures(platform_route_const_reference pr)
{
std::vector<route_departure_object> v;
for (auto&& e : pr.departures) {
int i = 0;
while (true)
{
if (pr.tt->timetable_route_departures.at(i).platform_name == pr.platform_name && pr.tt->timetable_route_departures.at(i).route_name == pr.route_name && pr.tt->timetable_route_departures.at(i).departure_time == e)
{
route_departure_const_reference rd = pr.tt->timetable_route_departures.at(i);
v.push_back(rd);
break;
}
i++;
}
}
return v;
}
There are many more objects in pr.tt->timetable_route_departures, I just choose some of them. Then, when I will use returned iterator on this returned vector, will it point at the exact same chosen objects in pr.tt->timetable_route_departures?????
Thanks a lot for help!
question from:
https://stackoverflow.com/questions/65602148/c-can-i-return-iterator-on-a-local-vector-from-a-method 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…