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.

58 comments:

nathan 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.

Avi 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.

Adi said...

Find Internet Marketing resource hare Online Marketing Strategy | Internet Marketing Tools | Online Marketing Campaign | Online Marketing Business | Online Marketing System | Online Business | Online Home Business | Online Business Tips | Internet Marketing Online

Adi said...

Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa pages. Thank you So much.
Oes Tsetnoc | Semangat Mengembalikan Jati Diri Bangsa

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!

zhengbin said...

Kauf und Verkauf von Gold in den thomas sabo Goldmarkt hat viel an Popularität gewonnen, sind thomas sabo shop Gold-Nuggets und Goldmünzen im Handel erhältlich herkömmlichen thomas sabo jewellery Wege der Kauf und Verkauf bei niedrigen wenn thomas sabo schmuck high.Tips Gold und Silver.Have eine thomassabo online shop klare Vorstellung davon, warum die Schmuck thomas sabo onlineshop verkaufen muss, um verkauft werden und Scout für die sabo schmuck entsprechende buyer.Volume aus Gold oder thomas sabo shop online Silber zu einem Zeitpunkt verkauft werden bestimmen den Verkaufspreis und die Verhandlungsmacht thomas sabo charm club der seller.Purity bestimmt auch den Endpreis der verkauften Artikel.

Emma said...

Well I was looking some legal procedure for my case testimonial about Handwriting expert and Handwriting analyst in Google although I have done lot of research and development about Handwriting analysis before suddenly my search stopped me at your blog I really enjoyed reading it. I'm supposed to be somewhere else in a minute but I stuck to reading the story. I love Your Blog……..

diablo ti said...

Haha, sunny day again! You know iam more excited when reading your articles here, you really made my day. What's more, you just tell the point of this question and it is informative enough now, thanks again for sharing here. runescape gold for sale , runescape gold

Popular shoes said...

It is appropriate time to 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.
More tags:
wow gold buy cheap

wow buy gold cheap

wow buy cheap gold

wow gold fast cheap

cheap wow gold fast


han yu 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

han yu 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

somnus11258 said...

Such a nice post, We loved it a great deal!I am a china tour lover,You can learn more: China vacation packages | China city tours | China Travel Agency

muhammed waqar said...

What is Lol...? Every thing you want in Lol and Troll, Now you Get all in one Network, ThatIsLol.. Lol Pictures, Lol Videos, Lol Peoples, Funny Peoples, Troll Images, troll pictures, funny pictures, Facebook pictures, facebook funny pictures, facebook lol pictures, Funny videos and Much More only Laughing out of Laughing
lolsgag.com

Azhar Hussain Qureshi said...

Latest cars and vehicles, Latest Mazda Models, Racing Cars, International Sport Cars, Concept Cars, PS-Pod, Strange Vehicles, Nissan, Royce Corniche, Ford Concept Cars, Strange Vehicles, Mercedes and More Sport Cars and Vehicles with Pictures and Info
WorldLatestVehicles.com

saadi said...

Internet World Wide Online Jobs and Business Plans. Genuines Works of Data Entry, Copy Pasting, Add Posting, Clicking, Web Surfing, Website Visiting, Article Sharing, Data Sharing, Google Business Plan and Much More Business Plans.
www.jobzcorner.com

hasnain raza said...

Lols Gag is the the Best Lol Network Ever, where you can every thing is lol and Funny, Troll Images, Prank Peoples, Funny Peoples, funny planet, funny facts, funny cartoons, funny movies pics, iphone funny, funny jokes, Prank Images, Fail Pictures, Epic Pictures, Lols and Gags, Lol Pictures, Funny Pictures, Lol is the Laugh out of Laugh where you can Fun Unlimited and Laughing Unlimited.
lolsgag.com

faisal said...

lol and Funny, Troll Images, Prank Peoples, Funny Peoples, funny planet, funny facts, funny cartoons, funny movies pics, iphone funny, funny jokes, Prank Images, Fail Pictures, Epic Pictures, Lols and Gags, Lol Pictures, Funny Pictures, Lol is the Laugh out of Laugh where you can Fun Unlimited and Laughing Unlimited.
LolsGag.Com

Anas Sana ur rahman 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
ForexAffairs.blogspot.com

ayaz said...

Best Business Investment Plan without any work, You can Invest as low as 1$ Dollar to 100$ Dollars and Get Daily profit of 10.00%, Guaranteed Payment with Minimum Earning is $1.
Join Now for Make Perfect Business and Earn Money online from home.
www.hotfxearnings.com

adnan khan said...

Lols Gag is the the Best Lol Network Ever, where you can every thing is lol and Funny, Troll Images, Prank Peoples, Funny Peoples, funny planet, funny facts, funny cartoons, funny movies pics, iphone funny, funny jokes, Prank Images, Fail Pictures, Epic Pictures, Lols and Gags, Lol Pictures, Funny Pictures, Lol is the Laugh out of Laugh where you can Fun Unlimited and Laughing Unlimited.
lolsgag.com

syedkazim ali said...

The Best Lol n Troll Network with the Name of Lols Gag... Troll Images, Prank Peoples, Funny Peoples, funny planet, funny facts, funny cartoons, funny movies pics, iphone funny, funny jokes, Prank Images, Fail Pictures, Epic Pictures, Lols and Gags, Lol Pictures, Funny Pictures, Lol is the Laugh out of Laugh where you can Fun Unlimited and Laughing Unlimited.
LolsGag.Blogspot.Com

