dimanche 18 avril 2021

Insert to list while preserving order

I declared a list of points in C++, how can I insert the new point while preserving the order?

I want to keep points ordered according to their x value.

Note: Already written an operator< for point class if needed.

I have seen multiple solutions but some has bugs and some don't work or wasn't agreed on.


In my last question: C++ can't use iterators? I used the suggested solution at: can I make std::list insert new elements with order ? or got to use std::sort? and as a side note I was told it's wrong

void JobsList::addJob(Command *cmd, bool isStopped) {
    JobsList::JobEntry tmp(0,0,cmd->cmd,isStopped);
    for (auto &job : jobs) {
        jobs.insert(std::lower_bound(jobs.begin(), jobs.end(), job), tmp);
    }
}

Aucun commentaire:

Enregistrer un commentaire