C++, copy set to vector
Solution 1:
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 least large enough to hold the input range.
std::back_inserter
creates an output iterator that calls push_back
on a container for each element, so each element is inserted into the container. Alternatively, you could have created a sufficient number of elements in the std::vector
to hold the range being copied:
std::vector<double> output(input.size());
std::copy(input.begin(), input.end(), output.begin());
Or, you could use the std::vector
range constructor:
std::vector<double> output(input.begin(), input.end());
Solution 2:
Just use the constructor for the vector that takes iterators:
std::set<T> s;
//...
std::vector v( s.begin(), s.end() );
Assumes you just want the content of s in v, and there's nothing in v prior to copying the data to it.
Solution 3:
here's another alternative using vector::assign
:
theVector.assign(theSet.begin(), theSet.end());