使用复制算法从向量复制到集合
Using copy algorithm to copy from vector to set
作为一种纯粹的学习体验,我希望能够使用 copy
算法从向量复制到集合。这就是我想要做的:
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));
网上某处建议 inserter
函数可以工作,但它给我以下编译错误:
error: no matching function for call to ‘inserter(std::set&)’
需要这样使用std::inserter
,表示插入位置作为第二个参数:
copy(myVector.begin(), myVector.end(), inserter(mySet, mySet.end()));
作为一种纯粹的学习体验,我希望能够使用 copy
算法从向量复制到集合。这就是我想要做的:
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));
网上某处建议 inserter
函数可以工作,但它给我以下编译错误:
error: no matching function for call to ‘inserter(std::set&)’
需要这样使用std::inserter
,表示插入位置作为第二个参数:
copy(myVector.begin(), myVector.end(), inserter(mySet, mySet.end()));