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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -