Arrays and vectors do not sort their payload objects themselves. But if we need that, this does not mean that we always have to switch to data structures, which were designed to do that automatically. If an std::vector is perfect for our use case, it is still very simple and practical to add items to it in a sorting manner.
Keeping std::vector instances sorted
How to do it...
In this section, we will fill an std::vector with random words, sort it, and then insert more words while keeping the vector's sorted word order intact.
- Let's first include all headers we're going to need.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include...