[ create a new paste ] login | about

Link: http://codepad.org/GWHKrVcz    [ raw code | output | fork ]

C++, pasted on Nov 2:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
class Test {
  int val;
public:
  Test() { val = 0; }
  Test(int x) { val = x; }
  bool showval() const { std::cout << val << " "; return true; }
  int doubleval() { val += val; return val; }
  int addval(int i) { val += i; return val; }
};
int main() {
  std::vector<Test *> v;
  v.push_back(new Test(1));
  v.push_back(new Test(2));
  v.push_back(new Test(3));
  v.push_back(new Test(4));
  v.push_back(new Test(5));
  for_each(v.begin(), v.end(), std::mem_fun(&Test::showval));
  std::cout << std::endl;

  for_each(v.begin(), v.end(), std::mem_fun(&Test::doubleval));
  for_each(v.begin(), v.end(), std::mem_fun(&Test::showval));
  std::cout << std::endl;

  for_each(v.begin(), v.end(), std::bind2nd(std::mem_fun1(&Test::addval), 10));
  for_each(v.begin(), v.end(), std::mem_fun(&Test::showval));
  std::cout << std::endl;
  
  std::vector<Test *>::iterator p = v.begin();
  while (p != v.end()) {
    delete *p;
    p++;
  }
  return 0;
}
/* end */


Output:
1
2
3
In function 'int main()':
Line 28: error: 'mem_fun1' is not a member of 'std'
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: