Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::bitset<N>::operator[]

      From cppreference.com
      <cpp‎ |utility‎ |bitset
       
       
      Utilities library
       
       
      bool operator[](std::size_t pos)const;
      (1)(constexpr since C++11)
      reference operator[](std::size_t pos);
      (2)(constexpr since C++23)

      Accesses the bit at positionpos.

      1) Returns the value of the bit.
      2) Returns an object of typestd::bitset::reference that allows modification of the value.

      Ifpos< size() isfalse, the behavior is undefined.

      (until C++26)

      Ifpos< size() isfalse:

      • If the implementation ishardened, acontract violation occurs. Moreover, if the contract-violation handler returns under “observe” evaluation semantic, the behavior is undefined.
      • If the implementation is not hardened, the behavior is undefined.
      (since C++26)

      Contents

      [edit]Parameters

      pos - position of the bit to return

      [edit]Return value

      1) The value of the requested bit.
      2) An object of typestd::bitset::reference, which allows writing to the requested bit.

      [edit]Exceptions

      Throws nothing.

      [edit]Example

      Run this code
      #include <bitset>#include <cstddef>#include <iostream> int main(){std::bitset<8> b1{0b00101010};// binary literal for 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::reference std::cout<<"After setting bit 0, b1 holds "<< b1<<'\n';}

      Output:

      b1[0]: 0b1[1]: 1b1[2]: 0b1[3]: 1b1[4]: 0b1[5]: 1b1[6]: 0b1[7]: 0After setting bit 0, b1 holds 00101011

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 11C++981. the description was missing in the C++ standard
      2. there was only the non-const overload
      1. description added
      2. added the const overload
      LWG 907C++98the behavior of reading the bit atpos was equivalent
      to that oftest(pos), buttest() may throw exceptions
      avoids mentioningtest()

      [edit]See also

      accesses specific bit
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/bitset/operator_at&oldid=182351"

      [8]ページ先頭

      ©2009-2025 Movatter.jp