如何用C++03中成员函数返回的值填充向量
How to fill vector with values returned by a member function in 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));
任何人都可以共享一种方法,用指向成员函数的指针初始化向量,而不是在其中构造然后生成?
这不起作用的原因:
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
相关文章:
- 用C++中的数字和条件填充向量
- 是否有可能让 c++ dll 在后台运行 python 程序并让它填充向量图?如果是这样,如何?
- 有没有办法使用数组填充向量?
- 用对对填充向量未按预期工作
- 可选类输入中的 C++ 填充向量
- 用构造函数调用填充向量
- 错误:以增量方式填充向量时,表达式必须具有整数或无作用域枚举类型
- C 中的填充向量
- 从输入文件中填充向量的向量
- 在循环中创建和填充向量
- 如何使用两列C 从文本文件中填充向量
- 在C++编程语言中,如何用n个向量长度的所有可能的字符串组合来填充向量
- 使用精神,如何在AST结构内填充向量
- 如何用键盘输入来填充C++向量
- 用随机数C 填充向量
- 构造时,用整数序列填充向量
- 使用指向部分专用函数成员的指针自动填充向量
- 用于填充向量的c++有状态函子
- C++:在类构造函数中填充向量后,向量成员被销毁
- 如何在类中用不同类的对象填充向量