cpp/utility/bitset/operator at: Difference between revisions
From cppreference.com
mNo edit summary |
m Shorten template names. Use {{lc}} where appropriate. |
||
| Line 1: | Line 1: | ||
{{cpp/utility/bitset/title | operator[]}} | {{cpp/utility/bitset/title | operator[]}} | ||
{{cpp/utility/bitset/navbar}} | {{cpp/utility/bitset/navbar}} | ||
{{ | {{begin}} | ||
{{ | {{| num=1 | notes={{mark until c++11}}<br>{{mark since c++11}} | | ||
bool operator[]( std::size_t pos ) const; | bool operator[]( std::size_t pos ) const; | ||
constexpr bool operator[]( std::size_t pos ) const; | constexpr bool operator[]( std::size_t pos ) const; | ||
}} | }} | ||
{{ | {{| num=2 | | ||
reference operator[]( std::size_t pos ); | reference operator[]( std::size_t pos ); | ||
}} | }} | ||
{{ | {{end}} | ||
Accesses the bit at position {{tt|pos}}. The first version returns the value of the bit, the second version returns an object of type {{ | Accesses the bit at position {{tt|pos}}. The first version returns the value of the bit, the second version returns an object of type {{|std::bitset::reference}} that allows modification of the value. | ||
Unlike {{rlpf|test}}, does not throw exceptions: the behavior is undefined if {{tt|pos}} is out of bounds. | Unlike {{rlpf|test}}, does not throw exceptions: the behavior is undefined if {{tt|pos}} is out of bounds. | ||
===Parameters=== | ===Parameters=== | ||
{{ | {{begin}} | ||
{{ | {{| pos | position of the bit to return}} | ||
{{ | {{end}} | ||
===Return value=== | ===Return value=== | ||
1) the value of the requested bit | 1) the value of the requested bit | ||
2) an object of type {{ | 2) an object of type {{|std::bitset::reference}}, which allows writing to the requested bit. | ||
===Exceptions=== | ===Exceptions=== | ||
| Line 56: | Line 56: | ||
===See also=== | ===See also=== | ||
{{ | {{begin}} | ||
{{ | {{| cpp/utility/bitset/dcl list test}} | ||
{{ | {{end}} | ||
[[de:cpp/utility/bitset/operator at]] | [[de:cpp/utility/bitset/operator at]] | ||
Revision as of 04:13, 1 June 2013
bool operator[]( std::size_t pos ) const;
constexpr bool operator[]( std::size_t pos ) const;
|
(1) | (until C++11) (since C++11) |
reference operator[]( std::size_t pos );
|
(2) | |
Accesses the bit at position pos. The first version returns the value of the bit, the second version returns an object of type std::bitset::reference that allows modification of the value.
Unlike test(), does not throw exceptions: the behavior is undefined if pos is out of bounds.
Parameters
| pos | - | position of the bit to return |
Return value
1) the value of the requested bit
2) an object of type std::bitset::reference, which allows writing to the requested bit.
Exceptions
None
Example
Run this code
#include <iostream>
#include <bitset>
int main()
{
std::bitset<8> b1(42);
for (std::size_t i = 0; i < b1.size(); ++i) {
std::cout << "b1[" << i << "]: " << b1[i] << '\n';
}
b1[0] = true; // modifies the first bit through bitset::refence
std::cout << "After setting bit 0, the bitset holds " << b1 << '\n';
}
Output:
b1[0]: 0
b1[1]: 1
b1[2]: 0
b1[3]: 1
b1[4]: 0
b1[5]: 1
b1[6]: 0
b1[7]: 0
After setting bit 0, the bitset holds 00101011
See also
| accesses specific bit (public member function) |