如何将 QList 与 std::generate 一起使用?

How to use QList with std::generate?

我 运行 一个测试来比较 QVectorQList insertions/sorting 性能与此代码:

#include <algorithm>
#include <iostream>

#include <boost/timer/timer.hpp>

#include <QVector>
#include <QList>

int  main()
{
  constexpr auto  size = 10000000ul;

  srand(time(nullptr));

  std::cout << std::endl;
  std::cout << "-------------------------------------------------------------------------" << std::endl;
  std::cout << std::endl;

  {
    std::cout << "First test with QVector<int>" << std::endl;
    boost::timer::auto_cpu_timer  _g;
    QVector<int>                  vector(size);

    {
      std::cout << "\t Generating:" << std::endl << "\t\t";
      boost::timer::auto_cpu_timer  _;

      std::generate(std::begin(vector), std::end(vector), []
      {
        return clock() % rand();
      });
    }
    std::cout << std::endl;

    {
      std::cout << "\t Sorting:" << std::endl << "\t\t";
      boost::timer::auto_cpu_timer  _;

      std::sort(std::begin(vector), std::end(vector));
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
  std::cout << "-------------------------------------------------------------------------" << std::endl;
  std::cout << std::endl;

  {
    std::cout << "Second test with QList<int>" << std::endl;
    boost::timer::auto_cpu_timer  _g;
    QList<int>                    list;
    list.reserve(size);

    {
      std::cout << "\t Generating:" << std::endl << "\t\t";
      boost::timer::auto_cpu_timer  _;

      std::generate(std::begin(list), std::end(list), []
      {
        return clock() % rand();
      });
    }
    std::cout << std::endl;

    {
      std::cout << "\t Sorting:" << std::endl << "\t\t";
      boost::timer::auto_cpu_timer  _;

      std::sort(std::begin(list), std::end(list));
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
  std::cout << "-------------------------------------------------------------------------" << std::endl;
  std::cout << std::endl;
}

一个可能的输出:

-------------------------------------------------------------------------

First test with QVector<int>
     Generating:
         5.544336s wall, 3.240000s user + 2.250000s system = 5.490000s CPU (99.0%)

     Sorting:
         2.593883s wall, 2.550000s user + 0.010000s system = 2.560000s CPU (98.7%)

 8.157371s wall, 5.790000s user + 2.280000s system = 8.070000s CPU (98.9%)

-------------------------------------------------------------------------

Second test with QList<int>
     Generating:
         0.000001s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)

     Sorting:
         0.000001s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)

 0.000060s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)

-------------------------------------------------------------------------

我用QList<int>::reserve函数预留内存,用std::generate生成运行dom号插入到QList变量中。但它什么都不做,因为 QList 总是空的。

我该如何解决?

问题是 reserve 只分配内存,但不设置大小。

您可以 reserve 任意数量,但大小仍为零。

没有 QList 函数来实际设置大小。

但是您也许可以使用例如std::back_inserter iterator adapter function to append elements to the list. Combine this with the reserve and the list shouldn't have to allocate new memory. Though you can't use it with std::generate, use std::generate_n 改为:

std::generate_n(std::back_inserter(list), size, []
{
    return clock() % rand();
});