FW:for的新特性,C++11_拔剑-浆糊的传说_新浪博客


Since c++11, we can iteratively visit members in an array like the following styles:

int data[]={1,2,3,4,5,6};
for(auto& i:data)
{
     cout<<i<<endl;
}

Before that, we have to use the following styles:

int data[]={1,2,3,4,5,6};
for(int i=0; i
{
     cout<<data[I]<<endl;
}

Now, the question is can we access our self-defined data structure using the aforementioned style?

The answer is YES:
-----------------------------------------------------
int kkk1[] = { 0, 2, 4 }; // global data, only for show the grammar, actually no need in real case
 class a_1
 {
 public:
  int* begin()
  {
   return ::kkk1;
  }
  int * end()
  {
   return ::kkk1 + 3;
  
  }
 
 };
 a_1 a_1_1;
 for (auto& a_1_1_1: a_1_1)
 {
  cout << a_1_1_1 << endl;
 }
-----------------------------------------

-------------- references ---------------

https://www.cnblogs.com/LuckCoder/p/8467640.html

for的新特性,C++11

范围for循环:
1.
基于范围的for循环
for(
元素类型 元素对象:容器对象)
{
循环体
}
(1.1)
如果循环体由单条语句或者单个结构块组成,可以省略花括号
(1.2)
用元素对象依次结合容器对象中的每一个元素,每结合一个元素,执行依次循环体,直至容器内的所有元素都被结合完为止.
(1.3)
不依赖于下标元素,通用
(1.4)
不需要访问迭代器,透明
(1.5)
不需要定义处理函数,简洁

#include "stdafx.h"

#include

#include

#include

using namespace std;

void print(int i)

{

cout << i << " ";

}

 

int _tmain(int argc, _TCHAR* argv[])

{

int ai[] {65, 66, 67, 68, 69};

size_t size = sizeof(ai) / sizeof(ai[0]);

vector<</span>int> vi(ai, ai + size);

//基于下标运算的for循环,不是所有的容器都支持下标运算,不通用

for (size_t i = 0; i < size; ++i)

cout << ai[i]<<" ";

cout << endl;

for (size_t i = 0; i < vi.size(); ++i)

cout << vi[i]<<" ";

cout << endl;

//基于迭代器的for循环,需要指明容器两端,且对迭代器做自增,必须了解迭代器的运算规则,不够透明

for (int *it = ai; it != ai + size; ++it)

cout << *it << " ";

cout << endl;

for (auto it = vi.begin(); it != vi.end(); ++it)

cout << *it << " ";

cout << endl;

//基于泛型函数的for循环,需要提供针对元素的处理函数,对于一般性遍历而言比较繁琐

for_each(ai, ai + size, print);

cout << endl;

for_each(vi.begin(),vi.end(), print);

cout << endl;

for (auto a : ai)

cout << a << " ";

cout << endl;

for (auto a : vi)

cout << a << " ";

cout << endl;

for (auto &a : ai)

++a;

for (auto &a : vi)

--a;

for (auto const&a : ai)

cout << a << " ";

cout << endl;

for (auto const&a : vi)

cout << a << " ";

cout << endl;

for (char a : ai)

cout << a << " ";

cout << endl;

for (char a : vi)

cout << a << " ";

cout << endl;

getchar();

return 0;

}

2.范围循环的注意事项
(2.1)
mapmultimap容器使用范围循环,每次拿到的元素既不是键也不是值,而是由键和值组成的pair
(2.2)
在使用基于范围的for循环时,不能违背容器本身的约束
(2.3)
基于范围的for循环,无论循环体执行多少次,冒号后面的表达式永远只执行一次
(2.4)
基于范围的for循环,其底层实现依然要借助于容器的迭代器,
因此任何可能导致迭代器失效的结构性改变,都可能引发未定义的后果

 1 #include "stdafx.h"

 2 #include

 3 #include <</span>string>

 4 #include

 5 #include

 6 using namespace std;

 7

 8 list<</span>int> getScores(void)

 9 {

10 cout << __FUNCTION__ << endl;

11 return{ 70, 75, 80, 85, 90, 95 };

12 }

13

14 int_tmain(int argc, _TCHAR* argv[])

15 {

16 //mapmultimap容器使用范围循环,每次拿到的元素既不是键也不是值,而是由键和值组成的pair

17 multimap<</span>string, int> msi;

18msi.insert(make_pair("张飞", 100));

19msi.insert(make_pair("赵云", 90));

20msi.insert(make_pair("关羽", 80));

21 for (auto c : msi)

22 cout << c.first << ":" << c.second << endl;

23 cout << endl;

24 for (auto it = msi.begin(); it != msi.end(); ++it)

25 cout << it->first << ":" << it->second << endl;

26 cout << endl;

27

28 //在使用基于范围的for循环时,不能违背容器本身的约束

29

32 for (auto c : msi)

33 cout << c.first << ":" << c.second << endl;

34 cout << endl;

35 //基于范围的for循环,无论循环体执行多少次,冒号后面的表达式永远只执行一次

36 for (auto score : getScores())

37 cout << score << " ";

38 cout << endl;

39 auto scores = getScores();

40 for (auto score : scores)

41 {

42 cout << score << " ";

43 //基于范围的for循环,其底层实现依然要借助于容器的迭代器,

44 //因此任何可能导致迭代器失效的结构性改变,都可能引发未定义的后果

45 //scores.pop_front();

46 }

47 cout << endl;

48 getchar();

49 return 0;

50 }

 

3.使自己定义的容器类型支持范围循环
一个类只要提供了分别获取起始和终止迭代器的beginend函数,就可以支持基于范围的for循环

 1 #include "stdafx.h"

 2 #include

 3 using namespace std;

 4

 5 template

 6 class Array

 7 {

 8 public:

 9 T &operator[](size_t i)

10 {

11 returnm_array[i];

12 }

13 T const &operator[](size_t i)const

14 {

15 returnconst_cast(*this)[i];

16 }

17 //获取起始迭代器

18 T *begin()

19 {

20 returnm_array;

21 }

22 T const*begin()const

23 {

24 returnconst_cast(this)->begin();

25 }

26 //获取终止迭代器

27 T *end()

28 {

29 returnm_array+S;

30 }

31 T const*end()const

32 {

33 returnconst_cast(this)->end();

34 }

35 private:

36 T m_array[S];

37 };

38

39

40 int_tmain(int argc, _TCHAR* argv[])

41 {

42 int i = 0;

43 Array<</span>int, 5> ai;

44 for (auto &a : ai)

45 a = ++i * 10;

46 auto const&cai = ai;

47 for (auto &a : cai)

48 cout << a << " ";

49 cout << endl;

50 getchar();

51 return 0;

52 }

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值