Std::forward_list is designed to have very fast insertions and removals anywhere in the container. It does this by giving each element a pointer to the next element.
Elements can be stored non-contiguously in memory. The forward list only supports forward iterators and 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. The vector container is almost always a better choice than the forward list.
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.
#include <iostream>
#include <forward_list>
#include <algorithm>
int main() {
std::forward_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;
}