C++, copy set to vector -
i need copy std::set std::vector:
std::set <double> input; input.insert(5); input.insert(6); std::vector <double> output; std::copy(input.begin(), input.end(), output.begin()); //error: vector iterator not dereferencable where problem?
you need use back_inserter:
std::copy(input.begin(), input.end(), std::back_inserter(output)); std::copy doesn't add elements container inserting: can't; has iterator container. because of this, if pass output iterator directly std::copy, must make sure points range @ least large enough hold input range.
std::back_inserter creates output iterator calls push_back on container each element, each element inserted container. alternatively, have created sufficient number of elements in std::vector hold range being copied:
std::vector<double> output(input.size()); std::copy(input.begin(), input.end(), output.begin()); or, use std::vector range constructor:
std::vector<double> output(input.begin(), input.end());
Comments
Post a Comment