Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing vector elements with algorithm and/or functional library

Tags:

c++

I was wondering if there is any way to print elements of vector only with using algorithm and/or functional library. I've done it with std::for_each() but I still need to use lambda function (and with my own named function), is there any way to avoid it? Here is my line of code that I need to replace with that new version:

std::for_each(v.begin(),v.end(), [](int n) { std::cout<<n<<std::endl;});

Thank You.

like image 556
ugursuz Avatar asked Jan 31 '26 05:01

ugursuz


1 Answers

This version doesn't need a lambda:

std::copy(v.begin(), v.end(),
          std::ostream_iterator<int>(std::cout, "\n"));
like image 100
cigien Avatar answered Feb 02 '26 22:02

cigien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!