基于范围的for循环(C++ 11)
#include <iostream>
using namespace std;
int main()
{
double prices[5] = {4.99, 10.99, 6.87, 7.99, 8.49};
for (double x : prices)
cout << x << endl;
cout << "---------------------" << endl;
for (double &x : prices)
{
x = x *0.80;
cout << x << endl;
}
return 0;
}
运行结果:
4.99
10.99
6.87
7.99
8.49
---------------------
3.992
8.792
5.496
6.392
6.792