Home > Articles > Programming > C/C++

This chapter is from the book

3.8 Algorithms

A data structure, such as a list or a vector, is not very useful on its own. To use one, we need operations for basic access such as adding and removing elements. Furthermore, we rarely just store objects in a container. We sort them, print them, extract subsets, remove elements, search for objects, etc. Consequently, the standard library provides the most common algorithms for containers in addition to providing the most common container types. For example, the following sorts a vector and places a copy of each unique vector element on a list:

void f(vector<Entry>& ve, list<Entry>& le)
{
  sort(ve.begin() ,ve.end()) ;
  unique_copy(ve.begin() ,ve.end() ,le.begin()) ;
}

The standard algorithms are expressed in terms of sequences of elements. A sequence is represented by a pair of iterators specifying the first element and the one-beyond-the-last element. In the example, sort() sorts the sequence from ve.begin() to ve.end()—which just happens to be all the elements of a vector. For writing, you need only to specify the first element to be written. If more than one element is written, the elements following that initial element will be overwritten.

If we wanted to add the new elements to the end of a container, we could have written this:

void f(vector<Entry>& ve, list<Entry>& le)
{
  sort(ve.begin() ,ve.end()) ;
  unique_copy(ve.begin() ,ve.end() ,back_inserter(le)) ; // append to le
}

A back_inserter() adds elements at the end of a container, extending the container to make room for them. Thus, the standard containers plus back_inserter()s eliminate the need to use error-prone, explicit C-style memory management using realloc(). Forgetting to use a back_inserter() when appending can lead to errors. For example:

void f(vector<Entry>& ve, list<Entry>& le)
{
  copy(ve.begin() ,ve.end() ,le) ;     // error: le not an iterator
  copy(ve.begin() ,ve.end() ,le.end()) ;  // bad: writes beyond the end
  copy(ve.begin() ,ve.end() ,le.begin()) ; // overwrite elements
}

3.8.1 Use of Iterators

When you first encounter a container, a few iterators referring to useful elements can be obtained; begin() and end() are the best examples of this. In addition, many algorithms return iterators. For example, the standard algorithm find looks for a value in a sequence and returns an iterator to the element found. Using find, we can count the number of occurrences of a character in a string:

int count(const string& s, char c) // count occurrences of c in s
{
  int n = 0;
  string: :const_iterator i = find(s.begin() ,s.end() ,c) ;
  while (i != s.end()) {
   ++n;
   i = find(i+1,s.end() ,c) ;
  }
  return n;
}

The find algorithm returns an iterator to the first occurrence of a value in a sequence or the one-past-the-end iterator. Consider what happens for a simple call of count:

void f()
{
  string m = "Mary had a little lamb";
  int a_count = count(m,'a') ;
}

The first call to find() finds the a in Mary. Thus, the iterator points to that character and not to s.end(), so we enter the loop. In the loop, we start the search at i+1; that is, we start one past where we found the a. We then loop finding the other three a's. That done, find() reaches the end and returns s.end() so that the condition i!=s.end() fails and we exit the loop.

That call of count() could be graphically represented like this:

Figure 1

The arrows indicate the initial, intermediate, and final values of the iterator i.

Naturally, the find algorithm will work equivalently on every standard container. Consequently, we could generalize the count() function in the same way:

template<class C, class T> int count(const C& v, T val)
{
  typename C: :const_iterator i = find(v.begin() ,v.end() ,val) ;
  int n = 0;
  while (i != v.end()) {
   ++n;
   ++i;   // skip past the element we just found
   i = find(i,v.end() ,val) ;
  }
  return n;
}

This works, so we can say:

void f(list<complex>& lc, vector<string>& vs, string s)
{
  int i1 = count(lc,complex(1,3)) ;
  int i2 = count(vs,"Diogenes") ;
  int i3 = count(s,'x') ;
}

However, we don't have to define a count template. Counting occurrences of an element is so generally useful that the standard library provides that algorithm. To be fully general, the standard library count takes a sequence as its argument, rather than a container, so we would say:

void f(list<complex>& lc, vector<string>& vs, string s)
{
  int i1 = count(lc.begin() ,lc.end() ,complex(1,3)) ;
  int i2 = count(vs.begin() ,vs.end() ,"Diogenes") ;
  int i3 = count(s.begin() ,s.end() ,'x') ;
}

