webForumDet fria alternativet

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

C/C++

0 svar · 4 072 visningar · startad av colper89

Medlem sedan juli 20141 inlägg
Frågan#1

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

int main() {
  vector<ShapePtr> shapevec;
  Vertex varr[] = { Vertex(0,0), Vertex(10,0), Vertex(5,2), Vertex(5,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>::iterator 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( Vertex(6,7))), shapevec.end());

  return 0;
}

Här är det kod som jag har skrivit i relevans till problemet:
ShapePtr.h

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);
};

ShapePtr.cpp

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;
}

Vertex.h

class Vertex {

  private:
    int x, y;

  public:
    Vertex(): x(0), y(0){}
    Vertex(int _X, int _Y): x(_X), y(_Y){}
    ~Vertex();

    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; }
};

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:

shapevec.erase(remove_if( shapevec.begin(), shapevec.end(), CloseTo( Vertex(6,7))), shapevec.end());

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

252 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
119 ms — deklarationer (db)
0 ms — hämta statistik (cache)
128 ms — hämta tråd, inlägg och bilagor (db)
121 ms — ändringar (db)