I have a problem, using STL algorithm with QList: while executing it crashes. Debuger takes one more step into lambda, so before to crash entry is . (So, if list is empty is crashes at 1 iteration, if list has 1 element - at 2 iteration etc.).
void FindDialog::findEntries()
{
QList<StudentEntry> searchResult;
condition = [this] (const StudentEntry &entry) -> bool {
// crashes here
return entry.name.getSurname() == surnameEdt1->text() &&
entry.group.getValue() == groupEdt->text();
};
std::copy_if(model->getStudentEntryList().begin(),
model->getStudentEntryList().end(),
searchResult.begin(),
condition);
}
How can I solve the problem?
std::copy_if
increments the output iterator when copying elements. You passed it searchResult.begin()
which is equally an end()
iterator since searchResult
is an empty container. And increment(ing) an iterator passed the end()
iterator invokes Undefined Behavior.
since QList<T>
supports a push_back(T)
member function, you should use std::back_inserter
to create a std::back_insert_iterator
that will be doing push back's to searchResult
std::copy_if(model->getStudentEntryList().begin(),
model->getStudentEntryList().end(),
std::back_inserter(searchResult),
condition);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.