Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


Table 9.4 Generalized Numeric Operations
Algorithm Description
accumulate Accumulates all elements in a range
inner_product Calculates the inner product of the elements in two ranges
partial_sum Calculates a generalized partial sum
adjacent_difference Calculates the differences of adjacent elements

9.1 Create Classes for Sequential Containers

A sequential container in the STL is a container whose elements are arranged in a linear order. vector, deque, and list are examples of sequential containers. When developing a class based on the vector or on the list container, programmers might want to apply the algorithms to the container class. This How-To describes this process and shows the usage of count, find, and unique algorithms.

9.2 Use Predicates with Sequence Operations

An STL predicate is a unary function that returns a result representing the truth or the falsehood. The result should be convertible to the bool data type. The equality operator (==) is a simple example of a predicate. Nonmutating and mutating sequence operations, such as count or find, can be extended to the operations that compare the elements using predicates other than the equality operator. Usually, the extended version of the algorithm ends with _if, such as count_if and find_if. This How-To shows the usage of the _if as well as of the other functions with predicates.

9.3 Repeat an Action with All Elements in a Container Range

The for statement in C++ is a very common solution to perform an action on a range of elements. The elements usually can be accessed by using a pointer, and the for loop increments the pointer by performing a move to the next element. An analog of this operation is provided in the STL with the for_each algorithm. The algorithm accesses elements in a container sequentially in a specified range. The algorithm consecutively applies a function to the elements. This How-To shows an example of the usage of the for_each algorithm.

9.4 Compare Two Sequences

A comparison of two sequences is solved by a few algorithms. The equal and the mismatch algorithms are very similar. However, there is a major difference: The mismatch algorithm returns the iterator to the first element that is different in the sequences; the equal algorithm returns just true or false depending on the result of the comparison. This How-To provides an example of the two algorithms and describes their usage.

9.5 Search for a Sequence of Values in a Container

Another very common task programmers have to solve is the search for a sub-sequence within a sequence of elements. C and C++ have standard functions that help solve this task with strings as sequences of characters. The STL provides the search algorithms that can be applied to different containers. This How-To shows how you can search for a sequence of words in text.

9.6 Accumulate All Container Elements and Create a Sequence of Accumulated Sums

A quite common task in programming consists of calculating sums and accumulated sums for a sequence of data. The extension of this task can be an accumulation of data based on an operation other than addition. For example, calculating an accumulated product also happens very often. The STL functions, such as accumulate and partial_sum, can help programmers perform these common tasks. This How-To explores the accumulation of data in the STL.

9.7 Sort Elements in a Container Using Different Sorting Indexes

The sort algorithm in the Standard Template Library can have a different syntax. It can use a default relational operator for ordering the data or another function specified by the programmer. A class that can use the sort algorithm should provide certain features. This How-To creates a phone book class and then applies the sort algorithm to it. The sorting order is provided by the overloaded relational operator firs and then by the user-defined LessThan function.

9.8 Change the Order of the Container Elements

It is very important to a programmer to be able to change the order of the sequence of elements. If the elements represent tasks in a queue, programs quite often rotate the elementsÑthat is, take the first element and move it to the back of the queue. If the elements represent playing cards in a computer game, the programs need to shuffle the deck of cards. It might be necessary to reverse the order of the data elements for other applications. This How-To describes the rotate, reverse, and random_shuffle algorithms. These mutating algorithms represent common operations on data that change the order of the elements in a sequence.

The second part of the How-To describes the partition and the stable_partition algorithms. These algorithms move all elements that satisfy a certain predicate before other elements in the sequence. In addition, the stable_partition algorithm preserves the relative order in the parts of the sequence.

9.1 Create classes for sequential containers?

Problem

I develop programs, such as parsers, for text processing. I want to maintain certain dictionaries as lists, for instance, a list of names. For the list of names, I want to create a fully operational class. However, the members of the list container don’t support many operations, such as counting or finding elements based on certain criteria. On the other hand, some member functions of the list container provide the same functionality as some algorithms. For example, there is the unique member function and the unique algorithm, as well. Which functions should I use to create the class in a simple and elegant way?


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.