Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 16:33

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Functor in copy_if

Discussion on Functor in copy_if within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
Dwarfche's Avatar
 
elite*gold: 0
Join Date: May 2013
Posts: 1,195
Received Thanks: 38
Functor in copy_if

Hey, I need some help with a functor in C++ for the copy_if algorithm. PM me if you can help, thanks in advance
Dwarfche is offline  
Old 04/22/2017, 12:16   #2


 
Jeoni's Avatar
 
elite*gold: 966
Join Date: Apr 2010
Posts: 1,105
Received Thanks: 681
I may help, but why not describing the problem in the thread, so anyone can participate and the solution may help others, too?
If that is not possible due to legal reason (your project is top secret), try breaking the error down to a simple case which has nothing to do with your project anymore.
With best regards
Jeoni
Jeoni is offline  
Old 04/22/2017, 12:54   #3
 
Dwarfche's Avatar
 
elite*gold: 0
Join Date: May 2013
Posts: 1,195
Received Thanks: 38
Okay, I have list of pointers to objects of class CEstate. CEstate is an abstract class which is inherited by CPlot,CHouse and CFlat.

I have to make a function that returns a list of all objects that are from a city(parameter of the function). It should be done with copy_if.

list<CEstate*> listByTown(string town){
copy_if(estateList.begin(), estateList.end(), listByTown.begin(), ?);
}
Dwarfche is offline  
Old 04/22/2017, 13:17   #4


 
Jeoni's Avatar
 
elite*gold: 966
Join Date: Apr 2010
Posts: 1,105
Received Thanks: 681
I guess the most simple version is passing a lambda function which does the town check.
Code:
std::copy_if(estateList.begin(), estateList.end(), result.begin(), [&town](CEstate* estate) -> bool { estate->town == town; });
The lambda (and therefor the check) is called for each object in estateList. The town-member is just made up, but I guess the class includes something like this. And town (not the member) is the argument passed to the listByTown function as already done in your example.
With best regards
Jeoni
Jeoni is offline  
Old 04/22/2017, 14:10   #5
 
Dwarfche's Avatar
 
elite*gold: 0
Join Date: May 2013
Posts: 1,195
Received Thanks: 38
That's what I wrote:
Code:
std::copy_if(estateList.begin(), estateList.end(), listByTown.begin(), [&town](CEstate* estate) -> bool { return estate->getAddress() == town; });
I got error: list iterator not incrementable.
The error appears when I run the program.
Dwarfche is offline  
Old 04/22/2017, 14:15   #6


 
Jeoni's Avatar
 
elite*gold: 966
Join Date: Apr 2010
Posts: 1,105
Received Thanks: 681
Is your result list (listByTown) big enough to hold the objects? Since copy_if do not change the size of the result list, it has to be allocated before.
With best regards
Jeoni
Jeoni is offline  
Old 04/22/2017, 14:18   #7
 
Dwarfche's Avatar
 
elite*gold: 0
Join Date: May 2013
Posts: 1,195
Received Thanks: 38
I did not allocate it before. just declared it
Dwarfche is offline  
Old 04/22/2017, 14:35   #8
 
elite*gold: 46
Join Date: Oct 2010
Posts: 782
Received Thanks: 525
Code:
std::copy_if(estateList.begin(), estateList.end(), std::back_inserter(listByTown), [&town](CEstate* estate) -> bool { return estate->getAddress() == town; });
should work as long as listByTown has a push_back method.
th0rex is offline  
Old 04/22/2017, 14:48   #9
 
Dwarfche's Avatar
 
elite*gold: 0
Join Date: May 2013
Posts: 1,195
Received Thanks: 38
Is there any way to reserve memory for X elements? Like the reserve method for vectors?
And yes, the list should have push_back method, since it's already defined. Still it doesnt work if my list size is less or more than the actual number of objects from the desired town.
Dwarfche is offline  
Reply




All times are GMT +1. The time now is 16:33.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.