Mar 28, 2011 at 7:33am UTC
os : windows xp sp3(32bits)
compilers : tdm gcc4.5(minGW)
this one is okay
1 2 3 4 5 6
std::tr1::array<size_t, 10> anchor;
std::iota(anchor.begin(), anchor.end(), 0);
std::tr1::function<void (size_t, std::vector<size_t>&)> f = [](size_t value, std::vector<size_t>& temp){ temp.push_back(value);};
std::vector<size_t> vec;
std::for_each(anchor, anchor + 5, std::tr1::bind(f, std::tr1::placeholders::_1, std::ref(vec) ));
std::copy(vec.begin(), vec.end(), std::ostream_iterator<size_t>(std::cout, ", " ) );
this one wouldn't work
1 2 3 4 5 6
std::tr1::array<size_t, 10> anchor;
std::iota(anchor.begin(), anchor.end(), 0);
std::vector<size_t> vec;
std::tr1::function<void (size_t, std::vector<size_t>&)> g = std::tr1::bind(f, std::tr1::placeholders::_1, std::ref(vec) );
std::for_each(anchor, anchor + 5, g);
std::copy(vec.begin(), vec.end(), std::ostream_iterator<size_t>(std::cout, ", " ) );*/
The error messages are
no match for call to '(std::tr1::function<void(unsigned int, std::vector<unsigned int>&)>) (unsigned int&)'
candidate is: _Res std::tr1::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = void, _ArgTypes = {unsigned int, std::vector<unsigned int>&}]
Thanks a lot
ps:I know it could done by
std::for_each(anchor, anchor + 5, [&](size_t value){ vec.push_back(*value); });
I want to try it by another way
Last edited on Mar 28, 2011 at 7:45am UTC
Mar 28, 2011 at 7:56am UTC
Besides, I find out in my compilers there exist
std::bind; std::tr1::bind; std::function; std::tr1::function; std::ref; std::tr1::ref; std::cref; std::tr1::cref
What are the difference between them?
Last edited on Mar 28, 2011 at 7:57am UTC