通过双向链表向后迭代.C++

-1 c++ iterator doubly-linked-list

使用我当前的方法我得到一个运行时错误,"列出iter not derefrencable".for循环看起来像:

for (iter = the_list.end(); iter != the_list.begin(); iter--)
{
    if (assignment >= (*iter)){ // If the assignment being added is greater than assignment being pointed to in the list, add it after
                                // the assignment being pointed to
        if (!(assignment == (*iter))) // If the assignment being added is not a duplicate, add it
        {
            iter++; // Increment the iterator to add the assignment to place after the one it was compared to
            the_list.insert(iter, assignment); // Insert the assignment at the iterator's position
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*k B 6

最直接的方法是使用反向迭代器:

for (iter = the_list.rbegin(); iter != the_list.rend(); ++iter)