In-depth mode setup: compile an Iterator compatible with STL algorithm in the Iterator Mode

Source: Internet
Author: User

[Cpp]
# Include <vector>
# Include <algorithm>
 
Class Seat
{
Public:
Seat (int no): no _ (no ){}
Int no _;
};
 
 
Class Table
{
Static const std: size_t BEGIN_SEAT = 1;
Static const std: size_t INVALID_SEAT = 0;
Static const std: size_t END_SEAT = 9;
Public:
Table ()
{
Seats _. push_back (new Seat (1 ));
Seats _. push_back (new Seat (2 ));
Seats _. push_back (new Seat (3 ));
Seats _. push_back (new Seat (4 ));
Seats _. push_back (new Seat (5 ));
Seats _. push_back (new Seat (6 ));
Seats _. push_back (new Seat (7 ));
Seats _. push_back (new Seat (8 ));
Seats _. push_back (new Seat (9 ));
}
 
 
Std: vector <Seat *> seats _;
 
 
Seat * get (const int seat)
{
Return seats _. at (seat-1 );
}
 
 
Public:
Class Iterator
{
Friend class CycleIterator;
Public:
Typedef size_t size_type;
Typedef ptrdiff_t difference_type;
 
 
Typedef std: forward_iterator_tag iterator_category;
Typedef Seat * value_type;
Typedef Seat ** pointer;
Typedef Seat * & reference;
 
 
Iterator (Table * table, const int position): table _ (table), position _ (position ){}
 
 
Iterator (const Iterator & it): table _ (it. table _), position _ (it. position _){}
 
 
Iterator operator ++ ()
{
For (++ position _; position _ <= Table: END_SEAT; ++ position _)
{
// If (table _-> get (position _)-> visible _ = true)
{
Return Iterator (table _, position _);
}
}
 
 
Position _ = Table: INVALID_SEAT;
 
 
Return Iterator (table _, Table: INVALID_SEAT );
}
 
 
Seat * operator-> ()
{
Return table _-> get (position _);
}
 
 
Seat * operator *()
{
Return table _-> get (position _);
}
 
 
Bool operator = (const Iterator & it) const
{
Return position _ = it. position _;
}
 
 
Bool operator! = (Const Iterator & it) const
{
Return position _! = It. position _;
}
 
 
Private:
Table * table _;
 
 
Int position _;
};
 
 
Public:
Iterator begin ()
{
Return Iterator (this, BEGIN_SEAT );
}
 
 
Iterator end ()
{
Return Iterator (this, INVALID_SEAT );
}
};
 
 
Bool FindNullSeat (Seat * seat)
{
Return seat-> no _ = 3;
}
 
 
Int main (int, char **)
{
Table table;
Table: Iterator seat = std: find_if (table. begin (), table. end (), FindNullSeat );
 
 
Return 0;
}

# Include <vector>
# Include <algorithm>

Class Seat
{
Public:
Seat (int no): no _ (no ){}
Int no _;
};


Class Table
{
Static const std: size_t BEGIN_SEAT = 1;
Static const std: size_t INVALID_SEAT = 0;
Static const std: size_t END_SEAT = 9;
Public:
Table ()
{
Seats _. push_back (new Seat (1 ));
Seats _. push_back (new Seat (2 ));
Seats _. push_back (new Seat (3 ));
Seats _. push_back (new Seat (4 ));
Seats _. push_back (new Seat (5 ));
Seats _. push_back (new Seat (6 ));
Seats _. push_back (new Seat (7 ));
Seats _. push_back (new Seat (8 ));
Seats _. push_back (new Seat (9 ));
}


Std: vector <Seat *> seats _;


Seat * get (const int seat)
{
Return seats _. at (seat-1 );
}


Public:
Class Iterator
{
Friend class CycleIterator;
Public:
Typedef size_t size_type;
Typedef ptrdiff_t difference_type;


Typedef std: forward_iterator_tag iterator_category;
Typedef Seat * value_type;
Typedef Seat ** pointer;
Typedef Seat * & reference;


Iterator (Table * table, const int position): table _ (table), position _ (position ){}


Iterator (const Iterator & it): table _ (it. table _), position _ (it. position _){}


Iterator operator ++ ()
{
For (++ position _; position _ <= Table: END_SEAT; ++ position _)
{
// If (table _-> get (position _)-> visible _ = true)
{
Return Iterator (table _, position _);
}
}


Position _ = Table: INVALID_SEAT;


Return Iterator (table _, Table: INVALID_SEAT );
}


Seat * operator-> ()
{
Return table _-> get (position _);
}


Seat * operator *()
{
Return table _-> get (position _);
}


Bool operator = (const Iterator & it) const
{
Return position _ = it. position _;
}


Bool operator! = (Const Iterator & it) const
{
Return position _! = It. position _;
}


Private:
Table * table _;


Int position _;
};


Public:
Iterator begin ()
{
Return Iterator (this, BEGIN_SEAT );
}


Iterator end ()
{
Return Iterator (this, INVALID_SEAT );
}
};


Bool FindNullSeat (Seat * seat)
{
Return seat-> no _ = 3;
}


Int main (int, char **)
{
Table table;
Table: Iterator seat = std: find_if (table. begin (), table. end (), FindNullSeat );


Return 0;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.