从字符串设置升压来,dynamic_bitset [英] Setting boost dynamic_bitset from a string

查看:211
本文介绍了从字符串设置升压来,dynamic_bitset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动态的bitset

我有,我需要填充使用案例

 的boost ::来,dynamic_bitset< unsigned char型>从一个std ::字符串缓冲区。

您能否提供至于怎样去这。所以,我需要拿出一个功能

 无效populateBitSet(标准::字符串&放大器;缓冲器,
            提高::来,dynamic_bitset< unsigned char型> &安培;位图){     //从字符串缓冲填充位图
}


解决方案

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

 字符串缓冲区=0101001111011

您想初始化它像这样(原来有一个<一个href=\"http://www.boost.org/doc/libs/1%5F38%5F0/libs/dynamic%5Fbitset/dynamic%5Fbitset.html#cons3\">constructor处理这种情况下):

 无效populateBitSet(标准::字符串&放大器;缓冲区,提振::来,dynamic_bitset&LT; unsigned char型&GT;&安培;位图)
{
   位=提振::来,dynamic_bitset&LT; unsigned char型&GT; (缓冲);
}

如果你想要的原始数据,使用iterator构造

 无效populateBitSet(标准::字符串&放大器;缓冲区,提振::来,dynamic_bitset&LT; unsigned char型&GT;&安培;位图)
{
   位=提振::来,dynamic_bitset&LT; unsigned char型&GT; (buffer.begin(),buffer.end());
}

这些都结束了两次分配所需的内存,所以你可能是一个堆栈分配和交换更好。或者你只是可以等到的C ++ 0x,让移动语义做他们的事。

  //不必要的C ++ 0x中
无效populateBitSet(标准::字符串&放大器;缓冲区,提振::来,dynamic_bitset&LT; unsigned char型&GT;&安培;位图)
{
   提高::来,dynamic_bitset&LT; unsigned char型&GT; localBitmap(buffer.begin(),buffer.end());
   bitMap.swap(localBitmap);
}

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

从另一个角度看看写的第一个版本:

 的typedef的boost ::来,dynamic_bitset&LT; unsigned char型&GT;位; //只是缩短的例子。
无效populateBitSet(标准::字符串&放大器;缓冲区,比特和放大器;位图)
{
   常量位和放大器;临时=比特(缓冲); // 1.初始化暂时的
   位=温度; // 2.在数据复制从临时位图
}

如果你把这两行在一起,如在第一个例子,你仍然可以获得一个临时的堆栈上构建的,其次是分配。在1升压需要为整组比特分配足够的内存。 2,升压需要再次分配足够的内存来认为,同一组位,然后复制值了。这有可能是位图已经有足够的内存,因此它可能并不总是需要重新分配,但它也有可能,这将释放它的后盾内存反正从头重新分配。

这符合STL模具大部分容器也有,你可以在指定的地方,当你打算扔掉交换的一侧使用交换功能。这些通常是O(1)和非投掷,因为他们往往​​只是涉及将一些指点。看到这个 GotW 的另一个原因为什么这些都是有用的。

C ++ 0x中,你就可以使用分配,并仍然得到交换的优点。既然你可以在R值(如临时)超载,容器知道,当你分配一个临时的,它知道它能够蚕食温度,基本上做一个交换。在Visual Studio团队博客已覆盖右值和移动语义<一个href=\"http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx\">quite还有这里。

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
}

解决方案

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.

这篇关于从字符串设置升压来,dynamic_bitset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