如何有效地将一系列连续的整数插入到std :: set中? [英] How to efficiently insert a range of consecutive integers into a std::set?
本文介绍了如何有效地将一系列连续的整数插入到std :: set中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++中,我有一个std :: set,我想插入一系列连续的整数。我怎么能有效地做到这一点,希望在O(n)时间,其中n是范围的长度?
In C++, I have a std::set that I would like to insert a range of consecutive integers. How can I do this efficiently, hopefully in O(n) time where n is the length of the range?
我在想我会使用inputIterator版本std :: insert,但是我不清楚如何构建输入迭代器。
I'm thinking I'd use the inputIterator version of std::insert, but am unclear on how to build the input iterator.
std::set<int> mySet;
// Insert [34 - 75):
mySet.insert(inputIteratorTo34, inputIteratorTo75);
如何创建输入迭代器,这将是范围大小的O(n)? / p>
How can I create the input iterator and will this be O(n) on the range size?
推荐答案
根据aksham提供的提示,我看到答案是:
Taking the hint provided by aksham, I see the answer is:
#include <boost/iterator/counting_iterator.hpp>
std::set<int> mySet;
// Insert [34 - 75):
mySet.insert(boost::counting_iterator<int>(34),
boost::counting_iterator<int>(75));
这篇关于如何有效地将一系列连续的整数插入到std :: set中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文