从字符串设置 boostdynamic_bitset

发布于 2024-07-15 11:34:41 字数 464 浏览 8 评论 0 原文

动态位集

我有一个需要的用例 您能否

boost::dynamic_bitset<unsigned char> , from a std::string buffer.

建议如何进行此操作。 所以我需要想出一个函数

void populateBitSet (std::string &buffer, 
            boost::dynamic_bitset<unsigned char> & bitMap) {

     //populate bitMap from a string buffer
}

Dynamic bitset

I have a use case where i need to populate

boost::dynamic_bitset<unsigned char> , from a std::string buffer.

Can you suggest as to how to go about this. So I need to come up with a function

void populateBitSet (std::string &buffer, 
            boost::dynamic_bitset<unsigned char> & bitMap) {

     //populate bitMap from a string buffer
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念﹏祤嫣 2024-07-22 11:34:41

如果您有这样的二进制数据:

string buffer = "0101001111011";

您想像这样初始化它(结果有一个 处理这种情况的构造函数):

void populateBitSet (std::string &buffer, boost::dynamic_bitset<unsigned char> & bitMap) 
{        
   bitMap = boost::dynamic_bitset<unsigned char> (buffer);
}

如果您想要原始数据,请使用 迭代器构造函数

void populateBitSet (std::string &buffer, boost::dynamic_bitset<unsigned char> & bitMap) 
{        
   bitMap = boost::dynamic_bitset<unsigned char> (buffer.begin(), buffer.end());
}

这些最终会分配所需的内存两次,因此您最好使用堆栈分配和交换。 或者您可以等到 C++0x,让移动语义完成它们的工作。

// Unecessary in C++0x
void populateBitSet (std::string &buffer, boost::dynamic_bitset<unsigned char> & bitMap) 
{        
   boost::dynamic_bitset<unsigned char> localBitmap(buffer.begin(), buffer.end());
   bitMap.swap(localBitmap);
}

编辑:
为了澄清为什么第一个版本分配两倍的内存:

看一下编写第一个版本的另一种方式:

typedef boost::dynamic_bitset<unsigned char> bits; // just to shorten the examples.
void populateBitSet (std::string &buffer, bits &bitMap) 
{        
   const bits &temp = bits(buffer); // 1. initialize temporary
   bitMap = temp; // 2. Copy over data from temp to bitMap
}

如果将这两行放在一起,如第一个示例所示,您仍然会在堆栈上得到一个临时构造,然后是一项任务。 1. boost需要为整组位分配足够的内存。 在2中,boost需要再次分配足够的内存来保存同一组位,然后复制这些值。 bitMap 可能已经拥有足够的内存,因此它可能并不总是需要重新分配,但也有可能它会释放其后备内存并从头开始重新分配。

大多数适合 stl 模具的容器还具有交换功能,当您打算丢弃交换的一侧时,可以使用该功能来代替分配。 这些通常是 O(1) 并且不会抛出异常,因为它们通常只涉及交换一些指针。 请参阅此 GotW 了解这些有用的另一个原因。

在 C++0X 中,您将能够使用赋值,并且仍然可以获得交换的优势。 由于您可以重载 r 值(如临时值),因此容器知道当您分配临时值时,它知道它可以蚕食临时值并基本上进行交换。 Visual Studio 团队博客介绍了右值和移动语义 这里很好

If you have binary data like this:

string buffer = "0101001111011";

You want to initialize it like this (turns out there's a constructor that handles this case):

void populateBitSet (std::string &buffer, boost::dynamic_bitset<unsigned char> & bitMap) 
{        
   bitMap = boost::dynamic_bitset<unsigned char> (buffer);
}

If you want the raw data, use the iterator constructor:

void populateBitSet (std::string &buffer, boost::dynamic_bitset<unsigned char> & bitMap) 
{        
   bitMap = boost::dynamic_bitset<unsigned char> (buffer.begin(), buffer.end());
}

These do end up allocating the needed memory twice, so you might be better off with a stack allocation and a swap. Or you just can wait until C++0x and let the move semantics do their thing.

// Unecessary in C++0x
void populateBitSet (std::string &buffer, boost::dynamic_bitset<unsigned char> & bitMap) 
{        
   boost::dynamic_bitset<unsigned char> localBitmap(buffer.begin(), buffer.end());
   bitMap.swap(localBitmap);
}

Edit:
To clarify why the first versions allocate twice as much memory:

Take a look at another way to write the first version:

typedef boost::dynamic_bitset<unsigned char> bits; // just to shorten the examples.
void populateBitSet (std::string &buffer, bits &bitMap) 
{        
   const bits &temp = bits(buffer); // 1. initialize temporary
   bitMap = temp; // 2. Copy over data from temp to bitMap
}

If you put these two lines together, as in the first example, you still get a temporary constructed on the stack, followed by an assignment. In 1. boost needs to allocate enough memory for the entire set of bits. In 2, boost needs to allocate again enough memory to hold that same set of bit and then copy the values over. It's possible that bitMap already has enough memory, so it may not always need to reallocate, but it's also possible that it will free its backing memory and reallocate from scratch anyway.

Most containers that fit the stl mold also have a swap function that you can use in place of assignment when you intend to throw away one side of the swap. These are usually O(1) and non-throwing as they often just involve swapping some pointers. See this GotW for another reason why these are useful.

In C++0X, you'll be able to use assignment, and still get the advantages of swap. Since you can overload on r-values (like the temporary), the container know that when you assign a temporary, it knows that it can cannibalize the temp and basically do a swap. The Visual Studio Team Blog has covered rvalues and move semantics quite well here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文