This example :
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
int v[] = { 1, 2, 3 };
std::copy( &v[0], &v[3], std::ostream_iterator< int >( std::cout, "\n " ) );
}
produces next output :
1
2
3
Is there a way to change the example to make it produce next output?
1
2
3
PS I know I could use the for loop, but I am interested in a solution that uses algorithms and iterators.
There is no way to do this with std::ostream_iterator. (IMHO, there should
be, but it's not there.) If you don't mind writing an extra small
function or class,
you can use std::transform, e.g.:
struct FormatString
{
std::string operator()( std::string const& original ) const
{
return ' ' + original + '\n';
}
};
// ...
std::transform(
v.begin(), v.end(),
std::ostream_iterator<std::string>( std::cout ),
FormatString() );
If you have C++11, you can use a lambda for the FormatString.
I find the need for this occurs often enough that I've written a
PatsubstTransformer—a functional object which basically
implements the $(patsubst...) function of GNU make. So I would just
have to write:
std::transform(
v.begin(), v.end(),
std::ostream_iterator<std::string>( std::cout ),
PatsubstTransformer( "%", " %\n" ) );
I find I use this a lot. (I also find using std::transform more
appropriate than std::copy, since what I'm outputting is a
transformation.)
If you want to use C++11 you can use a lambda.
e.g like this:
int v[] = { 1, 2, 3};
std::for_each( &v[0], &v[3], [](int i){ std::cout << " " << i << "\n";} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With