c++ replace elements in linked list

Does someone know how to write a function that replaces the first one with the last element in a dynamic, one-way linked list.
1
2
3
4
5
6
7
8
9
10
template <typename Item>
void replace_first_by_last( std::forward_list<Item> & my_forward_list )
{
    auto last = my_forward_list.begin();
    for( auto itr = my_forward_list.begin();  itr != my_forward_list.end();  ++itr)
    {
        last = itr;
    }
    *my_forward_list.begin() = *last;
}
Last edited on
Topic archived. No new replies allowed.