打酱油的萧萧

一个打酱油的程序员
C++ 0X学习:Lambda

C++ 0X学习:for的新语法使用心得

萧萧 posted @ 2011年12月04日 22:44 in C/C++ with tags c++ C++0x c++11 , 26720 阅读

在 C++11 里面,for 有了更方便的写法,具有范围的概念。类似于脚本语言的 for xx in xxx 之类的语法。

先看一个小例子:

int array[5] = {1, 2, 3, 4, 5};
for ( int x : array ) {
  std::printf("%d\n", x);
}

在这个例子里面,循环打印数组的元素。

for 的 “:” 左边,是用作范围迭代的变量,右边是被迭代的范围。

右边可以是 std::vector,std::map等等。

在范围是 STL 容器的时候,需要注意的是,范围迭代的变量,并不是 STL 容器的迭代器,而是 STL 容器里保存数据的数据结构,

比如 std::map 返回的迭代变量是 std::pair,所有如果这样写不会修改容器里面的内容

std::map<int, std::string> my_map{ {1, "xiao"}, 
                                   {2, "xiao"}, 
                                   {3, "xiao"} };

for ( auto iter : my_map ) {
  iter.second = "I'm Xiaoxiao";
}

在这里,iter 是一个 std::pair<const int, std::string> (谢谢egmkang的指出错误)变量,所以修改了它,并不会修改 my_map,

那么如果修改,则把 auto iter 修改为 auto& iter, 这样通过引用,即可修改原容器里面的内容了。

egmkang 说:
2012年10月22日 22:26

auto iter : my_map
这个iter不是std::pair<int, std::string>,而是类型std::pair<const int, std::string>

five nights at fredd 说:
2018年5月22日 14:30

是用作范围迭代的变量,右边是被迭代的范围。!

ludo king 说:
2018年5月25日 15:05

That's a good style, friend! Try yourself as a creative writer.You can definitely earn something!

fanfiction 说:
2018年7月03日 15:49

wow! These look perfect!! this is truly an amazing article

run 3 说:
2018年7月05日 15:24

Thank you for your sharing. Thanks to this article I can learn more things. Expand your knowledge and abilities. Actually the article is very practical.

hotmail login 说:
2018年7月25日 14:21

Thanks for the information on the site.

Gun Cars Zombies hac 说:
2018年8月07日 16:19

Hello everyone, where we show you how you can absolutely free all premium packages for My Guns Cars Zombies unlock your Android

The synchredible rev 说:
2018年8月07日 16:20

backup and synchronization tool Synchredible is an easy-to-use tool for folder and file synchronization. It offers a wizard interface .

Ford Explorer 说:
2018年8月09日 18:15

See Explorer reviews, photos and great deals for 2019 Ford Explorer.

Return Man 3 说:
2018年8月23日 10:01

I like to get up early to go out and breathe fresh air. I feel that it is good for health and a good habit

racing car games onl 说:
2018年8月27日 22:03

Great post shared by you. thanks for posting

basketball games 说:
2018年8月30日 17:30

Remember to play it everyday to laugh more

movie zone 说:
2018年9月27日 13:49

Enjoy Movies is Russian production company, founded in 2018 by an Armenian director.

live cricket video h 说:
2018年9月30日 19:00

Live Cricket Score Streaming, Asia Cup 2018 Ind vs Pak

Honda Odyssey 2018 说:
2018年10月01日 15:31

Reviews, prices, specs and pictures on U.S. News & World Report.

red ball 说:
2018年10月05日 16:01

Thanks for perfect music!

bouncing balls 说:
2018年10月05日 16:02

thanks, tapi saya lebih suka wardah bb cream. lebih ringan dan enak dipake

wings.io 说:
2018年11月29日 16:38

If there are many sad stories, share it with the people you trust. Sharing helps people get closer together and you also relieve some of that sadness.

Avast SecureLine Vpn 说:
2018年12月04日 17:56

vast secureline vpn license File cracked version 2018 available in the market which offer better set of features and capabilities.

Adobe photoshop cs6 说:
2018年12月04日 19:04

Adobe Photoshop CS6 Serial Number is the profession standard image editor with a few interesting additional features.

Wuxia 说:
2018年12月05日 17:36

Thanks for sharing such a great information with us. Your Post is very unique and all information is reliable for new readers. Keep it up in future, thanks for sharing such a useful post

bloxorz 说:
2018年12月14日 13:03

Thank you ever so for you blog post.Thanks Again. Keep writing.

friv4school 说:
2019年1月14日 10:10

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.

run 3 说:
2019年1月31日 16:46

If there are many sad stories, share it with the people you trust. Sharing helps people get closer together and you also relieve some of that sadness.

cat mario 说:
2019年3月07日 10:01

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.

Kesari Movie Downloa 说:
2019年4月04日 17:16

