Question related to deleting vector<list<T*> object.

Asked 2 years ago, Updated 2 years ago, 144 views

vector<list<CObj*>> m_vecObj;

for (UINT i = 0; i < iSize; ++i)

{

    list<CObj*>::iterator iter = m_vecObj[i].begin();
    for (; iter != m_vecObj[i].end(); ++iter)
    {

        if (i == MISSILE)
        {
            if ((*iter)->GetPos().y < n)
            {
                ******m_vecObj[MISSILE].pop_front(); ***** < - Yawl issue
                (We used * to highlight above only.)
            }
            else
            {
                (*iter)->update();
            }
        }
        else
        {
            (*iter)->update();
        }



    }

}

This code is part of the whole code. The purpose of the code is to delete a missile fired by a player when it goes out of a certain area (so delete it if the value of y is less than n) But the error window said, "Expression: can not increase value-initialized listiterator" and "line:202 of #include's list" was written.

Please let me know if there are any additional mistakes other than where the code *hit. Additionally, I would appreciate it if you could tell me the difference between erase and remove.

Additional Description: The missile moves only in the y-direction and the speed is fixed. So I used the pop front because the first missile I fired would be deleted anyway.

The function GetPos() is a function that I implemented and tells the position value of an object.

Update is a function that is called every time and has the ability to literally update everything.

MISSILE is one of the enum classes. I'm using it for index purposes. (For easy access to indexes using enum classes)

The reason why I used the list without using double-backer is that all the lower parts should be updated while circulating anyway, and if there is an exception in the middle, I need to reallocate it, but I heard that only the node can be easily deleted in the list.

list vector api c++

2022-09-22 19:42

1 Answers

Are you saying that a runtime error called expression: cannot increment value-initialized listiter occurred?

There is no strange place, but the value of iSize is m_vecObj.Is it equal to or less than size()?

std::list::remove() removes elements that have the same value as the factor you entered. std::list:erase() removes elements from a particular location through an iterator. For example, below.

#include <iostream>
#include <list>
void print(std::list<int> const& list) {
    for (auto v : list)
        std::cout << v << " ";
    std::cout << "\n";
}
int main() {
    std::list<int> list = {1, 2, 3, 2, 4};
    print(list);

    list.erase(list.begin(); // element at first position removed
    print(list);

    list.remove(2); // All elements with value of 2 have been removed
    print(list);

    return 0;
}

The results are shown below.

1 2 3 2 4
2 3 2 4
3 4


2022-09-22 19:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.