Saturday, March 15, 2008

C++ foreach

I can never remember the C++ for-each loop syntax. Thus, a helper macro I use:
    #define for_each(type, it, L) for (type::iterator (it) = (L).begin(); (it) != (L).end(); ++(it))
If L has type T, usage is:
    for_each(T, it, L) {
    printf("%d\n", (*it));
If you're only using gcc, one can use __typeof__ [1] to simplify this.


Anonymous said...

You should change your macro to do a pre-increment rather than a post to avoid the temporary the is required to implement a post-increment.

Connelly Barnes said...

Good point. Changed.

fr3@K said...

You really should use Boost.Foreach.

At least look into it.

Connelly Barnes said...

I find the one line macro easier. But, thanks.

Unknown said...

This will not work properly if "type" has commas inside it (for example when "type" is "map")
So I declare the iterators outside the loop, and don't use type in the macro...

Connelly Barnes said...

Good point; I use typedefs for that case.

Sildenafil said...

hey dear blogger do not worry about that at all I have the same problem every time I'm working on C++ code, so that is pretty normal!

Unknown said...

I can never remember the C++ for-each loop syntax. Thus, a helper macro I useguild wars 2 gold
buy guild wars 2 gold
cheap guild wars 2 gold
cheapest guild wars 2 gold
guild wars 2 gold for sale

Unknown said...

make a few plans for the longer term and it is time to be happy. I've learn this publish and if I could I desire to suggest you some attention-grabbing things or advice.
swtor gold
buy swtor gold
cheap swtor gold
tor credits
buy tor credits
cheap tor credits

Unknown said...

Forex Trading Business News updates, Brokers Directory News updates, Latest Currency news updates, latest forex trading business updates, trading updates, forex trading latest news, forex brokers directory, forex brokers list, Dollars news affairs, Stock Markets, stock market news, stock market analysis, technology news, international forex markets, international forex business news and all updates about Forex Trading

Unknown said...

Glad to visit your site. An awesome blog. Nice Information It's really very informative that I wanted ever, thanks for this. Jual Peninggi Badan Alami Bali Ratih Obat Jerawat Obat Asam Urat Alami Kapsul Mengkudu Obat Pelangsing Badan HerbalMadu Hitam Pahit Masker Wajah Alami

Unknown said...

The information you shared through your post is functional. Look, I've just found some useful info at"

Accredited GED Online

Unknown said...

شركة تنظيف كنب ومجالس بالجبيل

Bale said...

The most important thing is to always choose the right game to buy. You can search SWTOR Credits US on Google, and the highest ranked sites are relatively safe. The website I visit frequently is MMOAH. MMOAH continues to serve gamers for countless years. Equipment sold is 100% safe and reliable, the support service is also very enthusiastic, the after-sales service is perfect, and the old customers will definitely get more discounts. You can click here:

عبدالله هادى said...

A house cleaning company in Al-Ahsa depends on the use of the latest modern and advanced methods and methods in all cleaning and sterilization work for homes, by providing the latest equipment, tools and machines that work with tremendous power and superb and amazing capabilities in cleaning
شركة تنظيف منازل بالاحساء
شركة تنظيف موكيت بالاحساء

Air conditioning cleaning company in Al-Ahsa provides all the cleaning works and services for air conditioners, as it carries out complete and comprehensive cleaning of the air conditioner from the inside and outside, depending on the latest modern and advanced methods and methods.
شركة تنظيف مكيفات بالاحساء

There are a lot of people asking and looking for a specialized party in cleaning tanks, so we have provided you with a tank cleaning company in Al-Ahsa, tank cleaning services of all kinds.
شركة تنظيف خزانات بالاحساء

Al-Ahsa Insect Control Company is one of the first leading companies in the field of combating, attacking and exterminating insects, through its excellence and superiority in the control operations always everywhere and its superior ability to get rid of all kinds of insects, whether flying or crawling.شركة مكافحة حشرات بالاحساء
شركة مكافحة الصراصير بالاحساء

The termite control company in Al-Ahsa is interested in providing all the ingredients and capabilities necessary to combat and attack termites of all kinds, because the problem of ants is one of the problems facing many, many homes and homes.شركة مكافحة النمل الابيض بالاحساء

A company that sprays pesticides in Al-Ahsa can combat any type of insects that haunt homes and cause anxiety, terror, tension and annoyance for the people of the house. and annihilated her on the spot.شركة رش مبيدات بالاحساء