Problem with counting iterator and bind

I have this:
typedef LegacySequentialContainer<string> LegacyStringVector;
LegacyStringVector lsv;
// ... filling lsv with strings ...
I realized this:
boost::counting_iterator<int> it(0);
boost::counting_iterator<int> end(lsv.length());
while (it < end)
cout << boost::bind(&LegacyStringVector::operator[], v3, _1)(*it++) << " ";
cout << "\n";
and it works. However, I wish to do the same but using std::copy or
std::for_each instead.
I tried the following but none is able to compile:
copy(boost::counting_iterator<int>(0),
boost::counting_iterator<int>(v3.size()),
boost::bind(ostream_iterator<string>(cout, " "),
&vector<string>::operator[],
v3,
_1));
copy(boost::counting_iterator<int>(0),
boost::counting_iterator<int>(v3.size()),
boost::bind(ostream_iterator<string>(cout, " "),
boost::bind

Maitre Bart

Maitre Bart wrote:
Maitre Bart
writes: Typos corrected:
I have this:
typedef LegacySequentialContainer<string> LegacyStringVector;
LegacyStringVector lsv;
// ... filling lsv with strings ...
I realized this:
boost::counting_iterator<int> it(0); boost::counting_iterator<int> end(lsv.length()); while (it < end) cout << boost::bind(&LegacyStringVector::operator[], lsv, _1)(*it++) << " "; cout << "\n";
and it works. However, I wish to do the same but using std::copy or std::for_each instead.
I tried the following but none is able to compile:
copy(boost::counting_iterator<int>(0), boost::counting_iterator<int>(lsv.size()), boost::bind(ostream_iterator<string>(cout, " "), &vector<string>::operator[], lsv, _1));
std::copy doesn't take a function object as it third argument, just an iterator. Try std::transform: std::transform( boost::counting_iterator<int>(0), boost::counting_iterator<int>(lsv.size()), ostream_iterator<string>(cout, " "), boost::bind( &LegacyStringVector::operator[], lsv, _1) );

Peter Dimov wrote:
std::transform( boost::counting_iterator<int>(0), boost::counting_iterator<int>(lsv.size()), ostream_iterator<string>(cout, " "), boost::bind( &LegacyStringVector::operator[], lsv, _1)
Make that boost::bind( &LegacyStringVector::operator[], &lsv, _1) or boost::bind( &LegacyStringVector::operator[], boost::cref(lsv), _1) to not copy the whole lsv.
);
participants (2)
-
Maitre Bart
-
Peter Dimov