-->
Previous Table of Contents Next


Sets

The Set 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.

Table 27.10. Set operators.

Operator Description

Set s Declares a set named s that is initially empty
Set s(sz) Declares a set named s that is initially empty and has a set maximum size of sz
s.empty() Returns TRUE if s is empty
s.length() Returns the number of elements in s
i = s.add(z) Adds z to s, returning its index value
s.del(z) Deletes z from s
s.clear() Removes all elements from s
s.contains(z) Returns TRUE if z is in s
s.(i) Returns a pointer to the element indexed by i
i = a.first() Returns the index of the first item in the set
s.next(i) Makes i equal to the index of the next element in s
i = s.seek(z) Sets i to the index of z if z is in s, and 0 otherwise
set1 == set2 Returns TRUE if set1 contains all the same elements as set2
set1 != set2 Returns TRUE if set1 does not contain all the same elements as set2
set1 <= set2 Returns TRUE if set1 is a subset of set2
set1 |= set2 Adds all elements of set2 to set1
set1 -= set2 Deletes all the elements that are contained in set2 from set1
set1 &= set2 Deletes all elements from set1 that occur in set1 and not in set2

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 ==, !=, |=, <=, |=, -=, and &= 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.

Table 27.11. Additional operators for bags.

Operator Description

b.remove(z) Deletes all occurrences of z from b
b.nof(z) Returns the number of occurrences of z that are in b

Many other classes available in the GNU C++ 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.

Summary

C++ 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++ programmers. This chapter gives a brief introduction to object-oriented programming and also talks about the C++ features that exist in the GNU C compiler and the GNU debugger.

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

Programming C under Linux is discussed in Chapter 26, “Programming in C.”
Perl, a handle language for quick programming tasks, is discussed in Chapter 28, “Perl.”
The compilers available for Linux are discussed in Chapter 30, “Other Compilers.”


Previous Table of Contents Next