我无法通过从父矢量点到达相关的矢量来获取矢量的大小。我已经验证调用函数myfunc 1 2 3 4 5
会创建五个Person对象并将它们放在left
向量中。但是当我尝试通过从bridge
到left
的点走时返回相同的大小时,我得到0作为大小。
我做错了什么?
int main(int argc, char* argv[]) {
Person* p_ptr;
int id_source = 0;
vector<Person> left;
vector<Person> right;
bridge.push_back(left);
bridge.push_back(right);
cout << "bridge.size() = " << bridge.size() << endl;
for (int i = 1; i < argc; i++) {
id_source++;
cout << "Creating Person with crossing speed of " << argv[i] << " and id of " << id_source << endl;
p_ptr = new Person(atoi(argv[i]), id_source);
left.push_back(*p_ptr);
}
/*SIZE TESTING*/
cout << "Left side of bridge has " << left.size() << " people on it " << endl;
cout << "bridge.at(0).size() = " << bridge.at(0).size() << endl;
cout << "bridge.at(1).size() = " << bridge.at(1).size() << endl;
int slowest_id = get_slowest(0);
for (int i = 0; i < left.size(); i++) {
if (slowest_id == left.at(i).get_id()) {
p_ptr = &left.at(i);
}
}
cout << "The slowest person has id of " << slowest_id << " and speed of " << p_ptr->get_crossing_time() << endl;
}
}
答案 0 :(得分:3)
left
和bridge[0]
是两个不同的列表。当您致电bridge.push_back(left)
时,您会复制当前的left
列表(空白)。稍后添加的元素将不在bridge
版本中。