Erasing items from an STL list

前端 未结 6 1841
北恋
北恋 2021-02-02 13:21

I want to make a function which moves items from one STL list to another if they match a certain condition.

This code is not the way to do it. The iterator will most lik

6条回答
  •  离开以前
    2021-02-02 13:31

    Another attempt:

    for(std::list::iterator it = myList.begin(); it != myList.end; ) {
        std::list::iterator eraseiter = it;
        ++it;
        if(myCondition(*eraseiter)) {
            myOtherList.push_back(*eraseiter);
            myList.erase(eraseiter);
        }
    }
    

提交回复
热议问题