📜  C++ STL中的位集运算符[]

📅  最后修改于: 2021-05-30 03:18:52             🧑  作者: Mango

bitset ::运算符[]是C++ STL中的内置函数,用于将值分配给位集的任何索引。

句法:

bitset_operator[index] = value

参数:参数索引指定要分配值的位置

返回值:函数将值返回到索引处的位。

下面的程序说明了bitset :: 运算符[]函数。

程序1:

C++
// C++ program to illustrate the
// bitset::operator[]
#include 
using namespace std;
 
int main()
{
    // Initialization of bitset
    // by zeros of size 6
    bitset<6> b;
 
    // initialises second index as 1
    b[2] = 1;
 
    // initialises fifth index as 1
    b[5] = 1;
 
    // prints the bitset
    cout << "The bitset after assigning values is: " << b;
    return 0;
}


C++
// C++ program to illustrate the
// bitset::operator[]
// assign value from a index
#include 
using namespace std;
 
int main()
{
    // Initialization of bitset
    // by zeros of size 6
    bitset<6> b;
 
    // initialises second index as 1
    b[2] = 1;
 
    // initialises fifth index as the same as b[2]
    b[3] = b[2];
 
    b[0] = b[5];
     
    // prints the bitset
    cout << "The bitset after assigning values is: " << b;
    return 0;
}


C++
// C++ program to illustrate the
// bitset::operator[]
// prints the value of bit at index
#include 
using namespace std;
 
int main()
{
    // Initialization of bitset
    // by zeros of size 6
    bitset<6> b;
 
    // initialises second index as 1
    b[2] = 1;
 
    // initialises fifth index as the same as b[2]
    b[3] = b[2];
 
    b[0] = b[5];
     
     
    cout << "The bit value at index 3 is " << b[3];
    return 0;
}


输出:
The bitset after assigning values is: 100100






程式2:

C++

// C++ program to illustrate the
// bitset::operator[]
// assign value from a index
#include 
using namespace std;
 
int main()
{
    // Initialization of bitset
    // by zeros of size 6
    bitset<6> b;
 
    // initialises second index as 1
    b[2] = 1;
 
    // initialises fifth index as the same as b[2]
    b[3] = b[2];
 
    b[0] = b[5];
     
    // prints the bitset
    cout << "The bitset after assigning values is: " << b;
    return 0;
}
输出:
The bitset after assigning values is: 001100






程序3:

C++

// C++ program to illustrate the
// bitset::operator[]
// prints the value of bit at index
#include 
using namespace std;
 
int main()
{
    // Initialization of bitset
    // by zeros of size 6
    bitset<6> b;
 
    // initialises second index as 1
    b[2] = 1;
 
    // initialises fifth index as the same as b[2]
    b[3] = b[2];
 
    b[0] = b[5];
     
     
    cout << "The bit value at index 3 is " << b[3];
    return 0;
}
输出:
The bit value at index 3 is 1






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