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

Given a set of integers:

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

Now I want to insert the integer to a vector of integers under a certain condition:

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

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

But I got an error:

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

Why?



Solution 1:[1]

As others have mentioned in the comments, you should not be using insert in this case, you should be using push_back

vectorB.push_back(*it);

You would typically use insert if there is a specific position that you want to insert the new element. If you are not interested in adding the element to a specific position, then you can use push_back to (as the name suggests) add the element to the end of the vector.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cory Kramer