In C++ I need to iterate a certain number of times, but I don\'t need an iteration variable. For example:
for( int x=0; x<10; ++x ) {
/* code goes her
You could use the STL together with a lambda expression.
#include
#include
int main() {
int a[] = {1,2,3,4,5,6};
std::for_each(std::begin(a),
std::end(a),
[](int){std::cout << "Don't care" << std::endl;});
}
This approach also works for arbitrary containers such as vectors or lists. Let vector
, then you'd call a.begin()
and a.end()
. Note that you can also use a function pointer instead of a lambda expression.
The above preserves your notion of using a foreach, while not complaining about an unused parameter.