The use of a sequence allows us to use count for a built-in array and also to count parts of a container. For example:

void g(char cs[] , int sz)
{
  int i1 = count(&cs[0] ,&cs[sz] ,'z') ; // 'z's in array
  int i2 = count(&cs[0] ,&cs[sz/2] ,'z') ; // 'z's in first half of array
}

3.8.2 Iterator Types

What are iterators really? Any particular iterator is an object of some type. There are, however, many different iterator types because an iterator needs to hold the information necessary for doing its job for a particular container type. These iterator types can be as different as the containers and the specialized needs they serve. For example, a vector's iterator is most likely an ordinary pointer because a pointer is quite a reasonable way of referring to an element of a vector:

Figure 2

Alternatively, a vector iterator could be implemented as a pointer to the vector plus an index:

Figure 3

Using such an iterator would allow range checking.

A list iterator must be something more complicated than a simple pointer to an element because an element of a list in general does not know where the next element of that list is. Thus, a list iterator might be a pointer to a link:

Figure 4

What is common for all iterators is their semantics and the naming of their operations. For example, applying ++ to any iterator yields an iterator that refers to the next element. Similarly, * yields the element to which the iterator refers. In fact, any object that obeys a few simple rules like these is an iterator. Furthermore, users rarely need to know the type of a specific iterator; each container ''knows'' its iterator types and makes them available under the conventional names iterator and const_iterator. For example, list<Entry>: :iterator is the general iterator type for list<Entry>. I rarely have to worry about the details of how that type is defined.

3.8.3 Iterators and I/O

Iterators are a general and useful concept for dealing with sequences of elements in containers. However, containers are not the only place where we find sequences of elements. For example, an input stream produces a sequence of values and we write a sequence of values to an output stream. Consequently, the notion of iterators can be usefully applied to input and output.

To make an ostream_iterator, we need to specify which stream will be used and the type of objects written to it. For example, we can define an iterator that refers to the standard output stream, cout:

ostream_iterator<string> oo(cout) ;

The effect of assigning to *oo is to write the assigned value to cout. For example:

int main()
{
  *oo = "Hello, ";   // meaning cout ___"Hello, "
  ++oo;
  *oo = "world!\n";  // meaning cout ___"world!\n"
}

This is yet another way of writing the canonical message to standard output. The ++oo is done to mimic writing into an array through a pointer. This way wouldn't be my first choice for that simple task, but the utility of treating output as a write-only container will soon be obvious—if it isn't already.

Similarly, an istream_iterator is something that allows us to treat an input stream as a read-only container. Again, we must specify the stream to be used and the type of values expected:

istream_iterator<string> ii(cin) ;

Because input iterators invariably appear in pairs representing a sequence, we must provide an istream_iterator to indicate the end of input. This is the default istream_iterator:

istream_iterator<string> eos;

We could now read Hello, world! from input and write it out again like this:

int main()
{
  string s1 = *ii;
  ++ii;
  string s2 = *ii;

  cout << s1 << ' ' << s2 << '\n';
}

Actually, istream_iterators and ostream_iterators are not meant to be used directly. Instead, they are typically provided as arguments to algorithms. For example, we can write a simple program to read a file, sort the words read, eliminate duplicates, and write the result to another file:

int main()
{
  string from, to;
  cin >> from >> to;           // get source and target file names

  ifstream is(from.c_str()) ;      // input stream (c_str(); see §3.5.1)
  istream_iterator<string> ii(is) ;   // input iterator for stream
  istream_iterator<string> eos;     // input sentinel

  vector<string> b(ii,eos) ;       // b is a vector initialized from input
  sort(b.begin() ,b.end()) ;       // sort the buffer

  ofstream os(to.c_str()) ;       // output stream
  ostream_iterator<string> oo(os,"\n") ; // output iterator for stream

  unique_copy(b.begin() ,b.end() ,oo) ; // copy buffer to output,
  // discard replicated values

  return !is.eof() || !os;        // return error state (§3.2)
}

An ifstream is an istream that can be attached to a file, and an ofstream is an ostream that can be attached to a file. The ostream_iterator's second argument is used to delimit output values.

3.8.4 Traversals and Predicates

Iterators allow us to write loops to iterate through a sequence. However, writing loops can be tedious, so the standard library provides ways for a function to be called for each element of a sequence.

