fork download
  1. #include <memory>
  2. #include <set>
  3. #include <functional>
  4. #include <algorithm>
  5. #include <iostream>
  6. struct Cell {
  7. int mX;
  8. Cell(int x) : mX(x) {}
  9. size_t GetX() const { return mX;}
  10. };
  11. struct CellSorter {
  12. bool operator()(const std::shared_ptr<Cell>& l, const std::shared_ptr<Cell>& r) const
  13. {
  14. return l->GetX() < r->GetX();
  15. }
  16. };
  17.  
  18. class CellInCol : public std::unary_function<const std::shared_ptr<Cell>,
  19. bool>
  20. {
  21. public:
  22. CellInCol( size_t col ) : _col( col ) {}
  23. bool operator() ( const std::shared_ptr<Cell> &a ) const
  24. {
  25. return a->GetX() == _col;
  26. }
  27. private:
  28. size_t _col;
  29. };
  30. int main()
  31. {
  32. typedef std::set<std::shared_ptr<Cell>, CellSorter> Container;
  33. Container _grid;
  34. _grid.insert( std::shared_ptr<Cell>(new Cell(1)));
  35. _grid.insert( std::shared_ptr<Cell>(new Cell(2)));
  36. _grid.insert( std::shared_ptr<Cell>(new Cell(7)));
  37. _grid.insert( std::shared_ptr<Cell>(new Cell(10)));
  38. Container col;
  39. size_t c = 7;
  40. std::remove_copy_if( _grid.begin(), _grid.end(),
  41. std::inserter( col, col.begin() ),
  42. std::not1( CellInCol( c ) ) );
  43. std::cout << "col has " << col.size() << " elements\n"
  44. << " the first element is " << (*col.begin())->GetX() << '\n';
  45. }
  46.  
Success #stdin #stdout 0s 2964KB
stdin
Standard input is empty
stdout
col has 1 elements
 the first element is 7