简体   繁体   English

我可以让 std::list 按顺序插入新元素吗? 还是必须使用 std::sort?

[英]can I make std::list insert new elements with order ? or got to use std::sort?

If I want to use std::list and that the new elements inserted to the list will be inserted to the right position in relation to a compare function - can I do it ?如果我想使用std::list并且插入到std::list中的新元素将插入到与比较函数相关的正确位置 - 我可以这样做吗? or I have to use std::sort after each insertion?或者我必须在每次插入后使用 std::sort ?

You can use:您可以使用:

  • std::set if your elements a immutable std::set 如果你的元素是不可变的
  • std::map if your elements have immutable keys, but should have mutable values std::map 如果您的元素具有不可变的键,但应该具有可变的值
  • std::list and looking up the insertion position std::list 并查找插入位置

std::list with std::lower_bound: std::list 与 std::lower_bound:

#include <algorithm>
#include <list>
#include <iostream>

int main()
{
    std::list<int> list;
    int values[] = { 7, 2, 5,3, 1, 6, 4};
    for(auto i : values)
        list.insert(std::lower_bound(list.begin(), list.end(), i), i);
    for(auto i : list)
        std::cout << i;
    std::cout << '\n';
}

Alternatively you may populate an entire std::vector and sort it afterwards (Note: std::sort can not operate on std::list::iterators, they do not provide random access):或者,您可以填充整个 std::vector 并在之后对其进行排序(注意:std::sort 不能对 std::list::iterator 进行操作,它们不提供随机访问):

#include <algorithm>
#include <vector>
#include <iostream>

int main()
{
    std::vector<int> vector = { 7, 2, 5,3, 1, 6, 4};
    std::sort(vector.begin(), vector.end());
    for(auto i : vector)
        std::cout << i;
    std::cout << '\n';
}

Note: The performance of a list with manual lookup of the insertion position is the worst O(N²).注意:手动查找插入位置的列表的性能是最差的 O(N²)。

Yes you can.是的你可以。 Try something like following, just change compare function and type if needed.尝试以下操作,只需更改比较功能并根据需要输入即可。

#include <list>

inline
int compare(int& a, int&b) {
    return a - b;
}

template<typename T>
void insert_in_order(std::list<T>& my_list, T element, int (*compare)(T& a, T&b)) {
    auto begin = my_list.begin();
    auto end = my_list.end();
    while ( (begin != end) &&
        ( compare(*begin,element) < 0 )      ) {
        ++begin;
    }
    my_list.insert(begin, element);
}

int main() {
    std::list<int> my_list = { 5,3,2,1 };
    my_list.sort();                              //list == { 1,2,3,5}
    insert_in_order<int>(my_list, 4, &compare);  //list == {1,2,3,4,5}
}

You have three options:您有三个选择:

  1. Sort after every insertion每次插入后排序
  2. Find the right index and insert at that index找到正确的索引并在该索引处插入
  3. Use an std::set (recommended)使用std::set (推荐)

Example for third option:第三个选项的示例:

#include <iostream>
#include <set>

int main ()
{
  int myints[] = {75,23,65,42,13};
  std::set<int> myset (myints,myints+5);

  std::cout << "myset contains:";
  for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}

Output:输出:

myset contains: 13 23 42 65 75 myset 包含:13 23 42 65 75

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM