Std::list is designed to have very fast insertions and removals anywhere in the container. It achieves this by providing pointer pointer the next and previous items in the list
Elements can be stored non-contiguously in memory. The forward list only supports forward and reverse iterators, they don’t provide random access operators. The poor memory locality makes it hard to do CPU optimizations when traversing the list. We will have to do a linear search when looking for an element.
Some common methods of the forward_list type are push_front, which adds a value to front of the list, pop_front, which removes a value from the front of a list, and front, which returns the first value as well as push_back and pop_back. It also has the front and back methods to get the first and last elements. Like forward_list, the vector will usually be a better choice.
#include <iostream>
#include <list>
#include <algorithm>
int main() {
std::list<int> ints {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto it = std::find(ints.begin(), ints.end(), 5);
if (it != ints.end()) {
std::cout << "Found 5!" << std::endl;
} else {
std::cout << "Didn't find 5" << std::endl;
}
return 0;
}