16 14.4 The STL list<T> Class Template
These are just a few of the list<T> operations. A complete list can be found at the end of this
section.
AN APPLICATON: INTERNET ADDRESSES
The TCP (Transmission Control Protocol) and IP (Internet Protocol) communication protocols
specify the rules computers use in exchanging messages on the Internet. TCP/IP addresses are
used to uniquely identify computers in the Internet; for example, www.ksc.nasa.gov is the
address of a site at the NASA Kennedy Space Center. Such an address is made up of four fields
that represent specific parts of the Internet,
host.subdomain.subdomain.rootdomain
which the computer will translate into a unique TCP/IP numeric address. This address is a 32-bit
value, but it is usually represented in a dotted-decimal notation by separating the 32 bits into
four 8-bit fields, expressing each field as a decimal integer, and separating the fields with a
period; for example, 198.119.202.36 is the TCP/IP numeric address for the above site at the
NASA Kennedy Space Center. (A Part of the Picture detailing the TCP/IP Communications
Architecture written by William Stallings is available on the text’s CD and website.)
Problem. A gateway is a device used to interconnect two different computer networks. Sup-
pose that a gateway connects a university to the Internet and that the university’s network admin-
istrator needs to monitor connections through this gateway. Each time a connection is made (for
example, a student using the World Wide Web), the TCP/IP address of the student’s computer is
stored in a data file. The administrator wants to check periodically who has used the gateway and
how many times they have used it.
Solution. The TCP/IP addresses will be read from the file and stored in a linked list of nodes
that will store an address and the number of times that address appeared in the data file. As each
address is read, we check if it is already in the list. If it is, we increment its count by 1; other-
wise, we simply insert it at the end of the list. After all the addresses in the file have been read,
the distinct addresses and their counts are displayed.
The following program uses this approach to solve the problem. The addresses are stored in a
list<AddressCounter> object named addrCntList, where AddressCounter is a small
class containing two instance variables (address and count), input and output methods, and a
tally() method to increment the count instance variable. Also, operator==() is overloaded
so that STL’s find() algorithm can be used to search the list.
Figure 14.2 Internet Addresses.
/* internet.cpp reads TCP/IP addresses from a file and produces a
* list of distinct addresses and a count of how many times each
* appearedin the file. The addresses and counts are stored in a