Home > Enterprise >  Using copy algorithm to copy from vector to set [duplicate]
Using copy algorithm to copy from vector to set [duplicate]

Time:10-02

As a purely learning experience I want to be able to use the copy algorithm to copy from a vector to a set. This is what I am trying to do:

vector<int> myVector = {0, 1, 1, 2, 2, 3, 3, 4, 5, 6};
    
// set<int> mySet(myVector.begin(), myVector.end());
// This works, no issues
    
set<int> mySet;
copy(myVector.begin(), myVector.end(), some_inserter_that_will_work(mySet));

Somewhere on the web it was suggested that the inserter function would work but it is giving me the following compile error:

error: no matching function for call to ‘inserter(std::set&)’

CodePudding user response:

You need to use std::inserter in this way, indicating the insertion position as second argument:

copy(myVector.begin(), myVector.end(), inserter(mySet, mySet.end()));
  • Related