sorted_bag.h

Embed Size (px)

Citation preview

  • 7/31/2019 sorted_bag.h

    1/4

    #include#includeusingnamespacestd;//FILE:sorted_bag.h//WrittenbyBrendenHogan//Lastmodification:April8,2012////CONSTRUCTORforthesorted_bagclass://sorted_bag();// Postcondition:Anewsorted_baghasbeencreatedandbagoftype// listhasbeeninitialized////COPYCONSTRUCTORforthesorted_bagclass://sorted_bag(constsorted_bag&rhs);// Postcondition:Anewsorted_bagobjecthasbeencreated.Itsstate// isacompletecopyofthestateofsorted_bagobjectrhs.////DESTRUCTORforthesorted_bagclass// ~sorted_bag()// Postcondition:Alldynamicmemoryoftheobjecthasbeendeallocated.////voidinsert(Itemx);// Precondition:xisavaliditemforthetypeofthebagitsbeing

    // calledon.// Postcondition:xhasbeeninsertedintothebaginincressingorder.////voidinsert(list&x);// Precondition:xisthesametypelistassorted_bagis// Postcondition:xistraveresedandeachiteminxisinserted// intobag.////voidremove(list&x);// Precondition:xisthesametypelistassorted_bagis// Postcondition:xistraveresedandonecopyofeachitem// inxisremovedfrombag

    ////voidremove(Itemx);// Precondition:xisofthesametypeasthesorted_bagcallingthis// Postcondition:Allcopiesofxhavebeenremovedfromthebag////size_toccurrences(Itemx);// Precondition:xisofthesametypeofthesorted_bagcallingthis// Postcondition:Theamountofcoppiesofxinbagisreturned////sorted_bag&operator+=(constsorted_bag&rhs);// Precondition:Thisiscalledbyavalidsorted_bagand

    // rhsisavalidsorted_bag.// Postcondition:Allthetermsinrhshavebeenaddedtothe// sorted_bagcallingthefunction////sorted_bag&operator=(constsorted_bag&rhs);// Precondition:Avalidsorted_bagofthesametypeasrhs// callsthisandrhsisavalidsorted_bag// Postcondition:Thesorted_bagthatcallsthisissetequallto// rhs//

  • 7/31/2019 sorted_bag.h

    2/4

    //friendostream&operator

  • 7/31/2019 sorted_bag.h

    3/4

    typenamestd::list::iteratori;for(i=x.begin();i!=x.end();++i){

    insert(*i);}

    }//---------------------------------------------------------------------------------------------

    voidremove(list&x){typenamestd::list::iteratori;typenamestd::list::iteratorj;for(i=x.begin();i!=x.end();++i){

    for(j=bag.begin();j!=bag.end();++j){if(*i==*j){

    bag.erase(j);break;

    }}

    }}

    //---------------------------------------------------------------------------------------------

    voidremove(Itemx){bag.remove(x);

    }//---------------------------------------------------------------------------------------------

    size_toccurrences(Itemx){size_tcounter=0;typenamestd::list::iteratori;for(i=bag.begin();i!=bag.end();++i){

    if(*i==x)counter++;}returncounter;

    }//---------------------------------------------------------------------------------------------

    sorted_bag&operator+=(constsorted_bag&rhs){typenamestd::list::const_iteratori;sorted_bagtemp(rhs);

    for(i=temp.bag.begin();i!=temp.bag.end();++i){insert(*i);

    }return*this;

    }//---------------------------------------------------------------------------------------------

    sorted_bag&operator=(constsorted_bag&rhs){bag=rhs.bag;

    return*this;}

    //---------------------------------------------------------------------------------------------

    friendostream&operator

  • 7/31/2019 sorted_bag.h

    4/4

    }returnouts;

    }//---------------------------------------------------------------------------------------------

    friendbooloperator==(constsorted_bag&lhs,constsorted_bag&rhs){

    if(rhs.bag.size()!=lhs.bag.size())returnfalse;typenamestd::list::const_iteratori;typenamestd::list::const_iteratorj;for(i=rhs.bag.begin(),j=lhs.bag.begin();i!=rhs.bag.end

    ();++i,++j){if(*i!=*j)returnfalse;

    }returntrue;

    }};