daniyal raza said...

Genuines Works of Data Entry, Copy Pasting, Add Posting, Clicking, Web Surfing, Website Visiting, Article Sharing, Data Sharing, Google Business Plan and Much More Business Plans
www.jobzcorner.com

ayaz said...

Super Cars and World Latest vehicles, Latest Mazda Models, Racing Cars, International Sport Cars, Concept Cars, PS-Pod, Strange Vehicles, Nissan, Royce Corniche, Ford Concept Cars, Strange Vehicles, Mercedes and More Sport Cars and Vehicles with Pictures and Info
WorldLatestVehicles.com

Khaliqah Rahma 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

Syed Kazim Ali said...

The Best Lol n Troll Network with the Name of Lols Gag... Troll Images, Prank Peoples, Funny Peoples, funny planet, funny facts, funny cartoons, funny movies pics, iphone funny, funny jokes, Prank Images, Fail Pictures, Epic Pictures, Lols and Gags, Lol Pictures, Funny Pictures, Lol is the Laugh out of Laugh where you can Fun Unlimited and Laughing Unlimited.
LolsGag.Blogspot.Com

Syed Kazim Ali said...

Latest All Hot Current Affairs, Bollywood News updates, funny and lol pictures
HotCurrentAffairs

ali said...

Find Best Online Home based Jobs
www.jobzcorner.com

Ahsan Afsar said...

World Most Popular and Top Amazing Speedy Cars
TopAmazingCars.BlogSpot.Com

Syed Kazim Ali said...

Online Business with hourly profit, Just Invest and Rest
AllTimeProfit.com

Ahsan Afsar said...

Online Jobs of Data Entry and Copy Pasting with Jobz Corner
JobzCorner.com

muhammad yasir bawani said...

Find home based jobs of link building, facebook marketing, add marketing, add clicking and much more jobs.
www.jobzcorner.com

irfan a.r said...

Online Easy Jobs from home with data entry, copy pasting, facebook jobs
www.jobzcorner.com

hasnain raza said...

Online Corner is the best Platform to earn money online from internet, Just Join Now and start earnings
OnlineCornerz.com

hasnain raza said...

Get your website on google top 10 Results, Best Search Engine Optimization Company in Pakistan
Contact Now
Skype : Jobz.Corner
www.jobzcorner.com

hasnain raza said...

Business at home...??? want to join the best business without any work, just invest and rest
www.earningsclub.com

Syed Kazim Ali said...

Online Business with hourly profit, Just Invest and Rest
AllTimeProfit.com

ahmedraza moon said...

Get Facebook Likes on your fb page, likes on your facebook pictures, followers on your facebook id, shares of your facebook posts, every thing is available here, visit for more details
www.jobzcorner.com

ahmedraza moon said...

Get Facebook Likes on your fb page, likes on your facebook pictures, followers on your facebook id, shares of your facebook posts, every thing is available here, visit for more details
www.jobzcorner.com

ahmedraza moon said...

Get Facebook Likes on your fb page, likes on your facebook pictures, followers on your facebook id, shares of your facebook posts, every thing is available here, visit for more details
www.jobzcorner.com

hasnain raza said...

Online Jobs, Just Post a Comment on any blog site and earn $0.06 per Link, Just Like this post, Visit and Click on Link Relation
JobzCorner.com

adnan khan said...

Find Best online home based jobs, data entry, copy pasting, facebook and clicking jobs
JobzCorner.com

Syed Kazim Ali said...

Best Business Club of Earnings, Hourly Stable Plans Like 1% Hourly for 120 Hours, Total 120% Profit within 5 Days
EarningsClub.com

muhammad yasir bawani said...

Find home based jobs of link building, facebook marketing, add marketing, add clicking and much more jobs.
www.jobzcorner.com

hasnain raza said...

Play Games and Earn Money online from home, best add clicking website in the world
PaidVerts.com

Ahsan Afsar said...

Find best business plan from home without any work, just invest $1 and get 120% Total Profit within a week
EarningsClub.Com

Syed Kazim Ali said...

Earn Money on without investment, just join free add clicking websites and earn upto $10 per Day, Join Now
jobzcorner.com

ahmedraza moon said...

Earning is only for you, just spend 1 hour daily and earn upto $35 Daily with just clicking job, Join Now
adsclickearning.com

ahmedraza moon said...

Instant Payment without any Risk, Just Invest and Rest of your home, Just Invest as low as $5 and Get Instant Profit, Join Fast
HotProfitOnline.com

Ahmed Hassan said...

Hot Forex Investments where you can earn profit without any work, just invest on forex and get profit daily upto 12%, Join Now
HotFxInvest.com

Bilal Shah said...

Online Jobs is easy now for you, just spend few hours on internet and get unlimited income with Data entry copy pasting facebook and clicking jobs
JobzCorner.com

Saqib Khan said...

Find best online jobs ever where you can earn monthly earning without any risk, part time data entry copy pasting and facebook jobs, Join now
RapidIncomeCorner.com

Learn Chinese language on hanbridgemandarin said...

The best place to learn mandarin Chinese is in China. However, we understand that it isn't always possible to move here to study Chinese language. The next best thing is to study with our experienced teachers in a virtual classroom. Online students enjoy the same excellent way of Chinese language course and custom designed courseware that we provide for our face to face clients.

Ethan Harry said...

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

Accredited GED Online

Khaled Soliman said...

وكالة نيوز
مباراة
1437
مصر
منوعات