当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ bitset reset()用法及代码示例


bitset::reset()是C++ STL中的内置函数,用于重置参数中给定索引处的位。如果未传递任何参数,则所有位都将重置为零。

用法:

reset(int index) 

参数:该函数接受参数索引,该索引指示必须将位重置为零的位置。如果未传递任何参数,则位集中的所有位都将重置为零。


返回值:该函数不返回任何内容。

下面的程序演示了bitset::reset()函数。

示例1:

// CPP program to illustrate the 
// bitset::reset() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // Initialization of bitset 
    bitset<4> b1(string("1100")); 
    bitset<6> b2(string("111111")); 
  
    // Function that resets all bits 
    cout << "Before applying reset() function: "
         << b1 << endl; 
  
    b1.reset(); 
    cout << "After applying reset() function: "
         << b1 << endl; 
  
    // Function that resets all bits 
    cout << "Before applying reset() function: "
         << b2 << endl; 
  
    b2.reset(); 
    cout << "After applying reset() function: "
         << b2 << endl; 
  
    return 0; 
}
输出:
Before applying reset() function: 1100
After applying reset() function: 0000
Before applying reset() function: 111111
After applying reset() function: 000000

示例2:

// CPP program to illustrate the 
// bitset::reset() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // Initialization of bitset 
    bitset<4> b1(string("1101")); 
    bitset<6> b2(string("111111")); 
  
    // Function that resets all bits 
    cout << "Before applying reset() function: "
         << b1 << endl; 
    b1.reset(2); 
    cout << "After applying reset(2) function: "
         << b1 << endl; 
  
    // Function that resets all bits 
    cout << "Before applying reset() function: "
         << b2 << endl; 
  
    b2.reset(3); 
    b2.reset(5); 
    cout << "After applying reset(3) and reset(5) function: "
         << b2 << endl; 
  
    return 0; 
}
输出:
Before applying reset() function: 1101
After applying reset(2) function: 1001
Before applying reset() function: 111111
After applying reset(3) and reset(5) function: 010111


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 bitset reset() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。