程序清单5.20 nested.cpp
#include <iostream>
const int Cities = 5;
const int Years = 4;
int main()
{
using namespace std;
const char * cities[Cities] = //char cities[Cities][25]; string cities[Cities].
{
"Cribble City",
"Gribbletown",
"New Gribble",
"San Gribble",
"Gribble Vista"
};
int maxtemps[Years][Cities] =
{
{94, 98, 87, 103, 101},
{98, 99, 91, 107, 105},
{93, 91, 90, 101, 104},
{95, 100, 88, 105, 103}
};
cout << "Maximum temperatures for 2002-2005\n\n";
for (int city = 0; city < Cities; ++city)
{
cout << cities[city] << ": \t";
for (int year = 0; year < Years; ++year)
cout << maxtemps[year][city] << "\t";
cout << endl;
}
return 0;
}
“在希望字符串是可修改的情况下,string类自动调整大小的特性将使这种方法比使用二维数组更为方便。”