Exceptional C++ Style一书第四条阐述了std::mem_fun的相关问题,尝试了下述的代码。
for_each(l.begin(), l.end(), mem_fun(&list<int>::sort));
copy(s1.begin(), s1.end(), ostream_iterator<int>(cout, " "));
copy(s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));
no matching function for call to `mem_fun(<unknown type>)'
这个例子是从roguewave的STL文档中直接摘出来的例子,居然出错,百思不得其解。后来将这段代码放到Visual Stdio 2005 beta2中进行编译,顺利通过编译,运行也正确。
for_each(l.begin(), l.end(), mem_fun<void, std::list<int, std::allocator<int> > >(&list<int>::sort));
D:/Dev/Dev-Cpp/bin>g++ --version
g++ (GCC) 3.4.2 (mingw-special)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
注:利用春节假期,看了一点C++的书,尝试了一些代码,贴出来和大家共享。这是第一篇。