
Hello, I have the following question. I have two containers std::set<std::string> insertedEvents; std::vector<std::string> eventNames; How I would like to copy all those elements from eventNames which are not in insertedEvents. Can this can be done somehow using the boost::bind feature? I am doing it at the moment using a silly loop: for(size_t i=0; i<eventNames.size(); i++) { if (m_insertedEvents.find(eventNames[i])==m_insertedEvents.end()) eventNamesDegraded.push_back(eventNames[i]); } Thanks for any kind of help, Pshemek ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/information/legal_information.asp?Code=ECAS-8... for additional disclosures.

On Mon, Aug 23, 2010 at 04:49:58PM +0100, przemyslaw.sliwa@uk.bnpparibas.com wrote:
I have the following question. I have two containers
std::set<std::string> insertedEvents; std::vector<std::string> eventNames;
How I would like to copy all those elements from eventNames which are not in insertedEvents. Can this can be done somehow using the boost::bind feature?
<algorithm> contains std::set_difference, which is most probably what you want, assuming that eventNames is sorted. If it's not, std::remove_copy_if(eventNames.begin(),eventNames.end(), std::back_inserter(out), boost::bind(&std::set<std::string>::count, boost::cref(insertedEvents), _1) ); That is, bind the count member function of std::set to your set of events, and evaluate that for each element in your range. Of course, something like Phoenix or Lambda would make the predicate easier on the eyes. -- Lars Viklund | zao@acc.umu.se

On Mon, Aug 23, 2010 at 04:49:58PM +0100, przemyslaw.sliwa@uk.bnpparibas.com wrote:
I have the following question. I have two containers
std::set<std::string> insertedEvents; std::vector<std::string> eventNames;
How I would like to copy all those elements from eventNames which are not in insertedEvents. Can this can be done somehow using the boost::bind feature?
<algorithm> contains std::set_difference, which is most probably what you want, assuming that eventNames is sorted.
If it's not,
std::remove_copy_if(eventNames.begin(),eventNames.end(), std::back_inserter(out), boost::bind(&std::set<std::string>::count, boost::cref(insertedEvents), _1) );
That is, bind the count member function of std::set to your set of events, and evaluate that for each element in your range.
Of course, something like Phoenix or Lambda would make the predicate easier on the eyes.
It's worth noting that if your compiler supports C++0x lambdas (gcc 4.5+ or VS2010), then the predicate becomes std::remove_copy_if(eventNames.begin(),eventNames.end(), std::back_inserter(out), [&insertedEvents](const std::string &s){ return insertedEvents.find(s) != insertedEvents.end(); }); and there is no need for Boost involvement at all.

On Mon, Aug 23, 2010 at 01:28:35PM -0400, lfrfly@icqmail.com wrote:
It's worth noting that if your compiler supports C++0x lambdas (gcc 4.5+ or VS2010), then the predicate becomes std::remove_copy_if(eventNames.begin(),eventNames.end(), std::back_inserter(out), [&insertedEvents](const std::string &s){ return insertedEvents.find(s) != insertedEvents.end(); }); and there is no need for Boost involvement at all.
And of course, then you have std::bind too. -- Lars Viklund | zao@acc.umu.se

On Mon, Aug 23, 2010 at 11:28 AM, <lfrfly@icqmail.com> wrote:
On Mon, Aug 23, 2010 at 04:49:58PM +0100,
przemyslaw.sliwa@uk.bnpparibas.com
wrote:
I have the following question. I have two containers
std::set<std::string> insertedEvents; std::vector<std::string> eventNames;
How I would like to copy all those elements from eventNames which
are not
in insertedEvents. Can this can be done somehow using the
boost::bind
feature?
<algorithm> contains std::set_difference, which is most probably what
you want,
assuming that eventNames is sorted.
If it's not,
std::remove_copy_if(eventNames.begin(),eventNames.end(), std::back_inserter(out), boost::bind(&std::set<std::string>::count,
boost::cref(insertedEvents), _1)
);
That is, bind the count member function of std::set to your set of events, and evaluate that for each element in your range.
Of course, something like Phoenix or Lambda would make the predicate easier on the eyes.
It's worth noting that if your compiler supports C++0x lambdas (gcc 4.5+ or VS2010), then the predicate becomes std::remove_copy_if(eventNames.begin(),eventNames.end(), std::back_inserter(out), [&insertedEvents](const std::string &s){ return insertedEvents.find(s) != insertedEvents.end(); }); and there is no need for Boost involvement at all.
Or even shorter (using Boost.Phoenix as mentioned above): std::remove_copy_if(eventNames.begin(),eventNames.end(), std::back_inserter(out), find(ref(insertedEvents), val(s))!=end(ref(insertedEvents)) // for speed, should probably cache this 'end' call externally...
participants (4)
-
Lars Viklund
-
lfrfly@icqmail.com
-
OvermindDL1
-
przemyslaw.sliwa@uk.bnpparibas.com