[boost-users][multi-index] subtracting containers
Hi, What would be the shortest way to subtract 2 containers? multi_index_container < // ordered key
cont1, cont2; cont1.insert(a); cont1.insert(b); cont1.insert(c); cont2.insert(b); // here I'd like to do cont1 -= cont2, so that cont1 would contain (a,c) Obviously, the operator -() or -=() is unsupported. So is there a way to do this without an explicit loop? IIUC, std::remove_if() wouldn't work here, because it tries to rearrange the elements... Thank you!
________________________________________ De: boost-users-bounces@lists.boost.org [boost-users-bounces@lists.boost.org] En nombre de Igor R [boost.lists@gmail.com] Enviado el: domingo, 03 de agosto de 2008 14:22 Para: boost-users@lists.boost.org Asunto: [Boost-users] [boost-users][multi-index] subtracting containers
Hi,
What would be the shortest way to subtract 2 containers?
Hi Igor,
Excuse my late answering, I'm away on vacation and with little access to Internet.
Your problem is not particualrly related to Boost.MultiIndex, since it is entirely
analogous to the same issue as posed with std::sets. You can use
std::set_difference, but the interface of this function forces you yo create a
fresh copy with the result of the difference rather than doing the subtraction in place.
Another alternative would be to use std::for_each more or less like this:
typedef multi_index_container
Hi Joaquín, Thank you very much for your response. Have a nice vacation!
Hi Igor,
Excuse my late answering, I'm away on vacation and with little access to Internet. Your problem is not particualrly related to Boost.MultiIndex, since it is entirely analogous to the same issue as posed with std::sets. You can use std::set_difference, but the interface of this function forces you yo create a fresh copy with the result of the difference rather than doing the subtraction in place. Another alternative would be to use std::for_each more or less like this:
typedef multi_index_container
multi_t; struct multi_t_erase { multi_t_erase(multi_t& m):pm(&pm){} void operator()(int n){pm->erase(n);}
private: multi_t* pm; };
std::for_each(c2.begin(),c2.end(),multi_t_erase(c1));
Yet another alternative consists in traversing both containers more or less in the same way as std::set_difference does:
multi_t::iterator f1=c1.begin(),l1=c1.end(),f2=c2.begin(),l2=c2.end(); while(f1!=l1&&f2!=l2){ if(c1.value_comp()(*f1,*f2))c1.erase(f1++); else if(c1.value_comp()(*f2,*f1))++f2; else ++f1,++f2; } c1.erase(f1,l1);
participants (2)
-
Igor R
-
JOAQUIN M. LOPEZ MUÑOZ