c++ - Filter operation from vector to vector -


i'm trying implement filter operation takes vector v , creates vector d filtered elements of v. result can't pointer , i'm not allowed use regular loop/while. thinking of using: for_each, copy, copy_if none seem work.

    vector<car> fin;     vector<car> all(repo->getall());     for_each(all.begin(), all.end(), [=]( car& cc) {             if ( cc.getmodel() == model) {                car c(cc.getnumber(),cc.getmodel(),cc.getcategory());                 fin.push_back(c);             } 

this give me error when performing push_back.

copy_if(all.begin(), all.end(),fin.begin(),                 [&](const car& cc) { if (cc.getmodel()==model) return cc; }); 

this go inside iterator library , give me errors along "conditional expression of type const car illegal"

is there way make copies of elements need 1 vector , add them other inside loop this?

i tried if(find_if(...) on same idea, lambda , trying create new car , add d vector didn't work either

full corected filter function:

vector<car> controller::filterbycategory(string category) {     vector<car> fin;     vector<car> all(repo->getall());      copy_if(all.begin(), all.end(),fin.begin(),                 [&](car& cc) { return (cc.getcategory()==category); });     return fin; } 

this go inside iterator library , give me errors along "conditional expression of type const car illegal"

your lambda function expected return boolean value, not object. change

copy_if(all.begin(), all.end(),fin.begin(),                 [&](const car& cc) {                      return (cc.getmodel()==model); }); 

it's meant used condition copy_if() if item input range should copied result or not.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -