set_intersection of two sets

This is a discussion on set_intersection of two sets within the C++ Programming forums, part of the General Programming Boards category; set_intersection - C++ Reference
has an example of finding the set_intersection, but it declares the intersection set as
Code:
vector<int> ...

set_intersection of two sets

why exactly we need to specify the memory, as I know vector is dynamic array, this works fine even when I don't specify for smaller set intersection but when the intersection comes out to be bigger it shows segmentation fault, How to fix it without killing the dynamic property of the vector, or is there any better way to find set intersection, its better for us to use STL library if am not wrong.

You need to use, say, a back_insert_iterator (use std::back_insert to construct one). Iterators aren't dynamic; they require that there is enough storage.
Dynamic iterators do exist, and you can find most of them in the <iterator> header.

A normal iterator is a glorified pointer. It points to existing elements inside a container. Thus, you can only write to a specific element in a container. You cannot add new elements to a container via a normal iterator. You can only overwrite.
However, back_insert_iterator is special. What is basically is is an iterator associated with a container, and when you write to such an iterator, it calls push_back on its associated container. So it will add elements.
There are other such iterators, such as stream iterators.

What I find amazing is that you tried to write this statement in the middle of a set_intersection call.

The iterator basically contains this and calls this->push_back(whatever); on a dereference operation, because it overloaded the operators the algorithm was written with. That's why a back_inserter() works and what you tried does not.