template function to change container

Sep 19, 2013 at 9:32pm
I find myself changing containers a lot. Can someone show me how to write a template function to carry out something like this:
1
2
3
std::set<A> mySet;
for (std::list<A>::iterator it = myList.begin(); it != myList.end(); ++it)
    mySet.emplace (*it);

Last edited on Sep 19, 2013 at 9:52pm
Sep 19, 2013 at 9:37pm
What would be the desired effect of calling such function? It is specified to take both containers by value and return nothing.

(after the OP edit)
why not just construct directly: std::set<A> mySet(myList.begin(), myList.end()); ?
Last edited on Sep 19, 2013 at 10:29pm
Sep 19, 2013 at 9:50pm
Ok, this is an example I want to do with just one line:

1
2
3
std::set<A> mySet;
for (std::list<A>::iterator it = myList.begin(); it != myList.end(); ++it)
    mySet.emplace (*it);


How to write a function to do this with all possible container changes?
Last edited on Sep 19, 2013 at 9:51pm
Sep 19, 2013 at 9:55pm
<algorithm>
<iterator>
 
std::copy(Container1.begin(), Container1.end(), std::inserter(Container2, Iterator));
Topic archived. No new replies allowed.