简体   繁体   English

如何通过将第一个插入保留在其 position 中来对插入列表中的元素的 rest 进行排序?

[英]How do I sort rest of the elements that I insert in a list by keeping the first insertion remain in its position?

Suppose I'm trying to insert random stuff on a list in CPP using include 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);

how, and where do I call the newList.sort() to make all of them sorted except the first insertion, ie James?如何以及在哪里调用 newList.sort() 以使除第一个插入(即詹姆斯)之外的所有这些都排序?

Here's a potential solution, by first removing the first entry, then sorting.这是一个潜在的解决方案,首先删除第一个条目,然后进行排序。

auto front = newList.front();
newList.pop_front();
newList.sort();
newList.push_front(front);

This will make the entire list sorted, but whatever was at the beginning ( James in your example) will be re-added to the front of the list.这将使整个列表排序,但开头的任何内容(在您的示例中为James )都将重新添加到列表的前面。

A simple solution to keep the position of the first element:保留第一个元素的 position 的简单解决方案:

  • Store iterator to first element将迭代器存储到第一个元素
  • Sort the list对列表进行排序
  • Splice the element from the stored iterator back to beginning将存储的迭代器中的元素拼接回开头

If the "first insrertion" is not the first element - elements can be inserted in arbitrary positions after all - then a more complex logic is needed.如果“第一个插入”不是第一个元素 - 毕竟元素可以插入到任意位置 - 那么需要更复杂的逻辑。

Unfotunately, there appears to not exist a general sorting algorithm for sublists in the standard library.不幸的是,标准库中似乎不存在用于子列表的通用排序算法。

Use a custom comparator, that places James before all others:使用自定义比较器,将James置于所有其他人之前:

bool compare(const std::string& a, const std::string& b) {
    if (a == b) return false;
    else if (a == James) return true;
    else if (b == James) return false;
    else return a < b;
}

You need to take care of the a==b case to have a strict weak ordering ( James < James must return false ).您需要注意a==b的情况以进行严格的弱排序( James < James必须返回false )。 Then James comes before all others, all other come after James and otherwise you simply compare the two elements.然后James在所有其他人之前,所有其他人都在James之后,否则您只需比较这两个元素。

Note that if there is more than one James in the list they will both placed in front by this comparsion, so it isn't exactly doing what you asked for.请注意,如果列表中有多个James ,则通过此比较,它们都将排在前面,因此它并没有完全按照您的要求进行。

 auto first = newList.front();
 newList.sort();
 cout<<first<<" ";
 for(auto it :newList)
    cout<<it<<" ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在位置i处插入元素,并基于i个第一个元素返回信息 - Insert element at position i and return info based on i first elements 如何根据位置删除列表中的特定项目 - How do I remove a specific item in a list based on its position 如何优化插入类型字符串中的内存使用? - How do I optimize memory usage in my insertion sort of strings? 如何将队列元素插入向量? - How do I insert queue elements into a vector? 在链表尾部插入元素的插入函数 - insertion function to insert elements in a linked list at tail 我应该如何使我的分配器可重新绑定? 我可以在保留其字段私有性的同时做到这一点吗? - How am I supposed to make my allocator rebindable? Can I do it while keeping its fields private? 如何排序列表并获取前K个元素? (STL) - how would I sort a list and get the top K elements? (STL) 如何使用区分大小写的元素对std :: list进行排序? - How can I sort a std::list with case sensitive elements? 如何修复元组中插入排序的第二个值以与第一个值一起使用 - How do I fix the second value of insertion sorting in tuple to work as well with the first value 我可以让 std::list 按顺序插入新元素吗? 还是必须使用 std::sort? - can I make std::list insert new elements with order ? or got to use std::sort?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM