500-502.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 152 行

HTML
152
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Programming in C&#43;&#43;</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=27//-->

<!--PAGES=500-502//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="499-500.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="../ch28/503-505.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">Sets</FONT></H4>

<P>The <TT>Set</TT> class is used to store groups of information. The only restriction on this information is that no duplicate elements are allowed. The class library supports several different implementations of sets. All of the implementations support the same operators. These operators are shown in Table 27.10.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 27.10.</B> <TT>Set</TT> operators.

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH WIDTH="40%" ALIGN="LEFT">Operator

<TH WIDTH="60%" ALIGN="LEFT">Description

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD><TT>Set s</TT>

<TD>Declares a set named <TT>s</TT> that is initially empty

<TR>

<TD VALIGN="TOP"><TT>Set s(sz)</TT>

<TD>Declares a set named <TT>s</TT> that is initially empty and has a set maximum size of <TT>sz</TT>

<TR>

<TD><TT>s.empty()</TT>

<TD>Returns <TT>TRUE</TT> if <TT>s</TT> is empty

<TR>

<TD><TT>s.length()</TT>

<TD>Returns the number of elements in <TT>s</TT>

<TR>

<TD><TT>i = s.add(z)</TT>

<TD>Adds <TT>z</TT> to <TT>s</TT>, returning its index value

<TR>

<TD><TT>s.del(z)</TT>

<TD>Deletes <TT>z</TT> from <TT>s</TT>

<TR>

<TD><TT>s.clear()</TT>

<TD>Removes all elements from <TT>s</TT>

<TR>

<TD><TT>s.contains(z)</TT>

<TD>Returns <TT>TRUE</TT> if <TT>z</TT> is in <TT>s</TT>

<TR>

<TD><TT>s.(i)</TT>

<TD>Returns a pointer to the element indexed by <TT>i</TT>

<TR>

<TD><TT>i = a.first()</TT>

<TD>Returns the index of the first item in the set

<TR>

<TD><TT>s.next(i)</TT>

<TD>Makes <TT>i</TT> equal to the index of the next element in <TT>s</TT>

<TR>

<TD><TT>i = s.seek(z)</TT>

<TD>Sets <TT>i</TT> to the index of <TT>z</TT> if <TT>z</TT> is in <TT>s</TT>, and 0 otherwise

<TR>

<TD VALIGN="TOP"><TT>set1 == set2</TT>

<TD>Returns <TT>TRUE</TT> if <TT>set1</TT> contains all the same elements as <TT>set2</TT>

<TR>

<TD VALIGN="TOP"><TT>set1 != set2</TT>

<TD>Returns <TT>TRUE</TT> if <TT>set1</TT> does not contain all the same elements as <TT>set2</TT>

<TR>

<TD VALIGN="TOP"><TT>set1 &lt;= set2</TT>

<TD>Returns <TT>TRUE</TT> if <TT>set1</TT> is a subset of <TT>set2</TT>

<TR>

<TD><TT>set1 |= set2</TT>

<TD>Adds all elements of <TT>set2</TT> to <TT>set1</TT>

<TR>

<TD VALIGN="TOP"><TT>set1 -= set2</TT>

<TD>Deletes all the elements that are contained in <TT>set2</TT> from <TT>set1</TT>

<TR>

<TD VALIGN="TOP"><TT>set1 &#38;= set2</TT>

<TD>Deletes all elements from <TT>set1</TT> that occur in <TT>set1</TT> and not in <TT>set2</TT>

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P>The class library contains another class that is similar to sets. This class is known as the bag. A bag is a group of elements that can be in any order (just as is the case with sets) but in which there can also be duplicates. Bags use all the operators that sets use except for the <TT>==</TT>, <TT>!=</TT>, <TT>|=</TT>, <TT>&lt;=</TT>, <TT>|=</TT>, <TT>-=</TT>, and <TT>&#38;=</TT> operators. In addition, bags add two new operators for dealing with elements that are in the bag more than once. These new operators are shown in Table 27.11.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 27.11.</B> Additional operators for bags.

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH WIDTH="40%" ALIGN="LEFT">Operator

<TH WIDTH="60%" ALIGN="LEFT">Description

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD><TT>b.remove(z)</TT>

<TD>Deletes all occurrences of <TT>z</TT> from <TT>b</TT>

<TR>

<TD><TT>b.nof(z)</TT>

<TD>Returns the number of occurrences of <TT>z</TT> that are in <TT>b

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P>Many other classes available in the GNU C&#43;&#43; class library provide functions other than those listed here. In addition to what comes with the compiler, many other freely available class libraries can be useful, as well.

</P>

<H3><A NAME="Heading22"></A><FONT COLOR="#000077">Summary</FONT></H3>

<P>C&#43;&#43; offers many advantages over C. Some of these advantages come from the concepts of object-oriented programming, and others come from the highly flexible class libraries that are available to C&#43;&#43; programmers. This chapter gives a brief introduction to object-oriented programming and also talks about the C&#43;&#43; features that exist in the GNU C compiler and the GNU debugger.

</P>

<P>One problem that has existed with C&#43;&#43; for quite some time is the lack of freely available C&#43;&#43; development tools. You may notice that the number of free tools available for C&#43;&#43; programming is much smaller than the number available for C, but the tide is turning. As more and more people choose C&#43;&#43; over C, the number of tools and class libraries available keeps increasing. The tool support has reached the stage where learning C&#43;&#43; in the Linux environment is something that you can enjoy rather than avoid. See the following chapters for related information:</P>

<DL>

<DD>Programming C under Linux is discussed in Chapter 26, &#147;Programming in C.&#148;

<DD>Perl, a handle language for quick programming tasks, is discussed in Chapter 28, &#147;Perl.&#148;

<DD>The compilers available for Linux are discussed in Chapter 30, &#147;Other Compilers.&#148;

</DL>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="499-500.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="../ch28/503-505.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?