Consider writing a program that reads words from input and records the frequency of their occurrence. The obvious representation of the strings and their associated frequencies is a map:

map<string,int> histogram;

The obvious action to be taken for each string to record its frequency is as follows:

void record(const string& s)
{
  histogram[s]++; // record frequency of ''s''
}

Once the input has been read, we would like to output the data we have gathered. The map consists of a sequence of (string,int) pairs. Consequently, we would like to call

void print(const pair<const string,int>& r)
{
  cout << r.first << ' ' << r.second << '\n';
}

for each element in the map (the first element of a pair is called first, and the second element is called second). The first element of the pair is a const string rather than a plain string because all map keys are constants.

Thus, the main program becomes:

int main()
{
  istream_iterator<string> ii(cin) ;
  istream_iterator<string> eos;

  for_each(ii,eos,record) ;
  for_each(histogram.begin() ,histogram.end() ,print) ;
}

Note that we don't need to sort the map to get the output in order. A map keeps its elements ordered so that an iteration traverses the map in (increasing) order.

Many programming tasks involve looking for something in a container rather than simply doing something to every element. For example, the find algorithm provides a convenient way of looking for a specific value. A more general variant of this idea looks for an element that fulfills a specific requirement. For example, we might want to search a map for the first value larger than 42. A map is a sequence of (key,value) pairs, so we search that list for a pair<const string,int> where the int is greater than 42:

bool gt_42(const pair<const string,int>& r)
{
  return r.second>42;
}

void f(map<string,int>& m)
{
  typedef map<string,int>: :const_iterator MI;
  MI i = find_if(m.begin() ,m.end() ,gt_42) ;
  // ...
}

Alternatively, we could count the number of words with a frequency higher than 42:

void g(const map<string,int>& m)
{
  int c42 = count_if(m.begin() ,m.end() ,gt_42) ;
  // ...
}

A function, such as gt_42(), that is used to control the algorithm is called a predicate. A predicate is called for each element and returns a Boolean value, which the algorithm uses to perform its intended action. For example, find_if() searches until its predicate returns true to indicate that an element of interest has been found. Similarly, count_if() counts the number of times its predicate is true.

The standard library provides a few useful predicates and some templates that are useful for creating more.

3.8.5 Algorithms Using Member Functions

Many algorithms apply a function to elements of a sequence. For example, in §3.8.4,

for_each(ii,eos,record) ;

calls record() for each string read from input.

Often, we deal with containers of pointers and we really would like to call a member function of the object pointed to, rather than a global function on the pointer. For example, we might want to call the member function Shape: :draw() for each element of a list<Shape*>. To handle this specific example, we simply write a nonmember function that invokes the member function. For example:

void draw(Shape* p)
{
  p->draw() ;
}

void f(list<Shape*>& sh)
{
  for_each(sh.begin() ,sh.end() ,draw) ;
}

By generalizing this technique, we can write the example like this:

void g(list<Shape*>& sh)
{
  for_each(sh.begin() ,sh.end() ,mem_fun(&Shape: :draw)) ;
}

The standard library mem_fun() template takes a pointer to a member function as its argument and produces something that can be called for a pointer to the member's class. The result of mem_fun(&Shape: :draw) takes a Shape* argument and returns whatever Shape: :draw() returns.

The mem_fun() mechanism is important because it allows the standard algorithms to be used for containers of polymorphic objects.

3.8.6 Standard Library Algorithms

What is an algorithm? Knuth's general definition of an algorithm is ''a finite set of rules which gives a sequence of operations for solving a specific set of problems [and] has five important features: Finiteness...Definiteness...Input...Output...Effectiveness." In the context of the C++ standard library, an algorithm is a set of templates operating on sequences of elements.

The standard library provides dozens of algorithms. The algorithms are defined in namespace std and presented in the <algorithm> header. Here are a few I have found particularly useful:

Selected Standard Algorithms

for_each()

Invoke function for each element

find()

Find first occurrence of arguments

find_if()

Find first match of predicate

count()

Count occurrences of element

count_if()

Count matches of predicate

replace()

Replace element with new value

replace_if()

Replace element that matches predicate with new value

copy()

Copy elements

unique_copy()

Copy elements that are not duplicates

sort()

Sort elements

equal_range()

Find all elements with equivalent values

merge()

Merge sorted sequences


These algorithms and many more can be applied to elements of containers, strings, and built-in arrays.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020