关于std :: bitset的构造函数的几个问题?

时间:2018-09-30 11:01:20

标签: c++ string bitset

来自cppreference

#3

DescriptorProto

<?php
$images = get_post_meta(get_the_ID(), 'images', true);
if ( $images && is_single() ):
    $images = get_post_meta( $post->ID, 'images' );

    if ( $images ) {
        //you can't loop through strings, you need to convert the string to an array
        $images = explode( ",", $images );

        foreach ( $images as $attachment_id ) {
            $thumb = wp_get_attachment_image( intval($attachment_id), 'full' );
            $full_size = wp_get_attachment_url($attachment_id);
            printf( '<a href="%s">%s</a>', $full_size, $thumb );
        }
    }
    echo '<br>';
endif;
?>

#4

template< class CharT, class Traits, class Alloc >

explicit bitset( const std::basic_string<CharT,Traits,Alloc>& str,
                 typename std::basic_string<CharT,Traits,Alloc>::size_type pos = 0,
                 typename std::basic_string<CharT,Traits,Alloc>::size_type n =
                     std::basic_string<CharT,Traits,Alloc>::npos);                                                    (until C++11)

我对此有几个疑问:

  1. 对于template< class CharT, class Traits, class Alloc > explicit bitset( const std::basic_string<CharT,Traits,Alloc>& str, typename std::basic_string<CharT,Traits,Alloc>::size_type pos = 0, typename std::basic_string<CharT,Traits,Alloc>::size_type n = std::basic_string<CharT,Traits,Alloc>::npos, CharT zero = CharT('0'), CharT one = CharT('1')); (since C++11) template< class CharT > explicit bitset( const CharT* str, typename std::basic_string<CharT>::size_type n = std::basic_string<CharT>::npos, CharT zero = CharT('0'), CharT one = CharT('1')); (since C++11) ,为什么标准指定了#3而不是#4?我知道std::basic_string<...>也被称为std::string,并且其他字符类型,例如std::stringstd::basic_string<char>也可以应用于wchar_t。但是我认为没有必要,因为char16_t包含std::basic_string<...>bitset,正如其名称所示。

  2. 老实说,我对0类不是很熟悉:为什么添加1string可以做什么,而#4无法做到?

1 个答案:

答案 0 :(得分:1)

#3对于使用宽字符的人员很方便-您不能期望人们将宽“ 0”和“ 1”的字符串转换为窄字符串以使用基于字符串的构造函数只是因为您碰巧知道值始终是二进制的-否则应将此构造函数替换为1位宽的字符串变体。换句话说,它写起来更方便,那么消除这种方便的好处是什么?

#4适用于具有字符串文字或指向字符数组的指针的人,因此他们无需构造无用的临时std :: string。