|
You last visited: Today at 16:33
Advertisement
Functor in copy_if
Discussion on Functor in copy_if within the C/C++ forum part of the Coders Den category.
04/22/2017, 12:05
|
#1
|
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
|
|
|
04/22/2017, 12:16
|
#2
|
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
|
|
|
04/22/2017, 12:54
|
#3
|
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(), ?);
}
|
|
|
04/22/2017, 13:17
|
#4
|
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
|
|
|
04/22/2017, 14:10
|
#5
|
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.
|
|
|
04/22/2017, 14:15
|
#6
|
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
|
|
|
04/22/2017, 14:18
|
#7
|
elite*gold: 0
Join Date: May 2013
Posts: 1,195
Received Thanks: 38
|
I did not allocate it before. just declared it
|
|
|
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.
|
|
|
04/22/2017, 14:48
|
#9
|
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.
|
|
|
All times are GMT +1. The time now is 16:33.
|
|