#include <iostream>
#include <cstring>
using namespace std;
char week_name[7][4] = {"mon","tu","wed","thu","fri","sat","sun"};
class WeekTemp{
int temp[7];
public:
int& operator[] (const char* name){
for(int i = 0; i < 7; i++)
if(strcmp(week_name[i], name) == 0)
return temp[i];
}
};
int main(){
WeekTemp beijing;
beijing["mon"] = -3;
beijing["tu"] = -1; //可见3个字符长度的字符串,只放2个字符也行
beijing["wed"] = 0;
char temp_[4] = {0};
cin >> temp_;
cout << "Monday Temperature: "
<< beijing[temp_] << endl;
return 0;
}
从 thu 到 sun 由于未赋初值,所以得到的都是随机数。