If I have two std::deque
s, and I want to move the first n
objects from the beginning of one to the beginning of the other, what is the best way to do that? Doing the following:
template <typename T>
void fn(std::deque<T>& src)
{
std::deque<T> storage;
size_t i = /* some calculated value between 0 and src.size() */;
storage.insert(storage.begin(), src.begin(), src.begin()+i);
src.erase(src.begin(), src.begin()+i);
// ... other stuff ...
}
would create copies of the T objects. I guess I could do something like (not tested, as example only):
template <typename T>
void fn(std::deque<T>& src)
{
std::deque<T> storage;
size_t i = /* some calculated value between 0 and src.size() */;
for (auto& it = std::reverse_iterator<decltype(src.begin()>(src.begin()+i)
; it != std::reverse_iterator<decltype<src.begin()>(src.begin())
; ++it)
{
storage.push_front(std::move(*it));
}
src.erase(src.begin(), src.begin()+i);
// ... other stuff ...
}
but I'm wondering, is there an algorithm that would already handle this?
Aucun commentaire:
Enregistrer un commentaire