执行此操作的 OOP 方法是创建一个名为 maybe 的类ElectionInfo
,其中:
这些将是它的成员字段:
vector <string> countyNameVector;
vector <int> countyNCount;
vector <int> countyFCount;
vector <int> countyOCount;
int NCount;
int FCount;
int OCount;
int NTotal;
int FTotal;
int OTotal;
这些将是它的成员函数:
void add_county_election_file(const string);
void search_county(const string);
void print_results();
这样,您根本不必将引用传递给向量,而是您可以这样做:
ElectionInfo an_elect_info;
char selection = get_menu_choice();
// some if-statements to decide which of the following to call:
an_elect_info.add_county_election_file(county_name);
an_elect_info.search_county(county_name);
an_elect_info.print_results();
但是,如果您更愿意使用当前的功能方法:
在 main 方法中声明并初始化以下内容:
vector <string> countyNameVector;
vector <int> countyNCount;
vector <int> countyFCount;
vector <int> countyOCount;
int NCount;
int FCount;
int OCount;
int NTotal;
int FTotal;
int OTotal;
注释掉的函数声明的语法应调整为如下所示:
void add_county_election_file(const string, vector<string>&, vector<int>&, vector<int&, vector<int>&);
(当然定义也要跟上)
你会像这样调用它:
add_county_election_file(countyname, countyNameVector, countyNCount, countyFCount, countyOCount);
对象自动通过引用传递。