📜  C++ STL中的bitset size()

📅  最后修改于: 2021-05-30 04:14:48             🧑  作者: Mango

bitset :: size()是C++中的内置STL,它返回总位数。

句法:

bitset_name.size()

参数:该函数接受任何参数。

返回值:该函数返回一个整数值,该整数值表示位数。最终,它返回初始化位集时已给出的大小。

下面的程序说明了bitset :: size()函数。

程序1:

C++
// C++ program to illustrate the
// bitset::size() function
// when input is a string
 
#include 
using namespace std;
 
int main()
{
    // initilization of the bitset string
    bitset<4> b1(string("1100"));
    bitset<6> b2(string("010010"));
 
    // prints the size i.e.,
    // the number of bits in "1100"
    cout << "The number of bits in " << b1
         << " is " << b1.size() << endl;
 
    // prints the size i.e.,
    // the number of bits in  "010010"
    cout << "The number of bits in " << b2
         << " is " << b2.size() << endl;
 
    return 0;
}


C++
// C++ program to illustrate the
// bitset::size() function
// when input is a number
 
#include 
using namespace std;
 
int main()
{
    // initilization of the bitset string
    bitset<3> b1(5);
    bitset<5> b2(17);
 
    // prints the size i.e.,
    // the number of bits in 5
    cout << "The number of bits in " << b1
         << " is " << b1.size() << endl;
 
    // prints the size i.e.,
    // the number of bits in 17
    cout << "The number of bits in "
         << b2 << " is " << b2.size() << endl;
 
    return 0;
}


输出:
The number of bits in 1100 is 4
The number of bits in 010010 is 6




程式2:

C++

// C++ program to illustrate the
// bitset::size() function
// when input is a number
 
#include 
using namespace std;
 
int main()
{
    // initilization of the bitset string
    bitset<3> b1(5);
    bitset<5> b2(17);
 
    // prints the size i.e.,
    // the number of bits in 5
    cout << "The number of bits in " << b1
         << " is " << b1.size() << endl;
 
    // prints the size i.e.,
    // the number of bits in 17
    cout << "The number of bits in "
         << b2 << " is " << b2.size() << endl;
 
    return 0;
}
输出:
The number of bits in 101 is 3
The number of bits in 10001 is 5




要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”