Definicja szablonu funkcji search_iter pelniąca role remove_copy_if

0

Witam mam problem z ***H = *B ** w search_iter , nie wiem dlaczego nie mogę przypisać do listy wartości z vectora. Search_iter miał się zachowywac jak remove_copy_if . Czy znalazł by się ktoś i napisał, gdzie błędnie rozumuje ?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <random>
#include <vector>
#include <iterator>
#include <list>
using namespace std;
struct Kula {
    int r;
    Kula()
        : r(0)
    {
    }
    Kula(int r_)
        : r(r_)
    {
    }
};
struct Gen {
    int x, y;
    Gen(int x_, int y_)
        : x(x_)
        , y(y_)
    {
    }
    Kula operator()()
    {

        int seed = rand() % (y - x + 1) + x;
        if (seed == 15 || seed == 21) {
            return Kula(15);
        }
        return Kula(seed);
    }
};
ostream& operator<<(ostream& out, const Kula& ob)
{
    return out << ob.r;
}
struct wedlugR {

    bool operator()(const Kula& ob, const Kula& ob2)
    {
        return ob.r > ob2.r;
    }
};
struct Mnoz {

    int operator()(const Kula& ob)
    {
        return ob.r * ob.r * 3.14 * 4;
    }
};
template <typename IterWej, typename IterWyj, typename Pred>

IterWyj search_iter(IterWej B, IterWej E, IterWyj H, Pred P)
{
    for (; B != E; B++) {
        if (P(*B)) {
           // *H = *B;
            H++;
        }
    }
    return H;
}
int main()
{
    srand(time(NULL));
    vector<Kula> vec(10);
    generate(vec.begin(), vec.end(), Gen(10, 20));
    copy(vec.begin(), vec.end(), ostream_iterator<Kula>(cout, " "));
    cout << endl;
    sort(vec.begin(), vec.end(), wedlugR());
    copy(vec.begin(), vec.end(), ostream_iterator<Kula>(cout, " "));
    list<Kula> lst;
    transform(vec.begin(), vec.end(), back_inserter(lst), Mnoz());
    cout << endl;
    copy(lst.begin(), lst.end(), ostream_iterator<Kula>(cout, " "));

    int tab[] = { 1, 2, 3, 4 };
    vector<int> v(tab, tab + 4);
    list<vector<int>::iterator> lst2(4);
    list<vector<int>::iterator>::iterator out = search_iter(v.begin(), v.end(), lst2.begin(), bind2nd(greater<int>(), 1));

    return 0;
}

 
3
  1. Dlaczego nikt nie odpowiada w moim wątku? - gdybyś podał to co mówi kompilator to odpowiedź na pytanie by była trywialna
  2. H jest iteratorem listy iteratorów wektora, więc *H to lvalue vector<int>::iterator, do którego faktycznie nie można przypisać int (*B)
  3. rand jest niezalecany https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
  4. bind2nd jest deprecated w C++14, nie ma go w C++17, użyj bind lub, lepiej, lambdy
  5. nie używaj list jeśli nie musisz. vector jest preferowany o ile benchmarki nie mówią inaczej.
  6. cały kod sprawia wrażenie jakbyś na siłę chciał się kompilować z C++03 - taki był cel?

Tak poza tym, w końcu jakiś kod który można czytać :)

1
error: no match for 'operator=' (operand types are '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' and 'int')
             *H = *B;

Nie wydaje Ci się to oczywiste? Błędy kompilatora naprawdę warto czytać ;)

0

Macie racje , przepraszam :p

1 użytkowników online, w tym zalogowanych: 0, gości: 1