I have a for loop in the below code and I would like to implement it using std::for_each. I have implemented it. Could someone please tell me if that is the best way to do it using std::for_each? If not, could you please suggest the right one?
#include <vector>
#include <cstdint>
#include <string>
#include <algorithm>
#include <iostream>
#include <sstream>
int main()
{
std::vector<std::uint32_t> nums{3, 4, 2, 8, 15};
std::stringstream list1;
for (auto n : nums)
{
list1 << n<<",";
}
//Is this the right way to do using std::for_each so that above for loop can be done in 1 line??
std::for_each(nums.begin(),nums.end(),[&list1](std::uint32_t n){ list1 << n << ","; });
}