How to use for_each here correctly?
I am new to all the functional stuff in STL. I tried to do smething but it fails horribly no matter how I try it. Please comment:
#include <iostream>
#include <algorithm>
using namespace std;
class X
{
public:
void Print(int x)
{
cout << x << endl;
}
void Do()
{
int arr[] = {1, 2, 3, 4, 5};
mem_fun1_ref_t<void, X, int> oF = mem_fun_ref<void, X, int>(&X::Print);
binder1st<mem_fun1_ref_t<void, X, int> > oU = bind1st(oF, *this);
for_each(arr, arr+5, oU);
}
};
int main()
{
X x;
x.Do();
}
I get this error:
/usr/include/c++/4.3/backward/binders.h: In member function ‘typename _Operation::result_type std::binder1st<_Operation>::operator()(typename _Operation::second_argument_type&) const [with _Operation = std::mem_fun1_ref_t<void, X, int>]’:
/usr/include/c++/4.3/bits/stl_algo.h:3791: instantiated from ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = int*, _Funct = std::binder1st<std::mem_fun1_ref_t<void, X, int> >]’
main.cpp:19: instantiated from here
/usr/include/c++/4.3/backward/binders.h:121: error: no match for call to ‘(const std::mem_fun1_ref_t<void, X, int>) (const X&, int&)’
/usr/include/c++/4.3/bits/stl_function.h:632: note: candidates are: _Ret std::mem_fun1_ref_t<_Ret, _Tp, _Arg>::operator()(_Tp&, _Arg) const [with _Ret = void, _Tp = X, _Arg = int]
/usr/include/c++/4.3/backward/binders.h:121: error: return-statement with a value, in function returning 'void'
EDIT:
PluginLoader.h
#include <string>
#include <list>
#include <functional>
class Plugin;
//This class is an interface for loading the list of file names of shared objects.
//Could be by loading all file names in a dir, or by loading filenames specified in a file.
class FileNameLoader
{
public:
virtual std::list<std::string>& LoadFileNames() = 0;
};
class PluginLoader
{
public:
explicit PluginLoader();
virtual ~PluginLoader();
virtual bool Load();
virtual bool LoadPlugins(FileNameLoader&);
virtual bool LoadFunctions();
void SetLoadingPolicy(std::unary_function<std::string, void>*);
protected:
list<std::string> l_FileNames;
private:
explicit PluginLoader(const PluginLoader&);
PluginLoader& operator=(const PluginLoader&);
bool LoadSharedObjects();
void* LoadSharedObject(const std::string);
list<PluginFunction*> l_Functions;
list<Plugin*> l_Plugins;
std::unary_function<const std::string, void>*& p_LibLoader;
};
#endif // _PLUGIN_LOADER_HEADER_
PluginLoader.cpp
#include <PluginLoader.h>
#include <algorithm>
#include <dlfcn.h>
using namespace std;
//**************************************************************************************************
PluginLoader::PluginLoader():
{
mem_fun1_t<void, PluginFunction, int> oL(&PluginLoader::LoadSharedObject);
p_LibLoader = new binder1st<mem_fun1_t<void, PluginFunction, int> > oFunctor(oL, this);
}
//***********************************************************************************开发者_运维知识库***************
PluginLoader::~PluginLoader()
{
l_FileNames.clear();
l_Functions.clear();
l_Plugins.clear();
}
//**************************************************************************************************
bool PluginLoader::LoadSharedObjects()
{
for_each(l_FileNames.begin(), l_FileNames.end(), *p_LibLoader);
}
//**************************************************************************************************
void PluginLoader::LoadSharedObject(const std::string sFileName)
{
void* pHandle = dlopen(sFileName.c_str(), i_LibMode);
//if(pHandle == NULL)
//Check dlerror
}
//**************************************************************************************************
void PluginLoader::SetLoadingPolicy(unary_function<const string, void>*& pPolicy)
{
if(pPolicy != NULL)
{
delete p_LibLoader;
p_LibLoader = pPolicy;
}
}
I guess now it is ok.
Here's a simpler way to do it:
void Print(int x)
{
cout << x << endl;
}
class X
{
public:
void Do()
{
int arr[] = {1, 2, 3, 4, 5};
for_each(arr, arr+5, Print);
}
};
Or perhaps, redefine Print
as a functor, if you need it to store state:
struct Print {
void operator()(int x)
{
cout << x << endl;
}
};
class X
{
public:
void Do()
{
int arr[] = {1, 2, 3, 4, 5};
for_each(arr, arr+5, Print());
}
};
That saves you all the icky binding which is just painful to look at. (It also gives you a slimmer class, which is usually a good thing)
Although in this particular case, a more natural way to do it might actually be to ditch for_each
entirely and just copy
to the output stream:
int arr[] = {1, 2, 3, 4, 5};
std::copy(arr, arr+5, std::ostream_iterator<int>(std::cout));
The nice thing about the STL is that you have all sorts of algorithms, not just for_each
. You can copy, transform (map), accumulate (fold/reduce) or a number of other algorithms depending on your needs. In this case, what you want to do is copy the contents of your array to a stream, and streams can be dressed up as iterators, allowing std::copy
to work.
I got it to work :D.
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
class X
{
public:
void Print(int x)
{
cout << x << endl;
}
void Do()
{
int arr[] = {1, 2, 3, 4, 5};
mem_fun1_t<void, X, int> m(&X::Print);
binder1st<mem_fun1_t<void, X, int> > b(m, this);
for_each(arr, arr+5, b);
}
};
int main()
{
X x;
x.Do();
}
You need to have:
#include <functional>
in your code.
EDIT: see Billy ONeil's answer for what to do after you fix this.
Try this,
#include <iostream.h>
#include <algorithm.h>
精彩评论