将整数集转换为范围

2023-11-23

将一组整数转换为一组范围的最惯用的方法是什么?

例如。给定集合 {0, 1, 2, 3, 4, 7, 8, 9, 11} 我想得到 { {0,4}, {7,9}, {11,11} }。

假设我们正在转换std::set<int> into std::vector<std::pair<int, int>>。 我将范围视为包含双方,因为它在我的情况下更方便,但如果需要,我也可以使用开放式范围。

我已经编写了以下函数,但我想重新发明轮子。 请告诉我 STL 或 boost 中可能有一些东西可以解决这个问题。

typedef std::pair<int, int> Range;

void setToRanges(const std::set<int>& indices, std::vector<Range>& ranges)
{
    Range r = std::make_pair(-INT_MAX, -INT_MAX);

    BOOST_FOREACH(int i, indices)
    {
           if (i != r.second + 1)
           {
            if (r.second >= 0) ranges.push_back(r);
            r.first = i;                    
           }

           r.second = i;
    }

    ranges.push_back(r);
}

现在可以使用 Boost.ICL 中的interval_set (Boost > 1.46)

#include <set>
#include <iostream>
#include <algorithm>

#include <boost/icl/discrete_interval.hpp>
#include <boost/icl/closed_interval.hpp>
#include <boost/icl/interval_set.hpp>

typedef std::set<int> Set;
typedef boost::icl::interval_set<int> IntervalSet;

void setToInterval(const Set& indices, IntervalSet& intervals)
{
    Set::const_iterator pos;
    for(pos = indices.begin(); pos != indices.end(); ++pos)
    {
        intervals.insert(boost::icl::construct<boost::icl::discrete_interval<int> >(*pos, *pos, boost::icl::interval_bounds::closed()));
    }
}

int main()
{
    std::cout << ">>Interval Container Library Rocks! <<\n";
    std::cout << "----------------------------------------------------\n";

    Set indices = {0, 1, 2, 3, 4, 7, 8, 9, 11};
    IntervalSet intervals;

    setToInterval(indices, intervals);

    std::cout << "  intervals joined:    " << intervals  << "\n";

    return 0;
}

Output:

  intervals joined:    {[0,4][7,9][11,11]}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将整数集转换为范围 的相关文章

随机推荐

  • 带有 ng-repeat 的 bootstrap-label 之间没有间距

    当我使用 angular js 中的 ng repeat 添加标签时 它们显示时没有间距 这里有一个Plunker这证明了这一点 但是如果我手动添加标签 只是复制了 html 那么它们就会显示为空白 有没有一种方法可以在标签之间添加空白而不
  • Ruby 相当于 Python 的 for / else

    我一直在寻找 Ruby 中类似 Python 的 while else 结构的东西来改进我的代码 这意味着执行循环 如果循环中的条件任何时候都不为真 则返回 else 语句中的值 在红宝石中 我可以这样做 if items empty Em
  • 获取Qt 4.8 SDK

    我已经有一段时间没有在 Windows 上使用 Qt 了 我知道现在我们应该从 Qt 项目网站下载开源版本 但页面位于http qt project org downloads具有指向 Qt 库和 Qt Creator 的安装程序的单独链接
  • Perl:将 Unicode 字符串打印到 Windows 控制台

    我在将 Unicode 字符串打印到 Windows 控制台 时遇到一个奇怪的问题 考虑这段文字 Intermediary Bye Hello world test 假设它位于名为 file txt 的文件中 当我 go type file
  • 带有字符串参数的主方法入口点给出“不包含...合适的...入口点”错误

    为什么下面的代码块会给出 不包含适合入口点的静态 Main 方法 的编译错误 namespace MyConApp class Program static void Main string args string tmpString tm
  • 动态添加到 DOM 的元素的单击事件

    在 jQuery 1 8 之前 我能够使用 live 在单击由 jquery 动态插入的按钮时触发 现在 on 和 bind 都不适用于页面加载后添加到 DOM 的元素 现在有哪些选择 parent element on click chi
  • 如何启动kubelet服务?

    我运行命令systemctl 停止 kubelet然后尝试启动它systemctl 启动 kubelet 但无法启动它 这是输出systemctl 状态 kubelet kubelet service kubelet The Kuberne
  • 不区分大小写的 str_replace

    我该如何使用str replace但在搜索字符串时不区分大小写 例如 假设我想替换ABcD with a 结果命令将是 string str replace ABcD a string 但是如果有一些像 abCD 这样的字符串 那么我必须再
  • 如何编辑外部 web.config 文件?

    我正在尝试编写一个 winform 应用程序 该应用程序能够编辑已安装的 Web 应用程序的 web config 文件 我已经通读了 ConfigurationManager 和 WebConfigurationManager 类方法 但
  • Python函数无法访问类变量

    我试图访问外部函数中的类变量 但是我收到 AttributeError 类没有属性 我的代码如下所示 class example def init self somevariable raw input Input something de
  • 从 MySQL 数据库读取 BLOB 图像

    我在从 MySQL 数据库读回 blob 时遇到一些问题 我已将其成功插入数据库 但似乎无法将其读回 我知道你们中的一些人可能会想 为什么他使用数据库来存储图像的 blob 而不仅仅是文件路径 文件名 但我希望具有灵活性 因为许多图像将存储
  • 从选择器 SwiftUI 中取消选择项目

    我使用带有选择器的表单 一切正常 我可以从选择器中选择一个元素 但我无法取消选择它 是否存在从选择器中取消选择项目的方法 谢谢你 Picker selection model countries label Text country For
  • 如何说服詹金斯共享多个作业的内部版本号?

    开发团队确实要求设置构建系统 因此每个构建都将为所有分支提供唯一的构建号 构建是由詹金斯使用每个分支的作业进行的 有一个 jenkins 插件可以为作业设置下一个版本号 但这有点无用 至少有两个原因 它将设置单个作业的内部版本号 您无法知道
  • 执行 SQL Server 脚本

    如何自动运行给定文件夹中的所有 SQL 脚本 编写 Windows 脚本 使用FOR构建循环遍历您的文件并使用SQLCMD执行每个文件的实用程序 for f in c MySQLScripts sql do sqlcmd i f
  • 在 Android 中从文件读取/写入字符串

    我想通过从 EditText 输入的文本将文件保存到内部存储 然后我希望同一个文件以字符串形式返回输入的文本并将其保存到另一个字符串以便稍后使用 这是代码 package com omm easybalancerecharge import
  • 在gradle中重命名apk

    我想从 gradle 重命名我的 apk 我在构建中有以下几行 applicationVariants all variant gt def file variant outputFile def filename file name re
  • shared_ptr会自动释放内存吗?

    我需要在这里使用shared ptr 因为我无法更改API Foo1 foo1 new Foo1 shared ptr
  • Linux,spidev:为什么它不应该直接在设备树中?

    我想定义一个具有用户模式访问权限的 SPI 设备 如中所述http linux sunxi org SPIdev 按照这些示例 我在设备树中添加了以下内容 ecspi1 other stuff mydev 0 compatible spid
  • 在Go中用小RAM读取大文件的最快方法[关闭]

    Closed 这个问题需要多问focused 目前不接受答案 我想从不同的文本或JSON or CSV文件 我应该遵循哪种方法 我有博客文章文件读取 and 使用小 RAM 读取 2 GB 文本文件文件读取的不同方法 不同的方法 分块读取文
  • 将整数集转换为范围

    将一组整数转换为一组范围的最惯用的方法是什么 例如 给定集合 0 1 2 3 4 7 8 9 11 我想得到 0 4 7 9 11 11 假设我们正在转换std set