简体   繁体   English

std :: vector(InputIterator是第一个,InputIterator是最后的)线性时间复杂度?

[英]Is std::vector(InputIterator first, InputIterator last) linear time complexity?

I'd like to use the C++ std::vector input iterator constructor to build an array of consecutive integers like this: 我想使用C ++ std::vector输入迭代器构造函数来构建一个连续整数数组,如下所示:

std::vector<unsigned> indexes(boost::counting_iterator<unsigned>(0U),
                              boost::counting_iterator<unsigned>(10000U));

However, I'm wondering if it will have time complexity proportional to the distance between the iterators or whether it could have an additional logarithmic component due to repeated resizing to grow the vector? 但是,我想知道它是否具有与迭代器之间的距离成比例的时间复杂度,或者是否由于重复调整大小以使向量增长而具有额外的对数分量? In other words, does the constructor look at the distance between the two iterators? 换句话说,构造函数是否会查看两个迭代器之间的距离? Since the constructor arguments are not random access iterators, I'm not sure the distance can be computed? 由于构造函数参数不是随机访问迭代器,我不确定距离是否可以计算?

If it would resize repeatedly, is there a better solution than this to avoid that: 如果它会反复调整大小,是否有更好的解决方案来避免这种情况:

std::vector<unsigned> indexes;

indexes.reserve(10000U);

for (unsigned idx = 0; idx < 10000U; ++idx) {
  indexes.push_back(idx);
}

Is std::vector(InputIterator first, InputIterator last) linear time complexity? std::vector(InputIterator first, InputIterator last)线性时间复杂度?

In a nutshell, yes. 简而言之,是的。

The standard guarantees the following for vector(InputIterator, InputIterator) in §23.3.6.2: 该标准保证了vector(InputIterator, InputIterator)的以下内容:

Complexity: Makes only N calls to the copy constructor of T (where N is the distance between first and last) and no reallocations if iterators first and last are of forward, bidirectional, or random access categories . 复杂性:仅对N的复制构造函数进行N次调用(其中N是第一个和最后一个之间的距离) ,如果迭代器的第一个和最后一个是正向,双向或随机访问类别,则不进行重新分配 It makes order N calls to the copy constructor of T and order log(N) reallocations if they are just input iterators. 它使命令N调用T的复制构造函数和命令log(N)重新分配,如果它们只是输入迭代器。

Basically, for forward, bidirectional, or random access iterators you shouldn't be expecting to see any performance gain from using reserve() as in your second example; 基本上,对于正向,双向或随机访问迭代器,您不应期望在第二个示例中使用reserve()看到任何性能提升; the constructor will automatically do this for you. 构造函数会自动为您执行此操作。

For plain input iterators, reserve() would speed things up, but not more than by a constant factor. 对于普通的输入迭代器, reserve()会加快速度,但不会超过常量因子。 The log(n) reallocations would still be done in O(n) total time, so the total time of constructing the vector will also be O(n) . log(n)重新分配仍将在O(n)总时间内完成,因此构造向量的总时间也将是O(n)

暂无
暂无

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

相关问题 forward_list:分配(_InputIterator __first,_InputIterator __last)/分配(size_type __n,const _Tp&amp; __val) - forward_list: assign(_InputIterator __first, _InputIterator __last) / assign(size_type __n, const _Tp& __val) 为什么std :: next不接受InputIterator? - Why std::next does not accept InputIterator? c ++编译错误“要求&#39;_InputIterator std :: __ find_if(_InputIterator,_InputIterator,_Predicate,std :: input_iterator_tag)” - c++ compile error “required from '_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) ” range::view::transform 产生一个 InputIterator 阻止使用 std::prev - ranges::view::transform produces an InputIterator preventing the use of std::prev 用于连续内存的InputIterator? - InputIterator for contiguous memory? C ++ std :: string InputIterator代码转换为C代码 - C++ std::string InputIterator code to C code 自定义InputIterator用于Boost图形(BGL) - Custom InputIterator for Boost graph (BGL) 使用模板时出现奇怪的错误<class inputiterator>字符串(输入迭代器开始,输入迭代器结束);</class> - Strange Error in using template<class InputIterator> string (InputIterator begin, InputIterator end); std :: vector :: erase的时间复杂度 - Time Complexity of std::vector::erase 返回对 InputIterator 内部状态的引用是否合法? - Is it legal to return reference to an InputIterator internal state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM