Namespaces
Variants

cpp/utility/bitset/operator at: Difference between revisions

From cppreference.com
Radix (talk | contribs)
mNo edit summary
Space Mission (talk | contribs)
m {{p}} (for “paragraph”) gives lesser V offset.
 
(15 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{cpp/utility/bitset/title | operator[]}}
{{cpp/utility/bitset/title|operator[]}}
{{cpp/utility/bitset/navbar}}
{{cpp/utility/bitset/navbar}}
{{ddcl list begin}}
{{begin}}
{{ddcl list item | num=1 | notes={{mark until c++11}}<br>{{mark since c++11}} |
{{|num=1|=c++11|
bool operator[]( std::size_t pos ) const;
bool operator[]( std::size_t pos ) const;
constexpr bool operator[]( std::size_t pos ) const;
}}
}}
{{ddcl list item | num=2 |
{{|num=2|
reference operator[]( std::size_t pos );
reference operator[]( std::size_t pos );
}}
}}
{{ddcl list end}}
{{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 {{c|std::bitset::reference}} that allows modification of the value.
Accesses the bit at position {{|pos}}.
the value of the bit
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.
{{}}
if{{|pos }} is


===Parameters===
===Parameters===
{{param list begin}}
{{begin}}
{{param list item | pos | position of the bit to return}}
{{|pos|position of the bit to return}}
{{param list end}}
{{end}}


===Return value===
===Return value===
1) the value of the requested bit
1value of the requested bit


2) an object of type {{c|std::bitset::reference}}, which allows writing to the requested bit.
2object of type {{|std::bitset::reference}}, which allows writing to the requested bit.


===Exceptions===
===Exceptions===
None


===Example===
===Example===
{{example
{{example
| code=
|code=
#include <iostream>
#include <iostream>
#include <bitset>


int main()
int main()
{
{
     std::bitset<8> b1(42);
     std::bitset<8> b142
     for (std::size_t i = 0; i < b1.size(); ++i) {
     for (std::size_t i = 0; i < b1.size(); ++i)
         std::cout << "b1[" << i << "]: " << b1[i] << '\n';
         std::cout << "b1[" << i << "]: " << b1[i] << '\n';
    }
     b1[0] = true; // modifies the first bit through bitset::
     b1[0] = true; // modifies the first bit through bitset::refence
     std::cout << "After setting bit 0, the bitset holds " << b1 << '\n';
     std::cout << "After setting bit 0, holds " << b1 << '\n';
}
}
| output=
|output=
b1[0]: 0
b1[0]: 0
b1[1]: 1
b1[1]: 1
Line 52: Line 56:
b1[6]: 0
b1[6]: 0
b1[7]: 0
b1[7]: 0
After setting bit 0, the bitset holds 00101011
After setting bit 0, holds 00101011
}}
}}


===See also===
===See also===
{{dcl list begin}}
{{begin}}
{{dcl list template | cpp/utility/bitset/dcl list test}}
{{|cpp/utility/bitset/test}}
{{dcl list end}}
{{end}}


[[de:cpp/utility/bitset/operator at]]
deesfritjaptruzh
[[es:cpp/utility/bitset/operator at]]
[[fr:cpp/utility/bitset/operator at]]
[[it:cpp/utility/bitset/operator at]]
[[ja:cpp/utility/bitset/operator at]]
[[pt:cpp/utility/bitset/operator at]]
[[ru:cpp/utility/bitset/operator at]]
[[zh:cpp/utility/bitset/operator at]]

Latest revision as of 04:54, 28 March 2025