C++ Bitset 中的 set()函数

首页 / C++入门教程 / C++ Bitset 中的 set()函数

C++ bitset set()函数用于将所有位设置为1。如果仅传递单个参数,则它将该特定索引处的位设置为1。

set - 语法

set(int index, bool Val);
set();

set - 参数

index - 此参数指定必须设置该位的位置。该参数是可选的。

val      -  此参数指定一个布尔值,必须在索引处下注设置。该参数是可选的。

set - 返回值

它不返回任何值。

set - 例子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

set - 例子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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

持续交付36讲 -〔王潇俊〕

Go语言核心36讲 -〔郝林〕

MySQL实战45讲 -〔林晓斌〕

程序员的数学基础课 -〔黄申〕

设计模式之美 -〔王争〕

说透区块链 -〔自游〕

遗留系统现代化实战 -〔姚琪琳〕

大型Android系统重构实战 -〔黄俊彬〕

工程师个人发展指南 -〔李云〕

好记忆不如烂笔头。留下您的足迹吧 :)