error: passing const xxx as ‘this’ argument of member function discards qualifiers

The objects in the std::set are stored as const StudentT. So when you try to call getId() with the const object the compiler detects a problem, mainly you’re calling a non-const member function on const object which is not allowed because non-const member functions make NO PROMISE not to modify the object; so the compiler … 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

c++ STL set difference

Yes there is, it is in <algorithm> and is called: std::set_difference. The usage is: #include <algorithm> #include <set> #include <iterator> // … std::set<int> s1, s2; // Fill in s1 and s2 with values std::set<int> result; std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(result, result.end())); In the end, the set result will contain the s1-s2.

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

C++, copy set to vector

You need to use a back_inserter: std::copy(input.begin(), input.end(), std::back_inserter(output)); std::copy doesn’t add elements to the container into which you are inserting: it can’t; it only has an iterator into the container. Because of this, if you pass an output iterator directly to std::copy, you must make sure it points to a range that is at … Read more