[C ++] to solve the vector subscript out of range subscript out of range

1. Check traversal statement is correct:

vector <Girl> girls;
for (unsigned int g = 0; g < girls.size(); g++) {
	girls[g]...
	}
  • : Subscript i instead of using g for Girls
    . 1) to enhance readability
    easily distinguish 2) simultaneously traverse the other vector
	for (unsigned int b = 0; b < boys.size(); b++) {
		cout << boys[b] << " 的配对女士有:" << endl;
		for (unsigned int g = 0; g < girls.size(); g++) {
			if (boys[b].satisfied(girls[g]) == true && 
				girls[g].satisfied(boys[b]) == true) {//注意调用的是否是函数以及函数是否含参以及含参顺序
				//cout << boys[b].getName() << "<-->" << girls[g].getName() << endl;如果只打印姓名,可能有重名,无法确定实际是哪位
				//因为database与Boy和Girl类不存在继承关系,所以只能通过对象调用public方法
				cout << girls[g] ;
			}
		}
		cout << endl;//一位男士的所有配对结束后空一行
	}

2. The index found does not correspond to:

Here Insert Picture Description

  • Errors of: exact replication of the like bestMatchBoy (), the leakage changes alternative peer
  • When a similar method to achieve, after copying the replaced steps:
    1) substitution rules summarized: Boy <-> Girl, G <-> B
    2) to write information to the storage field vs built git
    Here Insert Picture Description
Released five original articles · won praise 0 · Views 118

Guess you like

Origin blog.csdn.net/ainu412/article/details/104224300
Recommended