如何通过将第一个插入保持在其位置来对我插入列表中的其余元素进行排序?

nxd*_*nxd 4 c++ sorting list

假设我尝试使用包含列表在 CPP 中的列表中插入随机内容,

list <string> newList;
list<string>::iterator z = newList.begin();
//James(Remains static, rest sorted)  -> Abraham-> Brian-> Chuck-> David-> Rick-> Morty
newList.insert(z,James);
newList.insert(z,David);
newList.insert(z, Rick);
newList.insert(z, Abraham);
newList.insert(z, Brian);
newList.insert(z, Morty);
newList.insert(z, Chuck);
Run Code Online (Sandbox Code Playgroud)

我如何以及在哪里调用 newList.sort() 以使除第一次插入之外的所有它们都排序,即 James?

Chr*_*sMM 6

这是一个潜在的解决方案,首先删除第一个条目,然后进行排序。

auto front = newList.front();
newList.pop_front();
newList.sort();
newList.push_front(front);
Run Code Online (Sandbox Code Playgroud)

这将使整个列表排序,但开头的内容(James在您的示例中)将重新添加到列表的前面。