0

Here is my setup:

class C {
public:
    template<typename T>
    void addFrom(T begin, T end){
        std::move(begin, end,
            std::back_inserter(vec));
    }
    std::vector<B> vec;
};
class A {
    C object;
    std::vector<B> vec;
    void passRangeToObject(){
     C.addFrom(std::make_move_iterator(
         vec.rbegin()),  
       std::make_move_iterator(vec.rbegin() 
       +5));
    }
};

After running the code the B objects are added in the C objects vector but they are not removed from A's vector. Is there a good solution out there?

T.C.
  • 133,968
  • 17
  • 288
  • 421
V Mircan
  • 203
  • 5
  • 13
  • Just add `vec.clear()` to `passRangeToObject()`? The leftover objects are basically empty shells at that point. –  Nov 29 '18 at 20:45
  • Yes this is my worst case scenario. I was just wondering if there was a cleaner solution out there. – V Mircan Nov 29 '18 at 20:51
  • Why not use [std::vector::swap](https://en.cppreference.com/w/cpp/container/vector/swap)? – cantordust Nov 29 '18 at 22:17
  • You should just use `vec.rbegin(), vec.rbegin()+5`, not the move iterators. `std::move` already knows to move. – M.M Nov 29 '18 at 23:07

0 Answers0