C++ bitset set() 函数
C++ bitset set() 函数用于将所有位都设置为1。如果只传递一个参数,它将在指定索引处将位设置为1。
语法
set(int index, bool Val);
set();
参数
index :该参数指定要设置位的位置。该参数是可选的。
val :该参数指定要在索引位置设置的布尔值。该参数是可选的。
返回值
不返回任何值。
示例1
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<4> b(string("1001"));
cout<< "before applying set method : " << b <<'\n';
cout<< "after applying reset method : " <<b.set() <<'\n';
return 0;
}
输出:
before applying set method : 1001
after applying reset method : 1111
示例2
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
// Initialization of bitset
bitset<4> b1(string("1100"));
bitset<6> b2(string("100100"));
// Function that resets all bits
cout<< "Before applying set() function: "<< b1 <<endl;
// single parameter is passed
b1.set(1);
cout<< "After applying set(1) function: "
<< b1 <<endl;
// Function that resets all bits
cout<< "Before applying set() function: "
<< b2 <<endl;
// both parameters is passed
b2.set(2, 0);
b2.set(4, 1);
cout<< "After applying set(2, 0) and"
<<" set(4, 1) function: " << b2 <<endl;
return 0;
}
输出:
Before applying set() function: 1100
After applying set(1) function: 1110
Before applying set() function: 100100
After applying set(2, 0) and set(4, 1) function: 110000