How to find the intersection of two STL sets?

You haven’t provided an output iterator for set_intersection template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result ); Fix this by doing something like …; set<int> intersect; set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(intersect, intersect.begin())); You need a std::insert iterator since the set is as of … Read more

Is the C++ std::set thread-safe?

STL has no built in thread support, so you’ll have to extend the STL code with your own synchronization mechanisms to use STL in a multithreaded environment. For example look here: link text Since set is a container class MSDN has following to say about the thread safety of the containers. A single object is … Read more

How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?

Sounds like a job for std::copy_if. Define a predicate that keeps track of elements that already have been processed and return false if they have. If you don’t have C++11 support, you can use the clumsily named std::remove_copy_if and invert the logic. This is an untested example: template <typename T> struct NotDuplicate { bool operator()(const … Read more