mardi 29 octobre 2019

Can I store an iterator after inserting a pair in a set, and later use that iterator to remove the pair in the set?

I have a class with two methods. Always Method1 is called, so the pair (value, index) is always stored in the ending_points set. In some cases, I need to call the Method2 and remove the iterator from the inserted pair. However, when I call Method2 the program crashes.

class TheClass: public ParentClass {
        public:
            TheClass(int value, int index, std::set<std::pair<int,int>>* const ending_points)
                : value_(value), index_(index), ending_points_(ending_points) {}
            ~TheClass() {}

            void Method1() {
                last_inserted_ = ending_points_->insert(std::make_pair(value_, index_)).first;
            }

            void Method2() {
                ending_points_->erase(last_inserted_);
            }

        private:
          int value_;
          int index_;
          std::set<std::pair<int64,int64>>::iterator last_inserted_;
          std::set<std::pair<int64,int64>>* const ending_points_;
    };      

I will really appreciate your help.

Aucun commentaire:

Enregistrer un commentaire