问题:编写函数,使其在第一次调用时返回0,然后再次调用时按顺序产生正整数(即返回其当前的调用次数)。

代码:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 int count_calls()
 5 {
 6     static int i = 0;        
 7     return i++;
 8 }
 9 int main()
10 {
11        for (int i=0;i<10;i++)
12     {
13         cout<<count_calls()<<endl;
14     }
15    return 0;
16 }

效果:

posted on 2015-01-31 20:48  Giser-阿飞  阅读(152)  评论(0编辑  收藏  举报