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?
我想我会使用 std::insert 的 inputIterator 版本,但不清楚如何构建输入迭代器.
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) 的范围大小吗?
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何有效地将一系列连续整数插入 std::set?


基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07