⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a.cpp

📁 boost库中 mem_fun使用实例 注意要有boost库才能编译
💻 CPP
字号:
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/functional.hpp>
#include <string>
#include <boost/bind.hpp>
#include <boost/mem_fn.hpp>

using namespace std;
struct B {
	int i;
  bool print(int j);
};
bool B::print(int j)
{i=0; cout << "I'm a B" << endl;return true; };


int main()
{
 // vector<D1*> V;

 // V.push_back(new D1);
//  V.push_back(new D2);
 // V.push_back(new D2);
 // V.push_back(new D1);

 // for_each(V.begin(), V.end(), mem_fun(D1::print));//
	
	B a;
	bool x=false;
	
	boost::bind1st(boost::mem_fn(&B::print),&a)(1);
	if(x)
	{
		cout<<"\nreturn true"<<endl;
	}
//	mem_fun<bool,B>;
	
	cout<<endl;
  return 1;
}
/*
// fo/genera1.cpp
#include <iostream>
#include <list>
#include <algorithm>
#include "print.hpp"
using namespace std;
class IntSequence 
{
private:
	int value;
public:
	//constructor
	IntSequence (int initialValue): value(initialValue) {}
	//''function call''
	int operator() () {
		return value++;
	}
};

int main()
{
	list<int> coll;
	//insert values from 1 to 9
	generate_n (back_inserter(coll), //start
				9, //number of elements
				IntSequence (1)); //generates values
	PRINT_ELEMENTS(coll);
	//replace second to last element but one with values starting at 42
	generate (++coll.begin(), //start
				--coll.end(), //end
			IntSequence (42)); //generates values
	PRINT_ELEMENTS(coll);
}*/
/*
1 2 3 4 5 6 7 8 9
1 42 43 44 45 46 47 48 9
*/
/*
// fo/memfunla.cpp
class Person 
{
private:
	
public:
    std::string name;
	void print() const {
		std::cout << name << std::endl;
	}
	void printWithPrefix (std::string prefix) const {
		std::cout << prefix << name << std::endl;
	}
};
void foo (const std::vector<Person>& coll)
{
	using std::for_each;
	using std::bind2nd;
	using std::mem_fun_ref;
	//call member function print() for each element
	for_each (coll.begin(), coll.end(),
			mem_fun_ref(&Person::print));
	//call member function printWithPrefix() for each element
	//-"person: " is passed as an argument to the member function
	for_each (coll.begin(), coll.end(),
			bind2nd (mem_fun_ref (&Person::printWithPrefix),"person: "));
}

void main()
{
    int a;
    vector<Person> coll;
    Person a1;
    a1.name="a1";
    coll.push_back(a1);
    Person a2;
    a2.name="a2";
    coll.push_back(a2);
    foo(coll);
    cin>>a;
}*/
/*
class Stuff

{

public:

       std::string m_Name;

void printName()

{

       std::cout << m_Name << std::endl;

}      

};

void main()

{

       std::vector<Stuff> v;

       Stuff s1;

       s1.m_Name = "Brandon";

       v.push_back(s1);

       std::for_each(v.begin(), v.end(), boost::mem_fn(&Stuff::printName));

}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -