串联升压::来,dynamic_bitset或std :: bitset的 [英] Concatenate boost::dynamic_bitset or std::bitset

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

问题描述

什么是连接2位集的最佳方式?

what is the best way to concatenate 2 bitsets?

例如我已经得到了

boost::dynamic_bitset<> test1( std::string("1111") );
boost::dynamic_bitset<> test2( std::string("00") );

他们应该被连接成一个THRID比特集TEST3然后持有

they should be concatenated into a thrid Bitset test3 which then holds

111100 

解决方案应该使用boost ::来,dynamic_bitset。如果解决方案使用的std :: bitset的工作,这将是很好了。并置位时,应该有一个专注于性能。

Solutions should use boost::dynamic_bitset. If the solution works with std::bitset, it would be nice too. There should be a focus on performance when concatenating the bits.

更新:
我比较(从我和尼尔和信使shiftmethod stringmethod)这两种方法和stringmethod是快了很多(10倍++)。 code在这里:
http://pastebin.com/HfpfYfy8

我希望引擎收录是确定长期张贴code-上市。如果有更好的办法,请与我联系。

I hope Pastebin is ok for posting long code-listings. If there is a better way please contact me.

推荐答案

有关标准的bitset,是这样的:

For the standard bitset, something like:

#include <bitset>
#include <string>
#include <iostream>
using namespace std;

template <int N1, int N2 >
bitset <N1 + N2> concat( const bitset <N1> & b1, const bitset <N2> & b2 ) {
    string s1 = b1.to_string();
    string s2 = b2.to_string();
    return bitset <N1 + N2>( s1 + s2 );
}

int main() {
    bitset <4> a( string("1010") );
    bitset <2> b( string("11") );
    cout << concat<4,2>( a, b ) << endl;
}

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

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