Re: std::for_each + break
On Wed, 21 Nov 2007 01:38:22 +0000, Sohail Somani wrote:
On Tue, 20 Nov 2007 17:18:24 -0800, Kai-Uwe Bux wrote:
However, it does not actually present any data in support of the claim
that generic algorithms tend to introduce the kind of bug you pointed
out.
I'm done with this thread, but I just want to point out that there was
no bug. That is an intended mode of use for std::transform: Perform an
operation on each element in an array and assign it to another.
Damnit, here you go.
// file.cpp
#include <iostream>
#include <vector>
#include <boost/lambda/lambda.hpp>
#include <algorithm>
namespace bl = boost::lambda;
int main()
{
std::vector<int> v1(10,5);
std::vector<int> v2(10);
// v2[i] = v1[i] + 1 for i in [0,v2.size())
std::transform(v1.begin(),v1.end(),v2.begin(),bl::_1 + 1);
std::for_each(v1.begin(),v1.end(),std::cout << bl::_1 << '\n');
std::for_each(v2.begin(),v2.end(),std::cout << bl::_1 << '\n');
}
Output:
5
5
5
5
5
5
5
5
5
5
6
6
6
6
6
6
6
6
6
6
--
Sohail Somani
http://uint32t.blogspot.com