मैं सीखना चाहता हूं कि बाध्यकारी कार्यों का उपयोग कैसे करें। यहाँ विचार है: मैं इस समारोह जो मानकों पर ले जाता है है:bind1st और bind2nd का उपयोग कैसे करें?
void print_i(int t, std::string separator)
{
std::cout << t << separator;
}
और मैं क्या करना चाहते हैं:
std::vector<int> elements;
// ...
for_each(elements.begin(), elements.end(), std::bind2nd(print_i, '\n'));
लेकिन यह काम नहीं करता है!
यहाँ मैं क्या मिलता है:
/usr/include/c++/4.3/backward/binders.h: In instantiation of ‘std::binder2nd<void()(int, std::string)>’:
main.cpp:72: instantiated from here
/usr/include/c++/4.3/backward/binders.h:138: error: ‘void()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:141: error: ‘void()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:145: error: ‘void()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:149: error: ‘void()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:155: error: ‘void()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:140: error: field ‘std::binder2nd<void()(int, std::string)>::op’ invalidly declared function type
/usr/include/c++/4.3/backward/binders.h: In function ‘std::binder2nd<_Operation> std::bind2nd(const _Operation&, const _Tp&) [with _Operation = void()(int, std::string), _Tp = char]’:
main.cpp:72: instantiated from here
/usr/include/c++/4.3/backward/binders.h:164: error: ‘void()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/bits/stl_algo.h: In function ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Funct = std::binder2nd<void()(int, std::string)>]’:
main.cpp:72: instantiated from here
/usr/include/c++/4.3/bits/stl_algo.h:3791: error: no match for call to ‘(std::binder2nd<void()(int, std::string)>) (int&)’
make: *** [all] Error 1
मैं functor इस्तेमाल कर सकते हैं, लेकिन यह बाध्यकारी उपयोग करने के लिए जल्दी है।
धन्यवाद!
यह प्रश्न से संबंधित नहीं है, इसलिए इसके बजाय एक छोटी टिप्पणी है। किसी श्रेणी को आउटपुट करने के लिए सबसे छोटा कोड शायद इसे 'ostream_iterator' में कॉपी कर रहा है, उदा। 'std :: copy (v.begin(), v.end(), std :: ostream_iterator (std :: cout," \ n "));'। –