Sobes.tech
Junior — Middle

Is it possible to use the push_back method with std::vector without passing elements to it?

sobes.tech AI

Answer from AI

The push_back method in std::vector requires passing the element to be added to the end of the vector. Calling push_back without arguments is impossible and will result in a compilation error, as the method signature requires one parameter — the element to add.

If you need to add a default element, you can use emplace_back() without arguments, which will create the element using the default constructor:

std::vector<int> v;
v.emplace_back(); // adds 0 for int

But push_back() without parameters cannot be used.