Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

C++ Can I return iterator on a local vector from a method?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?

The vector will be destroyed when it goes out of scope when the function exits, thus the returned iterator will be left dangling.

Will my vector v consists of the original objects by references or they will be references on new objects?

It will contain copies of the original objects.

when I will use the returned iterator outside this method, on vector of references to the same objects, will it work?

No. Because one, you don't have a vector of references, but a vector of copied objects. And two, the vector was already destroyed and you have a dangling iterator.

route_departures_const_reference departures(platform_route_const_reference pr)

If route_departures_const_reference is actually a reference (ie, std::vector<route_departure_object>&) as its name suggests, then you would be returning a dangling reference to a destroyed vector.

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?????

No, it would be referring to the copied objects that were made (if the returned vector/iterator were valid to begin with).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...