Meddelande

Minska
No announcement yet.

Hur kan man använda Remove_if algoritmen med en vector?

Minska
X
  • Filter
  • Klockan
  • Show
Clear All
new posts

  • Hur kan man använda Remove_if algoritmen med en vector?

    Hej !
    Jag har det svårt att bygga sista puzzelbiten i min uppgift given till mig. Problemet ligger i remove_if (algoritmen). Detta är kod som är givet till mig:

    main.cpp
    [kod]
    int main() {
    vector<ShapePtr> shapevec;
    Vertex varr[] = { Verte0,0), Verte10,0), Verte5,2), Verte5,5)};
    shapevec.push_back( ShapePtr(new Polygon(1, 4, varr, 4)) );
    shapevec.push_back( ShapePtr(new Circle(5, 5, 4)) );

    ofstream os("fildat.txt");
    ostream_iterator<const ShapePtr> shapeout(os, "\n");
    copy(shapevec.begin(), shapevec.end(), shapeout);
    os.close();

    ifstream is("fildat.dat");
    istream_iterator<ShapePtr> shapein(is), endofshapein;
    list<ShapePtr> shapelist(shapein, endofshapein );
    for (list<ShapePtr>:terator it = shapelist.begin(); it != shapelist.end(); it++)
    cout << *it << endl;

    shapevec.insert( shapevec.end(), shapelist.begin(), shapelist.end() );
    shapevec.erase(remove_if( shapevec.begin(), shapevec.end(), CloseTo( Verte6,7))), shapevec.end());

    return 0;
    }
    [/kod]

    Här är det kod som jag har skrivit i relevans till problemet:
    ShapePtr.h
    [kod]
    class ShapePtr{
    protected:
    Shape* ShpPtr;

    public:
    static int numshapes;
    ShapePtr(): ShpPtr(0){}
    ShapePtr(Shape *sT) {ShpPtr = sT->clone(); delete sT;}

    ShapePtr(const ShapePtr &ptr);
    ~ShapePtr();

    friend ostream& operator<<( ostream &os, const ShapePtr &baseptr);
    friend istream& operator>>( istream &is, ShapePtr &baseptr);
    };
    [/kod]

    ShapePtr.cpp
    [kod]
    int ShapePtr::numshapes = 0;

    ShapePtr::ShapePtr(const ShapePtr &ptr){
    cout << "\tShapePtr_constructor_Pointer_Begin" << endl;
    if(ptr.ShpPtr != 0){
    ShpPtr = ptr.ShpPtr->clone();
    numshapes = Shape::cs;
    cout << "\t numshapes= " << numshapes << endl;
    } else{
    ShpPtr = 0;
    }
    cout << "\tShapePtr_constructor_Pointer_Finish" << endl;
    }

    ShapePtr::~ShapePtr(){
    cout << "\t\tShapePtr_destructor" << endl;
    if ( ShpPtr != 0 ){
    delete ShpPtr;
    }
    ShpPtr = 0;
    }


    ostream& operator<<( ostream &os, const ShapePtr &baseptr) {
    os << *baseptr.ShpPtr;
    return os;
    }

    istream& operator>>( istream &is, ShapePtr &baseptr) {
    string shpe;
    if (!(is >> shpe))
    return is;
    Shape *b = 0;
    if ( shpe == "CIRCLE" )
    b = new Circle();
    else if ( shpe == "POLYGON" )
    b = new Polygon();
    is >> *b;
    baseptr.ShpPtr = b->clone();
    delete b;
    return is;
    }
    [/kod]

    Vertex.h
    [kod]
    class Vertex {

    private:
    int x, y;

    public:
    Verte): 0), y(0){}
    Verteint _X, int _Y): _X), y(_Y){}
    ~Verte);

    const int Xv() { return x; }
    const int Yv() { return y; }
    const void Xin(int X_in ) { x = X_in; }
    const void Yin(int Y_in ) { y = Y_in; }
    };
    [/kod]

    Problemet är att jag inte förstår hur jag kan bygga metoden "CloseTo()" i main.cpp. Jag har svårt att få detta att fungera:

    [kod]shapevec.erase(remove_if( shapevec.begin(), shapevec.end(), CloseTo( Verte6,7))), shapevec.end());[/kod]

    Kan någon hjälpa mig eller guida mig till en förklaring (helst tips) till vad jag kan göra?
Working...
X