for_each函数用法

article/2025/7/28 5:51:22

Introduction
學習過STL的container後,想要存取每一個iterator,你一定寫過以下的程式

#include  < vector >
#include 
< iostream >

using   namespace  std;

int  main()  {
  
int ia[] = {123};
  vector
<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
  
  
for(vector<int>::const_iterator iter = ivec.begin(); iter != ivec.end(); ++iter) {
    cout 
<< *iter << endl;
  }

}


執行結果

1
2
3


當時我覺得STL什麼都好,就是以下這一串又臭又長

for (vector < int > ::const_iterator iter  =  ivec.begin(); iter  !=  ivec.end();  ++ iter)  {


若不常寫,一時還會寫不出來,其實若配合container,C++其實不應該這樣寫迴圈,正確的方式該使用for_each(),語法會變的相當簡單。

for_each()事實上是個function template,其實做如下[effective STL item 41]

template < typename InputIterator, typename Function >
Function for_each(InputIterator beg, InputIterator end, Function f) 
{
  
while(beg != end) 
    f(
*beg++);
}


由以上source可知,for_each()只能配合global function和function object。

以下我們將對procedure based、object oriented、generics三種paradigm與for_each()搭配做探討。

Procedure Based與for_each()搭配
1.不傳入參數

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_GlobalFunction.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with global function
 6Release     : 05/11/2007 1.0
 7*/

 8 #include  < iostream >
 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12
13 using   namespace  std;
14
15 void  printElem( int &  elem)  {
16  cout << elem << endl;
17}

18
19 int  main()  {
20  int ia[] = {123};
21  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
22  
23  for_each(ivec.begin(), ivec.end(), printElem);
24}


執行結果

1
2
3


23行

for_each(ivec.begin(), ivec.end(), printElem);


只需將vector::begin(),vector::end()和global function name傳給for_each()即可,再也不用for迴圈那種複雜的語法了。 

2.傳入參數
若要傳參數給global function,就不能再只傳global function name而已,必須透過ptr_fun()這個function adapter將global function轉成function object,然後再用bind2nd()將參數bind成一個function object。

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_GlobalFunctionWithParameter.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with global function with Parameter
 6Release     : 05/11/2007 1.0
 7*/

 8 #include  < iostream >
 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12 #include  < functional >
13
14 using   namespace  std;
15
16 void  printElem( int  elem,  const   char *  prefix)  {
17  cout << prefix << elem << endl;
18}

19
20 int  main()  {
21  int ia[] = {123};
22  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
23  
24  for_each(ivec.begin(), ivec.end(), bind2nd(ptr_fun(printElem), "Element:"));
25}


執行結果

Element: 1
Element:
2
Element:
3


Object Oriented與for_each()搭配
1.不傳入參數
使用function object

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_FunctionObject.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with function object
 6Release     : 05/11/2007 1.0
 7*/

 8 #include  < iostream >
 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12
13 using   namespace  std;
14
15 struct  printElem  {
16  void operator() (int elem) {
17    cout << elem << endl;
18  }
 
19}
;
20
21 int  main()  {
22  int ia[] = {123};
23  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
24  
25  for_each(ivec.begin(), ivec.end(), printElem());
26}


執行結果

1
2
3


2.傳入參數
若使用function object,也可以將參數傳給printElem(),透過constructor的技巧接收參數。

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_FunctionObjectWithParameter.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with function object with parameter
 6Release     : 05/11/2007 1.0
 7*/

 8 #include  < iostream >
 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12
13 using   namespace  std;
14
15 struct  printElem  {
16  const char* _prefix;
17
18  printElem(const char* prefix) : _prefix(prefix) {}
19  
20  void operator() (int elem) {
21    cout << _prefix << elem << endl;
22  }
 
23}
;
24
25 int  main()  {
26  int ia[] = {123};
27  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
28  
29  for_each(ivec.begin(), ivec.end(), printElem("Element:"));
30}

執行結果 

Element: 1
Element:
2
Element:
3


function object有很多種寫法,但只要是function object都可以跟for_each()合作。

3.member_function與for_each()搭配
3.1 不傳入參數
本文的重點來了,在物件導向世界裡,最常用的就是for_each()配合member function,這該怎麼寫呢?直覺會這樣子寫

for_each(_doorVec.begin(), _doorVec.end(), & Door::open);


由於global function name本身就是一個pointer,所以想藉由&Door::open傳進一個address,但這樣compile並不會過,正確解法是

for_each(_doorVec.begin(), _doorVec.end(), mem_fun_ref( & Door::open));


透過mem_fun_ref()這個function adapter將member function轉成function object。

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : GenericAlgo_for_each_MemberFunctionObject.cpp
 5Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6Description : Demo how to use for_each with member function with object
 7Release     : 05/11/2007 1.0
 8*/

 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12 #include  < functional >
13
14 using   namespace  std;
15
16 class  Door  {
17public:
18  void open() const {
19    cout << "open door horizontally" << endl;
20  }

21  
22  void close() const {
23    cout << "close door horizontally" << endl;
24  }

25}
;
26
27 class  DoorController  {
28protected:
29  vector<Door> _doorVec;
30  
31public:
32  void addDoor(Door aDoor) {
33    _doorVec.push_back(aDoor);
34  }

35  
36  void openDoor() const {
37    for_each(_doorVec.begin(), _doorVec.end(), mem_fun_ref(&Door::open));
38  }

39}
;
40
41 int  main()  {
42  DoorController dc;
43  dc.addDoor(Door());
44  dc.addDoor(Door());
45  dc.openDoor();
46}


執行結果

open door horizontally
open door horizontally


37行

for_each(_doorVec.begin(), _doorVec.end(), mem_fun_ref( & Door::open));


值得注意的是,mem_fun_ref()用在object的member function。若要搭配多型,vector必須放pointer,也就是得使用object pointer的member function,此時得使用mem_fun()將member function轉成function object。

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : GenericAlgo_for_each_MemberFunctionObjectPointer.cpp
 5Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6Description : Demo how to use for_each with member function with object pointer
 7Release     : 05/11/2007 1.0
 8*/

 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12 #include  < functional >
13
14 using   namespace  std;
15
16 class  AbstractDoor  {
17public:
18  virtual void open() const {
19    cout << "open door horizontally" << endl;
20  }

21  
22  virtual void close() const {
23    cout << "close door horizontally" << endl;
24  }

25}
;
26
27 class  HorizontalDoor :  public  AbstractDoor  {
28}
;
29
30 class  VerticalDoor :  public  AbstractDoor  {
31public:
32  void open() const {
33    cout << "open door vertically" << endl;
34  }

35  
36  void close() const {
37    cout << "close door vertically" << endl;
38  }

39}
;
40
41 class  DoorController  {
42protected:
43  vector<AbstractDoor*> _doorVec;
44  
45public:
46  void addDoor(AbstractDoor& aDoor) {
47    _doorVec.push_back(&aDoor);
48  }

49  
50  void openDoor() const {
51    for_each(_doorVec.begin(), _doorVec.end(), mem_fun(&AbstractDoor::open));
52  }

53}
;
54
55 int  main()  {
56  DoorController dc;
57  dc.addDoor(HorizontalDoor());
58  dc.addDoor(VerticalDoor());
59  dc.openDoor();
60}


執行結果

open door horizontally
open door vertically


51行

for_each(_doorVec.begin(), _doorVec.end(), mem_fun( & AbstractDoor::open));


使用了mem_fun()。

3.2傳入參數
問題又來了,若要使member function也傳入參數呢?這時得使用bind2nd將function object和參數bind在一起,變成另外一個新的function object。

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : GenericAlgo_for_each_MemberFunctionObjectPointerWithParameter.cpp
 5Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6Description : Demo how to use for_each with member function with object pointer
 7Release     : 05/11/2007 1.0
 8*/

 9 #include  < iostream >
10 #include  < vector >
11 #include  < algorithm >
12 #include  < functional >
13
14 using   namespace  std;
15
16 class  AbstractDoor  {
17public:
18  virtual void open() const {
19    cout << "open door horizontally" << endl;
20  }

21  
22  virtual void close() const {
23    cout << "close door horizontally" << endl;
24  }

25  
26  virtual void openDoorBy(const char* name) const {
27    cout << name << " ";
28    open();
29  }

30}
;
31
32 class  HorizontalDoor :  public  AbstractDoor  {
33}
;
34
35 class  VerticalDoor :  public  AbstractDoor  {
36public:
37  void open() const {
38    cout << "open door vertically" << endl;
39  }

40  
41  void close() const {
42    cout << "close door vertically" << endl;
43  }

44}
;
45
46 class  DoorController  {
47protected:
48  vector<AbstractDoor*> _doorVec;
49  
50public:
51  void addDoor(AbstractDoor& aDoor) {
52    _doorVec.push_back(&aDoor);
53  }

54  
55  void openDoor() const {
56    for_each(_doorVec.begin(), _doorVec.end(), bind2nd(mem_fun(&AbstractDoor::openDoorBy), "John"));
57  }

58}
;
59
60 int  main()  {
61  DoorController dc;
62  dc.addDoor(HorizontalDoor());
63  dc.addDoor(VerticalDoor());
64  dc.openDoor();
65}


執行結果

1 John open door horizontally
2 John open door vertically


56行

for_each(_doorVec.begin(), _doorVec.end(), bind2nd(mem_fun( & AbstractDoor::openDoorBy),  " John " ));


透過了bind2nd將參數結合後,成為一個新的function object。

Generics與for_each()搭配
1.Function Template
1.1不傳入參數
在泛型世界裡,那for_each()該怎麼配合function template呢?

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_FunctionTemplate.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with function template
 6Release     : 05/11/2007 1.0
 7*/

 8 #include  < iostream >
 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12
13 using   namespace  std;
14
15 template < typename T >
16 void  printElem(T elem)  {
17  cout << elem << endl;
18}

19
20 int  main()  {
21  int ia[] = {123};
22  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
23  
24  for_each(ivec.begin(), ivec.end(), printElem<int>);
25  //for_each(ivec.begin(), ivec.end(), (void(*)(int))printElem);
26}


執行結果

1
2
3


若使用function template,有兩種寫法
一種是

for_each(ivec.begin(), ivec.end(), printElem < int > );


由於template function需要在compile時確定型別,所以要加上<int>確定為int型別。
另外一種寫法

for_each(ivec.begin(), ivec.end(), ( void ( * )( int ))printElem);


template function並沒有確定型別,但轉成function pointer時,並須明確轉成int型別的function pointer。

1.2 傳入參數
若要如function object那樣能傳參數呢?funtion template是可以,不過有些限制,若使用nontype parameter,只能使用以下三種型別
1.int或enum
2.pointer:pointer to object,pointer to function,pointer to member。
3.reference:reference to object,reference to function。

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_FunctionTemplateWithNontypeParameter.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with function template with nontype parameter
 6Release     : 05/11/2007 1.0
 7*/

 8 #include  < iostream >
 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12
13 using   namespace  std;
14
15 template < typename T,  int  i >
16 void  printElem(T elem)  {
17  cout << i << ":"  << elem << endl;
18}

19
20 int  main()  {
21  int ia[] = {123};
22  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
23  
24  for_each(ivec.begin(), ivec.end(), printElem<int5>);
25}


執行結果

5 : 1
5 : 2
5 : 3


所以無法如function object那樣可以傳入字串或任意型別,最少在目前ISO C++標準是做不到的。

既然討論了function template,那最具威力的class template是否也能搭配for_each()?

2.Class Template
2.1 不傳入參數

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_ClassTemplate.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with class template
 6Release     : 05/11/2007 1.0
 7*/

 8 #include  < iostream >
 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12 #include  < functional >
13
14 using   namespace  std;
15
16 template < typename T >
17 class  printElem :  public  unary_function < T,  void >   {
18public:
19  void operator() (T elem) {
20    cout << elem << endl;
21  }

22}
;
23
24 int  main()  {
25  int ia[] = {123};
26  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
27  
28  for_each(ivec.begin(), ivec.end(), printElem<int>());
29}


執行結果

1
2
3


17行

class  printElem :  public  unary_function < T,  void >   {


因為printElem只接受for_each()所傳的參數,算是單參數而已,所以public繼承了unary_function<T,void>,因為for_each的定義

template  < class  InputIterator,  class  UnaryFunction >
UnaryFunction for_each(InputIterator first, InputIterator last, UnaryFunction f);


傳進去的是UnaryFunction型別,第一個type parameter T表示傳入的型別,第二個type parameter void,表示回傳的型別,最後重新定義operator()。

2.2 傳入參數
若要使class template也能傳入參數,一樣利用function object的技巧,借用constructor。

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3Filename    : GenericAlgo_for_each_ClassTemplateWithParameter.cpp
 4Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 5Description : Demo how to use for_each with class template & parameter
 6Release     : 05/11/2007 1.0
 7*/

 8 #include  < iostream >
 9 #include  < vector >
10 #include  < iostream >
11 #include  < algorithm >
12 #include  < functional >
13
14 using   namespace  std;
15
16 template < typename T, typename U >
17 class  printElem :  public  unary_function < T,  void >   {
18private:
19  U _prefix;
20  
21public:
22  printElem(U prefix) : _prefix(prefix) {}
23  
24  void operator() (T elem) {
25    cout << _prefix << elem << endl;
26  }

27}
;
28
29 int  main()  {
30  int ia[] = {123};
31  vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
32  
33  for_each(ivec.begin(), ivec.end(), printElem<intconst char*>("Element:"));
34}


執行結果

Element: 1
Element:
2
Element:
3


Conclusion
STL的for_each()事實上很好用,不過由於限制很多,所以常令很多新手卻步,本文試著將所有會遇到問題的地方都提出來討論,包括procedure based、object oriented、generics三種paradigm與for_each()的搭配都涵蓋了,希望對各位有幫助。


http://chatgpt.dhexx.cn/article/OhkgiaHi.shtml

相关文章

each函数用法

<?php/*each()只是一个函数&#xff0c;参数就是一个数组&#xff0c;返回的值也是一个数组1.返回的值也是一个数组&#xff0c;数组固定有4个元素&#xff0c;而且下标也是固定的1(值) value(值) 0(下标) key(下标)2.each()只是处理当前的元素将当前的元素(默认当前元素是…

$.each的用法

1、示例1&#xff1a; <script type"text/javascript">var arr["1","2","3"];<span style"white-space:pre"> </span>//定义arr数组$.each(arr, function() {alert(this);<span style"white-spac…

for_each用法示例

文章目录 前言示例demo 前言 由于偶然间发现for_each能使得避免使用for循环&#xff0c;大大简化了代码。这里简单记录下for_each的一个简单示例demo,方便温习。 示例demo #include <iostream> #include "vector" #include "algorithm"void myfun…

字长、字节、字、字位的区别

字长、字节、字、字位的区别&#xff1a; &#xff08;1&#xff09;概念不一样 同一时间处理二进制数位数叫字长&#xff0c;字长是直接用二进制代码指令表达的计算机语言。 字节&#xff08;Byte &#xff09;是计算机信息技术用于计量存储容量的一种计量单位&#xff0c;…

微型计算机一个汉字多少字节,一个汉字多少字节(Byte)?

2006-01-07 一个汉字有几个字节&#xff1f; 依据编码形式&#xff1a; GB&#xff0d;231280 编码为 2个字节(Byte) 包含了 20902 个汉字&#xff0c;其编码范围是 0x8140-0xfefe。 GB18030-2000(GBK2K) 在 GBK 的基础上进一步扩展了汉字&#xff0c;增加了藏、蒙等少数民族的…

计算机中1kb等于多少字节,在计算机中1kb等于多少字节

在计算机中1kb等于1024个字节。字节是计算机信息技术用于计量存储容量的一种计量单位&#xff0c;也表示一些计算机编程语言中的数据类型和语言字符。一个字节存储8位无符号数。 本文操作环境&#xff1a;windows10系统、thinkpad t480电脑。 (学习视频分享&#xff1a;编程视频…

使用Java8新特性对List进行排序

前言&#xff1a; 在项目开发中往往会遇到各种数据需要排序展示在页面上&#xff0c;常见的从数据库查使用数据库的排序&#xff0c;还有一种就是使用我们的开发语言进行排序&#xff0c;这里给大家演示使用java8的新特性进行排序&#xff0c;众所周知java8带来了函数式编程和…

java8特性之forEach篇

java8特性之forEach篇 forEach介绍使用条件迭代原理性能 forEach介绍 forEach是java8的特性之一&#xff0c;它可以大大简化代码的操作&#xff0c;比如有关HashMap的操作&#xff1a; HashMap<Integer, String> hashMap new HashMap<>(3); hashMap.put(1, &quo…

java8特性快速对list集合的筛选过滤和计算

java8特性快速对list集合的筛选过滤和计算 一、准备工作 1.创建一个Student对象 package com.shiro.test.java8特性;import java.io.Serializable;/*** 学生的实体类*/ public class Student implements Serializable {private String id;private String username;private In…

Java8特性大全(最新版)

一、序言 Java8 是一个里程碑式的版本&#xff0c;凭借如下新特性&#xff0c;让人对其赞不绝口。 Lambda 表达式给代码构建带来了全新的风格和能力&#xff1b;Steam API 丰富了集合操作&#xff0c;拓展了集合的能力&#xff1b;新日期时间 API 千呼万唤始出来&#xff1b;…

Java 8特性之Optional详解

一、Optional类 简介 Optional类是 Java 8 引入的一个很有趣的特性。它主要解决的问题是臭名昭著的空指针异常&#xff08;NullPointerException&#xff09; Optional 类是一个可以为null的容器对象。如果值存在则isPresent()方法会返回true&#xff0c;调用get()方法会返回…

Java基础之java8 新特性

Java基础之java8 新特性 一、Lambda 表达式二、Stream 初体验三、方法引用四、默认方法五、Optional 类六、Base 64七、字符串拼接八、 equals 与 instanceof九、final 一、Lambda 表达式 Lambda 表达式是在 Java8 中引入的&#xff0c;并号称是 Java8 的最大的特点。Lambda 表…

java8新特性Lambda和Stream以及函数式接口等新特性介绍

主要内容 1.Lambda 表达式 2.函数式接口 3.方法引用与构造器引用 4.Stream API 5.接口中的默认方法与静态方法 6.新时间日期API 7.其他新特性 Java 8新特性简介 速度更快速度更快代码更少&#xff08;增加了新的语法Lambda 表达式&#xff09;强大的Stream API便于并行最大化…

python 读取并显示图片的两种方法

转自&#xff1a;http://www.cnblogs.com/yinxiangnan-charles/p/5928689.html 在 python 中除了用 opencv&#xff0c;也可以用 matplotlib 和 PIL 这两个库操作图片。本人偏爱 matpoltlib&#xff0c;因为它的语法更像 matlab。 一、matplotlib 1. 显示图片 import matplotli…

Python同时显示多张图片在一个画面中(两种方法)

很多时候需要把很多图片同时显示到一个画面中&#xff0c;现在分享两个方法&#xff0c;这里我恰好拿之前写的爬取网上图片保存到本地的爬虫模型爬一些图片作为素材Python 爬虫批量爬取网页图片保存到本地。 得到素材如下所示&#xff1a; 现在让这些图片同时显示。 方法一 …

python 打开 显示图片

import matplotlib.pyplot as plt # plt 用于显示图片 from PIL import Imageplt.figure() plt.plot([1,2], [1,2])plt.rcParams[font.sans-serif] [SimHei] # 中文乱码 plt.imshow(Image.open("b.png"))plt.axis(off)plt.tight_layout() manager plt.get_current…

python 把matplotlib绘制的图片显示到html中

需求 一般网页中图片显示是给url链接过去&#xff0c;但是有的时候显示的图表是临时计算绘制的&#xff0c;并不需要保存&#xff0c;因此就需要直接显示一个图片的方法。 灵感是来自于jupyter&#xff0c;发现他是这样的&#xff1a; 估计是base64编码了。 查了一下如何把ma…

python如何将图片显示在网页上

from flask import Flask, render_template_string import base64 import cv2import osapp Flask(__name__)# 读取图像app.route(/)def index():# 读取图像文件并将其转换为Base64编码的字符串img_path 1.pngimg_data open(img_path, rb).read()img_base64 base64.b64encod…

python matplotlib 显示图像

python matplotlib 显示图像 首先需要import import os from PIL import Image import matplotlib.pyplot as plt 显示一幅彩色图片 img Image.open(os.path.join(images, 2007_000648 .jpg))plt.figure("Image") # 图像窗口名称 plt.imshow(img) plt.axis(on)…

python中plt.imshow()不显示图片

python画图函数可能是需要一些设置&#xff0c;或者一些特定的函数运行才会出现图片的。 Python中plt.imshow(image)无法显示图片解决办法 使用plt.imshow()发现不能显示图片&#xff0c;加了plt.show()也还是不能显示 先引入包pylab import pylab 然后在plt.imshow(img)后面…