我想獲得相同的行爲,因爲這:如何用C++ 03中的成員函數返回的值填充向量?
IdentifiersGenerator gen;
for(int i = 0; i < 100; ++i)
v.push_back(gen.getNextIdentifiers());
具有類似的語法:
IdentifiersGenerator gen;
std::vector<Identifiers> v(100);
std::generate(v.begin(), v.end(),
std::bind1st(std::mem_fun(&IdentifiersGenerator::getNextIdentifiers), gen));
上面的代碼提供了以下錯誤:
test/src/IdentifiersGeneratorTest.cpp:449: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:100: error: no type named ‘second_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:103: error: no type named ‘first_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:106: error: no type named ‘first_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:111: error: no type named ‘second_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:117: error: no type named ‘second_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h: In function ‘std::binder1st<_Operation> std::bind1st(const _Operation&, const _Tp&) [with _Operation = std::mem_fun_t<const Identifiers&, IdentifiersGenerator>, _Tp = IdentifiersGenerator]’:
test/src/IdentifiersGeneratorTest.cpp:449: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:126: error: no type named ‘first_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’
或許這能夠即使沒有std::vector
默認對象初始化使用例如boost::make_function_input_iterator
。
EDIT行,所以我已經意識到,我可以使用boost::bind
像這樣:
std::vector<AlarmIdentifiers> v(100);
std::generate(v.begin(), v.end(),
boost::bind(&IdentifiersGenerator::getNextIdentifiers, &gen));
任何人都可以共享的方法來初始化指針成員函數,而不是構建載體中,然後在其內部產生?
是否好做一個仿函數? – NathanOliver
不,我需要成員函數'IdentifiersGenerator :: getNextIdentifiers' – Patryk
你可以使用函數來獲得它。我在問你是否可以用一個仿函數包裝這個電話。 – NathanOliver