I have the following code to assign a value to all the elements of a vector:
x = 100;
for (int i=0;i<vect.size();i++)
{
vect[i] = x;
}
It’s straightforward enough, but I’m wondering if there is a function in the STL that does the same thing; something like for_each, but for assignment.
Use
std::fill:Note if you want to initialize a vector to have all the same value, you can use the appropriate constructor:
assigncan be used to “reset the vector”, but if you’re just making the vector, use the constructor.