Teo
Teo

Reputation: 3213

Replace a range of elements in a vector with a value

I want to replace any value from a position a to a position b inside my vector.

I read about replace function, but I have to specify the value that I want replaced. I need to replace any values.

For example if the vector is: 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0, and I want to replace all the elements from position 0 to position 8 with another value, how can I use that replace function?

Now I'm using a for loop to do that, but my program is very slow.

Upvotes: 0

Views: 1984

Answers (1)

acraig5075
acraig5075

Reputation: 10756

Probably std::fill does what you want.

int newValue = 0;
std::fill(std::begin(v), std::begin(v) + 8, newValue);

Upvotes: 3

Related Questions