使用以位集作为键的映射时出现问题

2024-03-07

我正在尝试创建一个map在 C++ 中bitset作为钥匙。但是编译器会生成以下错误消息

In file included from /usr/include/c++/4.6/string:50:0,
                 from /usr/include/c++/4.6/bits/locale_classes.h:42,
                 from /usr/include/c++/4.6/bits/ios_base.h:43,
                 from /usr/include/c++/4.6/ios:43,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from test2.cpp:1:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::bitset<8u>]’:
/usr/include/c++/4.6/bits/stl_map.h:452:2:   instantiated from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::bitset<8u>, _Tp = int, _Compare = std::less<std::bitset<8u> >, _Alloc = std::allocator<std::pair<const std::bitset<8u>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::bitset<8u>]’
test2.cpp:22:30:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: no match for ‘operator<’ in ‘__x < __y’
/usr/include/c++/4.6/bits/stl_function.h:236:22: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/basic_string.h:2510:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2522:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2534:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:856:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_set.h:713:5: note: template<class _Key, class _Compare, class _Alloc> bool std::operator<(const std::set<_Key, _Compare, _Alloc>&, const std::set<_Key, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multiset.h:696:5: note: template<class _Key, class _Compare, class _Alloc> bool std::operator<(const std::multiset<_Key, _Compare, _Alloc>&, const std::multiset<_Key, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:894:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:812:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)

下面给出程序代码 我正在尝试使用 bitset 作为 C++ 中映射的键。但是,每次运行下面的代码时都会遇到错误。

#include <iostream>
#include <algorithm>
#include <string>
#include <bitset>
#include <set>
#include <utility>

using namespace std;

int main(int argc, char *argv[])
{
    bitset<8> test;
    test = 9;
    cout<<"Set to 9"<<endl;
    map <bitset<8> , int> mymap;
    pair <biset<8> , int> p;
    p.first = test;
    p.second = 9;
    string teststring;
    teststring = test.to_string<char,char_traits<char>,allocator<char> >();
    cout<<teststring<<temymap[test]<<endl;
    return 0;
}

只需使用您自己的比较器类:

struct Comparer {
    bool operator() (const bitset<8> &b1, const bitset<8> &b2) const {
        return b1.to_ulong() < b2.to_ulong();
    }
};
/* ... */
map <bitset<8> , int, Comparer> mymap;

请注意,您可以扩展此解决方案以支持任意长度的位集,只要它们足够小以转换为无符号长整型即可:

template<size_t sz> struct bitset_comparer {
    bool operator() (const bitset<sz> &b1, const bitset<sz> &b2) const {
        return b1.to_ulong() < b2.to_ulong();
    }
};
map <bitset<8> , int, bitset_comparer<8> > mymap;
map <bitset<16> , int, bitset_comparer<16> > mymap16;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用以位集作为键的映射时出现问题 的相关文章

  • 在 LINQ 查询中返回不带时间的日期

    我正在编写一个查询 我想计算按日期联系我们的呼叫中心的次数 看起来很简单 但由于联系日期字段是日期时间字段 我得到了时间 因此当我按联系日期 时间 分组时 每个联系日期实例的计数为 1 所以 我想只按日期分组 而不按时间分组 下面是我用来查
  • Signalr 在生产服务器中总是陷入长轮询

    当我在服务器中托管应用程序时 它会检查服务器端事件并始终回退到长轮询 服务器托管环境为Windows Server 2012 R1和IIS 7 5 无论如何 我们是否可以解决这个问题 https cloud githubuserconten
  • 如何在我的应用程序中使用 Windows Key

    Like Windows Key E Opens a new Explorer Window And Windows Key R Displays the Run command 如何在应用程序的 KeyDown 事件中使用 Windows
  • 跨多个控件共享事件处理程序

    在我用 C 编写的 Windows 窗体应用程序中 我有一堆按钮 当用户的鼠标悬停在按钮上时 我希望按钮的边框发生变化 目前我有以下多个实例 每个按钮一个副本 private void btnStopServer MouseEnter ob
  • 将字符串从非托管代码传递到托管

    我在将字符串从非托管代码传递到托管代码时遇到问题 在我的非托管类中 非托管类 cpp 我有一个来自托管代码的函数指针 TESTCALLBACK FUNCTION testCbFunc TESTCALLBACK FUNCTION 接受一个字符
  • c# Asp.NET MVC 使用FileStreamResult下载excel文件

    我需要构建一个方法 它将接收模型 从中构建excel 构建和接收部分完成没有问题 然后使用内存流导出 让用户下载它 不将其保存在服务器上 我是 ASP NET 和 MVC 的新手 所以我找到了指南并将其构建为教程项目 public File
  • 当 Cortex-M3 出现硬故障时如何保留堆栈跟踪?

    使用以下设置 基于 Cortex M3 的 C gcc arm 交叉工具链 https launchpad net gcc arm embedded 使用 C 和 C FreeRtos 7 5 3 日食月神 Segger Jlink 与 J
  • 按字典顺序对整数数组进行排序 C++

    我想按字典顺序对一个大整数数组 例如 100 万个元素 进行排序 Example input 100 21 22 99 1 927 sorted 1 100 21 22 927 99 我用最简单的方法做到了 将所有数字转换为字符串 非常昂贵
  • .Net Core / 控制台应用程序 / 配置 / XML

    我第一次尝试使用新的 ConfigurationBuilder 和选项模式进入 Net Core 库 这里有很多很好的例子 https docs asp net en latest fundamentals configuration ht
  • 使用向量的 merge_sort 在少于 9 个输入的情况下效果很好

    不知何故 我使用向量实现了合并排序 问题是 它可以在少于 9 个输入的情况下正常工作 但在有 9 个或更多输入的情况下 它会执行一些我不明白的操作 如下所示 Input 5 4 3 2 1 6 5 4 3 2 1 9 8 7 6 5 4 3
  • Windows 窗体不会在调试模式下显示

    我最近升级到 VS 2012 我有一组在 VS 2010 中编码的 UI 测试 我试图在 VS 2012 中启动它们 我有一个 Windows 窗体 在开始时显示使用 AssemblyInitialize 属性运行测试 我使用此表单允许用户
  • 使用 LINQ 查找列表中特定类型的第一个元素

    使用 LINQ 和 C 在元素列表中查找特定类型的第一个项目的最短表示法是什么 var first yourCollection OfType
  • 是否有比 lex/flex 更好(更现代)的工具来生成 C++ 分词器?

    我最近将源文件解析添加到现有工具中 该工具从复杂的命令行参数生成输出文件 命令行参数变得如此复杂 以至于我们开始允许它们作为一个文件提供 该文件被解析为一个非常大的命令行 但语法仍然很尴尬 因此我添加了使用更合理的语法解析源文件的功能 我使
  • 我的 strlcpy 版本

    海湾合作委员会 4 4 4 c89 我的程序做了很多字符串处理 我不想使用 strncpy 因为它不会终止 我不能使用 strlcpy 因为它不可移植 只是几个问题 我怎样才能让我的函数正常运行 以确保它完全安全稳定 单元测试 这对于生产来
  • 将日期参数传递给对 MVC 操作的 ajax 调用的安全方法

    我有一个 MVC 操作 它的参数之一是DateTime如果我通过 17 07 2012 它会抛出一个异常 指出参数为空但不能有空值 但如果我通过01 07 2012它被解析为Jan 07 2012 我将日期传递给 ajax 调用DD MM
  • 如何在内存中存储分子?

    我想将分子存储在内存中 这些可以是简单的分子 Methane CH4 C H bond length 108 7 pm H H angle 109 degrees But also more complex molecules like p
  • GDK3/GTK3窗口更新的精确定时

    我有一个使用 GTK 用 C 语言编写的应用程序 尽管该语言对于这个问题可能并不重要 这个应用程序有全屏gtk window与单个gtk drawing area 对于绘图区域 我已经通过注册了一个刻度回调gtk widget add ti
  • 如何使用 ReactiveList 以便在添加新项目时更新 UI

    我正在创建一个带有列表的 Xamarin Forms 应用程序 itemSource 是一个reactiveList 但是 向列表添加新项目不会更新 UI 这样做的正确方法是什么 列表定义 listView new ListView var
  • 如何将字符串“07:35”(HH:MM) 转换为 TimeSpan

    我想知道是否有办法将 24 小时时间格式的字符串转换为 TimeSpan 现在我有一种 旧时尚风格 string stringTime 07 35 string values stringTime Split TimeSpan ts new
  • 将 viewbag 从操作控制器传递到部分视图

    我有一个带有部分视图的 mvc 视图 控制器中有一个 ActionResult 方法 它将返回 PartialView 因此 我需要将 ViewBag 数据从 ActionResult 方法传递到 Partial View 这是我的控制器

随机推荐

  • 如何将数组传递给函数,并以数组返回结果

    所以我试图学习如何通过函数传递数组 这样我就可以解决 PHP 无法返回多个值的问题 到目前为止还没有任何成果 但这是我最好的尝试 有人能指出我哪里出错了吗 function foo array array 3 array 0 array 1
  • 用于在网页中查找值的正则表达式

    我需要找到一个正则表达式 它可以从 html 文档中的表格单元格中提取值 该表格单元格的示例内容为 结果 40 分钟 我需要一个正则表达式来匹配实际数字 40 这是用java编写的 先谢谢了 我之前尝试过使用正则表达式来做到这一点 但这是一
  • jQuery中onclick方法中调用服务器端方法

    我有一种使用 C 进行后端编码的方法 现在 我想在设计部分使用 jQuery 在按钮的 onClick 事件上调用它 请帮我解决这个问题 例如 请查看下面的代码 aspx 页面 SubmitButton click function Cal
  • 将 Typescript Map 转换为 json 字符串表示形式

    我有一个Map
  • .NET 捕获一般异常

    NET 编程指南规定我们不应该捕获一般异常 我认为以下代码不是很好 因为一般异常类型捕获 private object CreateObject string classname object obj null if string IsNu
  • 如果设备空闲,Android 前台服务会变慢

    我有一个 android 前台服务 通过通知调用 在服务中 我只是每 10 秒记录一次 Tick trap 但服务的优先级是每 X 秒在 Web 视图中导航一次 所以我使用新线程并也在主线程中工作 如果我将应用程序连接到 USB 日志似乎没
  • 为什么对变量调用方法会阻止 Rust 推断变量的类型?

    此代码编译 derive Debug Default struct Example impl Example fn some method self fn reproduction gt Example let example Defaul
  • 网站图标不工作

    我尝试插入网页的图标无法正常工作 有人可以告诉我插入它所需的代码和样式吗
  • 以动态/编程方式向 SQL 添加 WHERE 子句

    如何以编程方式向 SQL 存储过程添加搜索条件 在我的应用程序 C 中 我使用存储过程 SQL Server 2008R2 ALTER PROCEDURE dbo PROC001 userID varchar 20 password var
  • Vue 过渡与 Tailwind css 在淡出时不可见

    我使用 Tailwind css 和 Vue js 创建模式 由于 Tailwind 不支持 Vue 2 我必须添加过渡 您可以在这里看到想要的效果 https tailwindui com components application u
  • VideoView的setVideoPath和setVideoURI有什么区别

    视频查看 http developer android com reference android widget VideoView html有两种不同的方式来指定要播放的视频 设置视频路径 http developer android c
  • 如何在仅引用数据的表中循环

    我正在使用功能模块RSAQ QUERY CALL 取回一张桌子 DATA gr data TYPE REF TO data CALL FUNCTION RSAQ QUERY CALL EXPORTING query ZXXXXXXXX us
  • 通过 docker 实现 RSelenium

    我的操作系统是windows 8 1 R的版本是3 3 3 我已经安装了 RSelenium 软件包 并尝试使用以下命令运行它 library RSelenium start RSelenium server startServer che
  • 如何在步骤 1 中单击“下一步”时让 JQuery-Steps 调用 ajax 服务

    我正在使用 jquery 步骤 尽管我需要在单击第一个 下一步 时通过 ajax 调用 c 服务 但这是否可以在显示步骤 2 之前调用并返回 尽管 ajax 事件在加载步骤 2 后返回 但以下代码仍然有效 非常感谢 感谢任何帮助 Jquer
  • Java 形式类型参数定义(泛型)

    我想定义一个泛型类型 其实际类型参数只能是 数字基元包装类之一 Long Integer Float Double String 我可以用这样的定义满足第一个要求 public final class MyClass
  • 以编程方式重新启动设备

    在我的 Android 应用程序中 我想在单击按钮时重新启动我的 Android 设备 但它不起作用 到目前为止我已经做到了 ImageButton restartmob ImageButton this findViewById R id
  • 传单图块在移动设备上加载不正确

    我遇到了传单地图在移动设备上加载不正确的问题 该地图的加载纬度 经度为 25 748503 80 286949 迈阿密市中心 但在移动设备上加载的纬度 经度约为 25 584223 80 028805 大西洋沿岸 将地图拖回到正确的位置似乎
  • 我可以将 terraform 输出设置为环境变量吗?

    因此 terraform 生成了一堆我感兴趣的输出 如何将这些值通过管道传递给环境变量或其他东西 以便我的脚本能够在有人运行后获取它们terraform apply Terraform 无法直接修改调用它的 shell 的环境 一般来说 程
  • 在谷歌地图中为每个国家提供不同的颜色

    有谁知道如何在谷歌地图中为每个国家提供不同的颜色 e g 在世界地图上 蓝色覆盖英国 然后红色中国 等 我想知道google是否提供API来为每个国家提供颜色 Thanks 使用谷歌地图这确实不容易 正如 oezi 所说 你需要为你想要着色
  • 使用以位集作为键的映射时出现问题

    我正在尝试创建一个map在 C 中bitset作为钥匙 但是编译器会生成以下错误消息 In file included from usr include c 4 6 string 50 0 from usr include c 4 6 bi