0

Есть словарь:

map<int, DWORD> thread_ids;
map<int, DWORD>::iterator it_thread_ids;

for (i = 0; i < threads_count; i++)
{
    int currentThreadid = 0;
    tmp_id_thread++;
    ghThreads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)marker, &thread_ids, 0, &dwThreadID);
    currentThreadid = GetThreadId(ghThreads[i]);
    thread_ids.insert(std::pair<int, DWORD>(tmp_id_thread, currentThreadid));
}

При удалении элемента:

for (it_thread_ids = thread_ids.begin(); it_thread_ids != thread_ids.end(); it_thread_ids++)
{
    if (it_thread_ids->second ==  id_thread_to_remove)
    {
        thread_ids.erase(it_thread_ids);
    }
}

выпадает: "Expression: map/set iterators incompatible". В чём проблема?

1 ответ 1

2

Итератор на удаленный элемент становится невалидным. Надо получать новый итератор на следующий элемент:

auto it_thread_ids{thread_ids.begin()};
while (thread_ids.end() != it_thread_ids)
{
    if (it_thread_ids->second == id_thread_to_remove)
    {
        it_thread_ids = thread_ids.erase(it_thread_ids);
    }
    else
    {
        ++it_thread_ids;
    }
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.