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

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(使用前将#替换为@)

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

随机推荐

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

    所以我试图学习如何通过函数传递数组 这样我就可以解决 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