如何在不指定位大小的情况下将字符串转换为bitset ?

how do i convert a string a bitset without specifying size of bits?

本文关键字:字符串 情况下 转换 bitset 定位      更新时间:2023-10-16

我知道我们可以通过以下方式将字符串转换为bitset:

std::bitset<8> my_bitset(std::string("00011100");

,但是如果字符串被用户作为输入或来自某个文件,并且字符串的大小可能会发生变化,该怎么办?

在上面的例子中,我们知道string对象的大小是8,所以我们将bitset对象的大小初始化为8。

我尝试传递字符串的大小,但得到编译错误。

这个问题可以更一般地改写为,"我如何使用运行时属性执行编译时生成的代码?"

基本答案是构建一个编译时指针数组,这些指针指向执行该工作的模板函数,然后索引到该数组。

#include <bitset>
#include <cstdint>
#include <array>
#include <utility>
// define the parameters of supported lengths
struct traits
{
  static const std::size_t lowest = 1;
  static const std::size_t highest = 64;
  static const std::size_t extent = (highest - lowest) + 1;
};
//
// use a bitset of some length to decode the string
//
template<std::size_t Bits>
unsigned long long decode_impl(std::string const& s)
{
  std::bitset<Bits> bs(s);
  return bs.to_ullong();
}
//
// build an array of pointers to the decode functions, one for each Is
//
template<std::size_t...Is>
constexpr std::array<unsigned long long (*)(std::string const&), traits::extent>
decoders(std::index_sequence<Is...>)
{
  return {{
    &decode_impl<Is + traits::lowest>...
  }};
}
//
// build all pointers
//
std::array<unsigned long long (*)(std::string const&), traits::extent>
constexpr decoders()
{
  return decoders(std::make_index_sequence<traits::extent>());
}
//
// use runtime attributes to switch into compile-time constructs
//
unsigned long long decode(std::string const& s)
{
  static constexpr auto my_decoders = decoders();
  auto sz = std::max(s.size(), traits::lowest);
  if (sz > traits::highest)
    throw std::logic_error("unsupported length");
  return my_decoders[sz - traits::lowest](s);
}
相关文章: