杭电水题--1196 Lowest Bit (bitset:to_string有关问题)

来源:互联网 发布:对锐捷网络的了解 编辑:程序博客网 时间:2024/06/01 22:15

题目地址:  http://acm.hdu.edu.cn/showproblem.php?pid=1196

题目很简单,把一个10进制数表示成二进制,然后求最后一个1的大小,即:88:二进制为:1011000,即 8


计算很简单,直接用bitset,但是遇到编译错误:no matching function for call to `std::bitset<15>::to_string()'


奇怪了,vs编译没有任何问题,上网查了一下,有这样的解释:

 /**
     *  @brief Retuns a character interpretation of the %bitset.
     *  @return  The string equivalent of the bits.
     *
     *  Note the ordering of the bits:  decreasing character positions
     *  correspond to increasing bit positions (see the main class notes for
     *  an example).
     *
     *  Also note that you must specify the string's template parameters
     *  explicitly.  Given a bitset @c bs and a string @s:
     *  @code
     *     s = bs.to_string<char,char_traits<char>,allocator<char> >();
     *  @endcode
    */

从bitsets转换成字符串,这里有一个接受一个string参数的构造函数,和bitset<>::to_string()成员函数。接受string的构造函数和to_string()成员函数都是成员摸板,因为运行库的std::basic_string类本身就是模板;平常的字符串类,std::string是basic_string<char>的一个别名。这些成员模板的通用性受C++的一些晦涩的规则的不幸影响。
  

如果b 是bitset,你不能只是写:  
std::string s = b.to_string();
    你必须改用这种形式:
std::string s = b.template to_string<char,std::char_traits<char>,std::allocator<char> >();


改掉之后就可以过了:

#include<cstdio>#include<cstring>#include<bitset>#include<cmath>using namespace std;//char data[1000];int n;//char arr[1000][1000];int main(){while(~scanf("%d",&n),n){bitset<15>bit(n);string str=bit.to_string<char,char_traits<char>,allocator<char> >();string::size_type i=str.find_last_not_of('0');if(i==string::npos)printf("0\n");elseprintf("%d\n",(int)pow(2.0,(int)(str.length()-i-1)));}return 0;}

题目没有难度,但是遇到了关于STL的一个语法问题,但是解释还不是很清楚,又查了一下,

在:http://www.cplusplus.com/reference/bitset/bitset/to_string/这个标准说明中有提到:

Notice that this function template uses the template parameters to define the return type. Therefore, they are not implicitly deduced by the compiler.

意思是说,请注意,这个函数模板使用模板参数定义返回类型。因此,他们不由编译器隐式推导。所以需要显示的标明参数类型。

不过为什么在vs中可以使用.to_string()呢。回到vs2010的源代码中可以看到:其实已经写好了多个版本的to_string()了,第三个已经有了明显的参数,所以只是我个人的猜测而已,可能是这个原因,如果不对的话还希望指正。

template<class _Elem,class _Tr,class _Alloc>basic_string<_Elem, _Tr, _Alloc>to_string(_Elem _E0 = (_Elem)'0',_Elem _E1 = (_Elem)'1') const{// convert bitset to stringbasic_string<_Elem, _Tr, _Alloc> _Str;typename basic_string<_Elem, _Tr, _Alloc>::size_type _Pos;_Str.reserve(_Bits);for (_Pos = _Bits; 0 < _Pos; )if (test(--_Pos))_Str += _E1;else_Str += _E0;return (_Str);}template<class _Elem,class _Tr>basic_string<_Elem, _Tr, allocator<_Elem> >to_string(_Elem _E0 = (_Elem)'0',_Elem _E1 = (_Elem)'1') const{// convert bitset to stringreturn (to_string<_Elem, _Tr, allocator<_Elem> >(_E0, _E1));}template<class _Elem>basic_string<_Elem, char_traits<_Elem>, allocator<_Elem> >to_string(_Elem _E0 = (_Elem)'0',_Elem _E1 = (_Elem)'1') const{// convert bitset to stringreturn (to_string<_Elem, char_traits<_Elem>,allocator<_Elem> >(_E0, _E1));}string to_string(char _E0 = '0', char _E1 = '1') const{// convert bitset to stringreturn (to_string<char, char_traits<char>, allocator<char> >(_E0, _E1));}



原创粉丝点击