iterator and algorithm

i have a question about std::for_each should i be using this with list or should i use something different when using ostream and i get error but it doesn't capture it.I'm trying to use things form the iterator library and algorithm library need help basically what i want to do is just to print the name

1
2
3
4
5
                os << name << std::endl;
		std::for_each(example.begin(),example.end(), [](int&i)
		{
		    os << i << std::endl;//error:os not capture
		});
Write the captures inside the [] of the lambda.
[&os] - captures os by reference.
[&] - automatically captures by reference all the variables that need to be captured.


Instead of std::for_each you might find it easier using a range-based for loop.

1
2
3
4
for (int i : example)
{
	os << i << std::endl;
}

Last edited on
Topic archived. No new replies allowed.