bitset::operator []是C++ STL中的內置函數,用於將值分配給位集的任何索引。
用法:
bitset_name[index] = value
參數:參數索引指定要分配值的位置。
返回值:該函數將值返回到索引處的位。
以下示例程序旨在說明bitset::operator []函數。
程序1:
// CPP program to illustrate the
// bitset::operator[]
#include <bits/stdc++.h>
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;
}
輸出:
The bitset after assigning values is:100100
程序2:
// CPP program to illustrate the
// bitset::operator[]
// assign value from a index
#include <bits/stdc++.h>
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:
// CPP program to illustrate the
// bitset::operator[]
// prints the value of bit at index
#include <bits/stdc++.h>
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++ bitset any()用法及代碼示例
- C++ bitset none()用法及代碼示例
- C++ bitset set()用法及代碼示例
- C++ bitset test()用法及代碼示例
- C++ bitset size()用法及代碼示例
- C++ bitset count()用法及代碼示例
- C++ bitset::flip()用法及代碼示例
- C++ bitset all()用法及代碼示例
- C++ bitset reset()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 bitset operator[] in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。