tags:

views:

132

answers:

2

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.

+11  A: 

Use std::fill:

std::fill(vect.begin(), vect.end(), 100);

Note if you want to initialize a vector to have all the same value, you can use the appropriate constructor:

std::vector<int> v(5, 100); // 5 elements set to 100

assign can be used to "reset the vector", but if you're just making the vector, use the constructor.

GMan
If you want to fill an existing vector with fewer elements than it already has and trim the rest to save space then use the swap idiom: `vector<int>(3,25).swap(v);`
wilhelmtell
^^ ... the swap idiom will trigger a reallocation, I should mention, but will result in less memory used for the vector. So ultimately it's a good idea only if your intention is to cut back on memory use, not to fill a vector with (coincidentally fewer) values.
wilhelmtell
+7  A: 
vect.assign(n, x);

n is the number of elements.

x is the value to fill with.

The difference from std::fill is that you supply the length for this. std::fill would use the previous length of the vector. (Actually, it would use whatever segment of the vector that you tell it to, but using vect.begin() and vect.end() as in the above example would use the previous length.) Depending on the situation, you will want to use one or the other.

Zifre
The other difference is that `assign()` is a member of `vector` while `fill()` is part of the more generic algorithms library which works with any forward iterators (or an output iterator if you use a different signature). I think could reasonably expect `vector::assign()` to perform better than `std::fill()`.
Michael Burr
@Michael: Probably. The reason I chose `fill` was because it matches the code in the question, though arguably I suppose one could do `vect.assign(vect.size(), x);`. Then again, `std::fill` could be specialized for random access iterators, yielding the same result performance-wise.
GMan
@Michael and GMan: I doubt that most standard libraries optimize enough for it to make a difference in speed (although `assign` has the potential to be faster because it could potentially use SIMD, which I don't think `fill` could, unless the compiler is really smart). I think you would want to avoid `vect.assign(vect.size(), x)` because it might reallocate the internal array, depending on the implementation. The reason I chose `assign` was because it is the function that I use more, along with the constructor, which is very similar. `fill` probably makes more sense in this case.
Zifre
`vect.assign(vect.size(), x)` is guaranteed not to reallocate. I'm sure all modern implementations will optimize `fill` for random access iterators, generating the same code as `vect.assign(vect.size(), x)`, and therefore getting the same optimizations. I'll test some stuff out later to make sure.
GMan