9

As with c++11 we have two types of list:

std::list<int> lst = { 1, 2, 3, 4, 5 };

std::forward_list<int> flst = { 5, 4, 3, 2, 1};

As we know that the list is based on the doubly linked list and forward_list is based on the singly linked list.

How should we decide which one to used? Is there any performance benefit of any of the list above other?

Swapnil
  • 1,424
  • 2
  • 19
  • 30
  • 2
    For memory comparison, see this: https://stackoverflow.com/questions/11545058/memory-used-stdlist-v-s-stdforward-list – Azeem Aug 25 '18 at 09:50
  • 5
    [_"Compared to std::list this container provides more space efficient storage when bidirectional iteration is not needed._"](https://en.cppreference.com/w/cpp/container/forward_list). Thus, if you are already set on using a linked list, and if you don't need bidirectional iteration, choose `std::forward_list`, whereas if you need bidirectional iteration capacities, use `std::list`. But before making this decision, make sure to compare if you really want a linked list at all, of if a contiguous storage random-access `std::vector` or `std::array` could be a viable alternative. – dfrib Aug 25 '18 at 09:55
  • 1
    @Azeem note that the accepted answer in that thread is very contrived (to the point of misleading), using a linked list for objects of 1 byte size (in which case the additional pointer in `std::list` will seem extremely memory costly). The non-accepted answer in the linked Q&A makes a more sound comparison. – dfrib Aug 25 '18 at 10:01
  • 1
    The decision is simple. Never use either one. There's always a better data structure. Well, almost always. If you are facing the necessity of using a linked list, you are doing pretty esoteric stuff and SO isn't going to help you. – n. m. could be an AI Aug 25 '18 at 10:14
  • 2
    @n.m. I wouldn't go quite that far, but yes, of you answered every question about std list with "don't use std list" you'd have above a 99% accuracy rate. – Yakk - Adam Nevraumont Aug 25 '18 at 13:13

2 Answers2

14

How should we decide which one to used?

Decide if you need bidirectional iteration. If forward iteration is good enough, use std::forward_list, unless you need to support C++ versions older than C++11, which may only have std::list.

Is there any performance benefit of any of the list above other?

std::forward_list eliminates a pointer per node (with all the attendant benefits for the data cache and memory subsystem), while std::list provides constant-time iterator decrement.

But in practice, neither of these containers is as widely used as one might believe when attending computer science school. The real performance of std::vector is superior for many applications, and its memory usage is always less. More demanding applications requiring lists would do well to consider intrusive lists, which standard C++ does not provide.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

In addition to what john-zwinck said, if you have to use linked-lists, bear in mind the followings when making your decision:

std::forward_list doesn't have the modifiers functions operating at the end due to its nature:

push_back, pop_back, emplace_back

Furthermore, std::forward_list doesn't provide a size member function for more efficiency according to the std::list. size function of the std::list gives the number of elements in constant time by C++11. On the other hand, it requires some extra storage for the internal counter and this makes inserting and removing slightly less efficient. In order to attain the size of a std::forward_list, the distance algorithm can be used with begin() and end() functions but this operation takes linear time.