What is the "correct" way to add all elements from one std::list to another one?
void
Node::addChilds(const NodeList *list)
{
for(NodeList::const_iterator i = list->begin();
i != list->end();
++i)
{
this->m_childs.push_back(*i);
}
}
I thought about std::copy, but afaik for copy I have to resize the destination list, backup the end iterator (before resize) etc.
I'm searching for a single-line statement.
You can use the extend() method to add another list to a list, i.e., combine lists. All items are added to the end of the original list. You may specify other iterable objects, such as tuple . In the case of a string ( str ), each character is added one by one.
To append elements from another list to the current list, use the extend() method.
Use addAll () method to concatenate the given list1 and list2 into the newly created list.
this->m_childs.insert(this->m_childs.end(), list->begin(), list->end());
Use a back_insert_iterator
. If std::list<T>
is the type of m_childs
,
std::copy(list.begin(), list.end(),
std::back_insert_iterator<std::list<T> >(m_childs));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With