2016-08-04 49 views
0

我想獲得相同的行爲,因爲這:如何用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)); 

任何人都可以共享的方法來初始化指針成員函數,而不是構建載體中,然後在其內部產生?

+0

是否好做一個仿函數? – NathanOliver

+0

不,我需要成員函數'IdentifiersGenerator :: getNextIdentifiers' – Patryk

+0

你可以使用函數來獲得它。我在問你是否可以用一個仿函數包裝這個電話。 – NathanOliver

回答

1

的原因,這並不工作:

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

bind1st需要二進制功能,而你提供一元之一。沒有預先包裝的C++ 03解決方案,如果boost::bind是我可以使用的一個選項。

否則,可以編寫自己的工廠,它需要一個指向無效成員函數的指針和一個指向該類的指針,並返回一個調用它的對象operator()

0

(張貼在這裏僅做參考)

好了,我已經意識到我可以使用boost::bind像這樣:

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

綁定是一個真正的「老」,大多過時的方式來實現的東西類似於更好的lambda語法。

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

你也可以使用generate_n

+0

再次,lambda是C++ 11,我已明確標記此問題爲C++ 03 – Patryk

相關問題