c++algorithmstdvectorstdforward-list

How to insert std::vector or array to a std::forward_list without using any loop?


forward_list<int> listOne;
forward_list<int> listTwo;
vector<int> arr = {2,4,3};
forward_list<int>::iterator it;

In the code mention above, I want to insert a std::vector in listOne and I tried using insert_after function.


it = listOne.begin();
listOne.insert_after(it,arr);

But it didn't work.

I want to know that, is there a way to add a std::vector or array in a std::forward_list without any loop ?


Solution

  • You could use a std::copy with std::front_inserter

    copy(arr.rbegin(), arr.rend(), front_inserter(listOne));
    

    Working demo