如何用C++03中成员函数返回的值填充向量

How to fill vector with values returned by a member function in C++03?

本文关键字:填充 向量 返回 函数 何用 C++03 成员      更新时间:2023-10-16

我想得到与以下相同的行为:

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));

任何人都可以共享一种方法,用指向成员函数的指针初始化向量,而不是在其中构造然后生成?

这不起作用的原因:

std::bind1st(std::mem_fun(&IdentifiersGenerator::getNextIdentifiers), gen)

bind1st需要二进制函数,而您提供的是一元函数。没有一个预打包的C++03解决方案,如果boost::bind是一个选项,我只会使用它。

否则,可以编写自己的工厂,该工厂获取一个指向null成员函数的指针和一个指向类的指针,并返回一个具有调用它的operator()的对象

(此处发布仅供参考)

好的,所以我已经意识到我可以像这样使用boost::bind

std::vector<AlarmIdentifiers> v(100);
std::generate(v.begin(), v.end(),
    boost::bind(&IdentifiersGenerator::getNextIdentifiers, &gen));

bind实际上是一种"旧"且大多过时的方法,可以实现类似于更好的lambda语法的功能。

IdentifiersGenerator gen;
std::vector<Identifiers> v(100);
std::generate(v.begin(), v.end(), [&](){ return gen.getNextIdentifiers(); });

您也可以使用generate_n