Unusual control structure

Does C++ have something like

1
2
3
4
for (i = {1, 2, 3, 17, 39, 151, etc})
{
	printf("%d\n", i);
}


For same cases (complex code) using arrays in "for" and "while" loops is very inpractical.

1
2
3
4
5
array = {1, 2, 3, 17, 39, 151, etc};
for (i = 0; i < array.size; i++)
{
	printf("%d\n", array[i]);
}


Best regards,
Peter
I think C++0x will introduce something similar

http://www.research.att.com/~bs/C++0xFAQ.html#for
Last edited on
Anything new before giving up :)?
Nope. This is the closest:
1
2
3
4
5
6
7
8
9
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(17);
v.push_back(39);
v.push_back(151);
for (size_t a=0;a<v.size();a++)
    std::cout <<v[a]<<std::endl;
Last edited on
1
2
3
4
5
6
7
#include <boost/array.hpp>
#include <iostream>

typedef boost::array<int,6> Array;
Array a = { 1, 2, 3, 17, 39, 151 };
for( Array::const_iterator i = a.begin(); i != a.end(); ++i )
    std::cout << *a << std::endl;


or better yet

1
2
3
4
5
6
7
8
#include <algorithm>
#include <boost/array.hpp>
#include <boost/lambda.hpp>
#include <iostream>

boost::array<int,6> a = { 1, 2, 3, 17, 39, 151 };
std::for_each( a.begin(), a.end(),
    std::cout << boost::lambda::_1 << '\n' );

Topic archived. No new replies allowed.