无法将集合的元素插入到 C++ 中的向量中

Fail to insert the element of the set to the vector in c++

给定一组整数:

set<int> setA = {1,2,3,4,5};

现在我想在特定条件下将整数插入到整数向量中:

vector<int> vectorB;
for (set<int>::iterator it = setA.begin(); it != setB.end(); it++){

  if (*it % 2 == 0){
  }else{
    vectorB.insert((*it));
    count += 1;
  }
}

但是我得到一个错误:

error: no matching function for call to 'std::vector<int>::insert(const int&)'

为什么?

正如其他人在评论中提到的,您不应该使用 insert in this case, you should be using push_back

vectorB.push_back(*it);

如果要插入新元素的特定位置,您通常会使用insert。如果您对将元素添加到特定位置不感兴趣,那么您可以使用 push_back(顾名思义)将元素添加到向量的末尾。