I'm working with std::vectors for a graphical program. Those vectors contain positions on the screen, and they are sorted. Now I'd like to merge them together and keep the actual sorting, while removing possible duplicates, something like this:
vector1 : [2, 6, 10]
vector2 : [1, 5, 6, 10]
result : [1, 2, 5, 6, 10]
STL has this concept of separation of containers and algorithms, so while
std::vector indeed does not have members to sort or merge it, STL provides all the required algorithms via non-member function templates that handle iterators.
E.g. to sort a vector you would call
Check algorithm header for further reference, namely
To merge and remove duplicates you can use
std::set_union, which is probably your best bet.
Here is a tutorial on iterators, though for this specific task you would only need self-explanatory
For removing duplicates from a single container you would usually use
std::unique_copy(), as @unwind mentioned.
There is one caveat with
std::unique() and other "removing" algorithms like
std::remove(), which stems from that "separation of containers and algorithms" that I mentioned:
an algorithm has no means to actually remove elements from the container - it's been given an iterator or a range, but it has no idea about the actual type and implementation of the container.
So instead common approach is to just move the elements intended for removal to the end of the range, and then return the iterator to the first of these elements. Then you can call another function to do actual removal (notice how this time it will be a container method).
This is how it's done with
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
std::unique_copy will not require this trick, but it will pretty much copy the whole vector, so it makes sense only if you intended to copy it anyway.