Dont getting the approach

how to store the values in forward list in the last(like insertion in the back) if we take data of forward list from the user. i tried but getting error. please help.

#include<bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int a,n,i,k=0;
cin>>n;
forward_list<int>l;
for(i=0;i<5;i++)
{
    cin>>a;
    if(i==0)
    {
        l.push_front(a);
    }

    else 
    {
        l.insert_after(l.begin()+k,a);
        k++;
    }

}

for(auto&it:l)
cout<<it<<"->";

return 0;

}

@shivamgoel150 i have updated this in your code look at this you will understand https://ide.codingblocks.com/s/263388

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

can you please explain the purpose and function of line 12 of code that is (auto k=l.before_begin():wink:

Return iterator to before beginning

Returns an iterator pointing to the position before the first element in the container.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

https://ide.codingblocks.com/s/263388 in this what does the l.before_begin() do

hi @shivamgoel150
It returns an iterator which points to the position before the first element of the forward_list.