The film which is based on the Battle of Saragarhi, in which 21 Sikh soldiers.

Best Parental Contro 说:
2019年5月06日 14:43

This is also a very good contribution that I love to read is not every day that I have the opportunity to see such a thing.

DIGITAL MARKETING BL 说:
2019年5月06日 14:43

There's a lot of useful information here, I am glad to visit here this are the information I am really need.. Thanks.

Dental Implant 说:
2019年5月06日 14:43

As usual, amazing post full of excellent advice, very easy to understand and follow. Please, keep the greatness coming.

Dental Implant in In 说:
2019年5月06日 14:44

I will definitely sharing this! Thanks for such a comprehensive and amazing blog! Congrats and more power.

Tarot Reading Free 说:
2019年5月06日 15:43

Thanks for all the tips mentioned in this article! I must say you are an incredible writer, I love the way that you describe the things.

How ByteFence instal 说:
2019年5月06日 20:42

you probably did an excellent task through scripting this blog and anybody will really love your work. For more info visit on

How to Prevent and R 说:
2019年5月06日 20:49

Your work is awesome. You have such good knowledge about the subject which is quite impressive in itself.

Parental Control App 说:
2019年5月07日 17:33

I am ceaselessly examining on the web for articles that can bolster me. There is obviously a lot to consider this. I think you made some incredible concentrations in Features as well. Keep working, inconceivable occupation

Easy Ways to Reduce 说:
2019年5月07日 17:34

I'm truly fulfilled to discover this site.I need to thank you just for this staggering read. I plainly esteemed every single bit of it Not simply incredible posts yet also exceptional comments.

Bit Guardian Parenta 说:
2019年5月07日 17:34

Charming stuff! I like to examine the information that you have granted to us. I have to get more updates to extend my knowledge.

What is a Digital F 说:
2019年5月07日 17:34

Positive webpage, where did u concoct the data on this posting?I have perused a couple of the articles on your site now, and I truly like your style. You're the best and please keep up the successful work.

name generator 说:
2019年5月13日 17:01

Thank you for sharing the news! This is very helpful for me.

Free Video Downloade 说:
2019年6月05日 15:29

This is a very good post .Thanks for letting us know some good tips . I
check out my article to get some facebook video downlaoding tips and tricks

Displayfusion downlo 说:
2019年6月25日 19:30

DisplayFusion is a Windows application that helps you manage multiple monitors.

How To Set Up X Box 说:
2019年8月04日 23:41

Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it.

baixar snaptube 说:
2019年8月12日 12:41

Your post is very attractive. It discusses in detail the issues.

Travel ERP Solution 说:
2019年8月22日 15:18

This article is an appealing wealth of useful informative that is interesting and well-written. Thanks a million and please keep up the effective work.

SpyHunter 5 Crack do 说:
2019年9月17日 12:25

It is very strong & most popular anti-spyware and anti-malware.

Jio rockers 说:
2019年12月17日 13:48

This is a very good post .Thanks for letting us know some good tips . I
check out my article to get some facebook video downlaoding tips and tricks

Jio rockers 说:
2019年12月17日 13:49

Positive webpage, where did u concoct the data on this posting?I have perused a couple of the articles on your site now, and I truly like your style.

Aamirtecha 说:
2020年2月07日 23:31

Appreciate your work. Thanks for sharing with us.

Features of Travel A 说:
2020年2月13日 14:50

The information you have posted is very useful. I really enjoy reading and also appreciate your work. Please keep inspiring more readers!

Tamilrockers & Isaim 说:
2020年7月30日 12:42

2.0 Tamil Movie: At the beginning of the film, Akshay Kumar hangs himself on a cell phone tower.

mkvcinemas 说:
2020年7月30日 12:43

The information you have posted is very useful.

Hdhub4u 说:
2020年7月30日 12:44

Hdhub4u is a new website created in 2020 to provide all Bollywood & Hollywood movies online for free.

basketball legends 说:
2020年10月09日 12:13

Your hands are very skillful and full of art, and you have a very creative brain to make these wonderful dishes.

kuttyrockers 说:
2020年11月26日 19:37

This is a very good post .Thanks for letting us know some good tips . I
check out my article to get some facebook video downlaoding tips and tricks

Visa Consultant in V 说:
2022年5月13日 19:15

Great share! Amazing write-up!

I liked it. I appreciate your work

NCERT Sanskrit Sampl 说:
2022年9月29日 00:57

Generally the Sanskrit sample papers are prepared by the subject experts of Leading Educational Institutes for all 11th standard students with all important questions. In fact, NCERT Sanskrit Sample Paper Class 11 the questions that are given in Sanskrit model papers are taken from the 11th Standard Sanskrit studying syllabus which is mentioned in NCERT Sanskrit sample papers 2023 class 11.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter