|
Boost Users : |
From: JOAQUIN M. LOPEZ MUÑOZ (joaquin_at_[hidden])
Date: 2008-08-04 18:21:22
________________________________________
De: boost-users-bounces_at_[hidden] [boost-users-bounces_at_[hidden]] En nombre de Igor R [boost.lists_at_[hidden]]
Enviado el: domingo, 03 de agosto de 2008 14:22
Para: boost-users_at_[hidden]
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<int,...> 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);
HTH,
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net