assert(students.size() > 1); int i=0; foreach (QString str ,students[++i]) { qDebug() << str; const QString studentID = str; for(auto& id : students){ auto studentIt = m_students.find(id); if(studentIt == m_students.end()){ qDebug() << "student is not exist"; return; } auto it = studentIt.value()->m_workMap.find(actName); if(it != studentIt.value()->m_workMap.end()){ qDebug() << "activity repeat"; return; } activity* act = new activity; act->m_name = actName; act->m_startTime = time; act->m_endTime = -1; act->m_type = type; act->m_data.push_back(data); act->m_week.push_back(week); studentIt.value()->m_workMap.insert(act->m_name, act); log(m_currentPeople, "发布了活动[" + actName + "]给[" + studentIt.value()->m_name + "]"); } }
时间: 2024-02-10 19:08:32 浏览: 78
在这段代码中,对 `i` 进行了自增操作,但是自增操作的位置不正确。如果想要从 `students[1]` 开始遍历,应该将 `i` 的初始值设为 `-1`,并将自增操作放在 `foreach` 循环内部。例如:
```c++
assert(students.size() > 1);
int i = -1;
foreach (QString str, students) {
if (++i == 0) continue; // 跳过第一个元素
qDebug() << str;
const QString studentID = str;
for (auto& id : students) {
auto studentIt = m_students.find(id);
if (studentIt == m_students.end()) {
qDebug() << "student is not exist";
return;
}
auto it = studentIt.value()->m_workMap.find(actName);
if (it != studentIt.value()->m_workMap.end()) {
qDebug() << "activity repeat";
return;
}
activity* act = new activity;
act->m_name = actName;
act->m_startTime = time;
act->m_endTime = -1;
act->m_type = type;
act->m_data.push_back(data);
act->m_week.push_back(week);
studentIt.value()->m_workMap.insert(act->m_name, act);
log(m_currentPeople, "发布了活动[" + actName + "]给[" + studentIt.value()->m_name + "]");
}
}
```
这样就可以从 `students[1]` 开始遍历了。同时,还需要注意,在 `foreach` 循环中遍历 `students` 时,不再需要使用 `++i`,因为 `foreach` 会自动遍历所有元素。
阅读全文