📄 bitset.html
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<TITLE>MFC Programmer's SourceBook : STL Programmer's Guide</TITLE>
<META name="description"
content="A freely available implementation
of the C++ Standard Template Library, including
hypertext documentation.">
<META name="keywords"
content="generic programming, STL, standard template library">
</HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
var adcategory = "cpp";
// -->
</SCRIPT>
<body background="../../fancyhome/back.gif" bgcolor="#FFFFFF" >
<SCRIPT LANGUAGE="JavaScript"><!--
var nfrm = location.href.indexOf("_nfrm_");
var validframes = (top.frames.length > 0 && top.frames['ad'] && top.frames['logo'] );
var random = Math.random();
if( !validframes && nfrm == -1 )
{
var dclkPage = "www.codeguru.com/";
if( self.adcategory )
dclkPage += adcategory;
else
dclkPage += "mfc";
document.write('<nolayer><center>');
document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
+ random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
+ 'frameborder=0 scrolling=no bordercolor="#000000">');
document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
+ random + '">');
document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
+ random + '" height=60 width=468>' + '</a>');
document.write('</iframe>');
document.write('</center></nolayer>');
document.write('<layer src="http://ad.doubleclick.net/adl/' + dclkPage +
';ord=' + random + '"></layer>');
document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}
// top.location = "/show.cgi?" + adcategory + "=" + location.pathname;
// -->
</SCRIPT>
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupacdFCY34AAHfZfDM">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupacdFCY34AAHfZfDM"></a>
</p>
</noscript>
<BR Clear>
<H1>bitset<N></H1>
<Table CellPadding=0 CellSpacing=0 width=100%>
<TR>
<TD Align=left><Img src = "containers.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD>
<TD Align=right><Img src = "type.gif" Alt="" WIDTH = "194" HEIGHT = "39" ></TD>
</TR>
<TR>
<TD Align=left VAlign=top><b>Category</b>: containers</TD>
<TD Align=right VAlign=top><b>Component type</b>: type</TD>
</TR>
</Table>
<h3>Description</h3>
<tt>Bitset</tt> is very similar to <A href="Vector.html">vector</A><bool> (also known as
<A href="bit_vector.html">bit_vector</A>): it contains a collection of bits, and provides
constant-time access to each bit. There are two main differences
between <tt>bitset</tt> and <tt>vector<bool></tt>. First, the size of a <tt>bitset</tt>
cannot be changed: <tt>bitset</tt>'s template parameter <tt>N</tt>, which specifies
the number of bits in the bitset, must be an integer constant.
Second, <tt>bitset</tt> is not a <A href="Sequence.html">Sequence</A>; in fact, it is not an STL
<A href="Container.html">Container</A> at all. It does not have iterators, for example, or
<tt>begin()</tt> and <tt>end()</tt> member functions. Instead, <tt>bitset</tt>'s interface
resembles that of unsigned integers. It defines bitwise arithmetic
operators such as <tt>&=</tt>, <tt>|=</tt>, and <tt>^=</tt>.
<P>
In general, bit <tt>0</tt> is the least significant bit and bit <tt>N-1</tt> is the
most significant bit.
<h3>Example</h3>
<pre>
int main() {
const bitset<12> mask(2730ul);
cout << "mask = " << mask << endl;
bitset<12> x;
cout << "Enter a 12-bit bitset in binary: " << flush;
if (cin >> x) {
cout << "x = " << x << endl;
cout << "As ulong: " << x.to_ulong() << endl;
cout << "And with mask: " << (x & mask) << endl;
cout << "Or with mask: " << (x | mask) << endl;
}
}
</pre>
<h3>Definition</h3>
Defined in <A href="bitset/index.htm">bitset</A>.
<h3>Template parameters</h3>
<Table border>
<TR>
<TH>
Parameter
</TH>
<TH>
Description
</TH>
<TH>
Default
</TH>
</TR>
<TR>
<TD VAlign=top>
<tt>N</tt>
</TD>
<TD VAlign=top>
A nonzero constant of type <tt>size_t</tt>: the number of bits that the
bitset contains.
</TD>
<TD VAlign=top>
</TD>
</tr>
</table>
<h3>Model of</h3>
<A href="Assignable.html">Assignable</A>, <A href="DefaultConstructible.html" tppabs="http://www.sgi.com/Technology/STL/DefaultConstructible.shtml">Default Constructible</A>, <A href="EqualityComparable.html" tppabs="http://www.sgi.com/Technology/STL/EqualityComparable.shtml">Equality Comparable</A>
<h3>Type requirements</h3>
<tt>N</tt> is a constant integer expression of a type convertible to
<tt>size_t</tt>, and <tt>N</tt> is a positive number.
<h3>Public base classes</h3>
None.
<h3>Members</h3>
<Table border>
<TR>
<TH>
Member
</TH>
<TH>
Where defined
</TH>
<TH>
Description
</TH>
</TR>
<TR>
<TD VAlign=top>
<tt>reference</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
A proxy class that acts as a reference to a single bit.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset()</tt>
</TD>
<TD VAlign=top>
<A href="DefaultConstructible.html">Default Constructible</A>
</TD>
<TD VAlign=top>
The default constructor. All bits are initially zero.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset(unsigned long val)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Conversion from unsigned long.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset(const bitset&)</tt>
</TD>
<TD VAlign=top>
<A href="Assignable.html">Assignable</A>
</TD>
<TD VAlign=top>
Copy constructor.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& operator=(const bitset&)</tt>
</TD>
<TD VAlign=top>
<A href="Assignable.html">Assignable</A>
</TD>
<TD VAlign=top>
Assignment operator.
</TD>
</TR>
<TR>
<TD VAlign=top>
<pre>
template<class Char, class Traits, class Alloc>
explicit
bitset(const <A href="basic_string.html">basic_string</A><Char,Traits,Alloc>& s,
size_t pos = 0,
size_t n =
<A href="basic_string.html">basic_string</A><Char,Traits,Alloc>::npos)
</pre>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Conversion from string.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& operator&=(const bitset&)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Bitwise and.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& operator|=(const bitset&)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Bitwise inclusive or.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& operator^=(const bitset&)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Bitwise exclusive or.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& operator<<=(size_t)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Left shift.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& operator>>=(size_t)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Right shift.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset operator<<(size_t n) const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns a copy of <tt>*this</tt> shifted left by <tt>n</tt> bits.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset operator>>(size_t n) const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns a copy of <tt>*this</tt> shifted right by <tt>n</tt> bits.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& set()</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Sets every bit.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& flip()</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Flips the value of every bit.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset operator~() const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns a copy of <tt>*this</tt> with all of its bits flipped.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& reset()</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Clears every bit.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& set(size_t n, int val = 1)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Sets bit <tt>n</tt> if <tt>val</tt> is nonzero, and clears bit <tt>n</tt> if <tt>val</tt> is zero.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset& reset(size_t n)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Clears bit <tt>n</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bitset flip(size_t n)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Flips bit <tt>n</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>size_t size() const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns <tt>N</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>size_t count() const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns the number of bits that are set.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bool any() const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns <tt>true</tt> if any bits are set.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bool none() const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns <tt>true</tt> if no bits are set.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bool test(size_t n) const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns <tt>true</tt> if bit <tt>n</tt> is set.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>reference operator[](size_t n)</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns a <tt>reference</tt> to bit <tt>n</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bool operator[](size_t n) const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns <tt>true</tt> if bit <tt>n</tt> is set.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>unsigned long to_ulong() const</tt>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns an <tt>unsigned long</tt> corresponding to the bits in <tt>*this</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<pre>
template<class Char, class Traits, class Alloc>
<A href="basic_string.html">basic_string</A><Char,Traits,Alloc> to_string() const
</pre>
</TD>
<TD VAlign=top>
<tt>bitset</tt>
</TD>
<TD VAlign=top>
Returns a string representation of <tt>*this</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bool operator==(const bitset&) const</tt>
</TD>
<TD VAlign=top>
<A href="EqualityComparable.html">Equality Comparable</A>
</TD>
<TD VAlign=top>
The equality operator.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bool operator!=(const bitset&) const</tt>
</TD>
<TD VAlign=top>
<A href="EqualityComparable.html">Equality Comparable</A>
</TD>
<TD VAlign=top>
The inequality operator.
</TD>
</TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -