Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a noop iterator in the stl?

Tags:

c++

stl

If I wanted to copy the same value across an iterator range, I would think that it would be easy to have a noop iterator where you pass it a value, and when it is incremented, it would not move anywhere. This would allow using the existing std::copy and std::copy_if algorithms.

However, I can't seem to find such a beast. Am I going to have to roll my own?

like image 617
Adrian Avatar asked Dec 13 '25 01:12

Adrian


1 Answers

Use std::fill or std::fill_n algorithm.

Some containers, e.g. std::vector<> and std::list<>, have a constructor with size and initializer:

std::vector<int> v(10, 42); // 42 is the initializer
v.resize(20, 42); // Initialize new elements with 42.
like image 199
Maxim Egorushkin Avatar answered Dec 14 '25 15:12

Maxim Egorushkin