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



The first and the last random access iterators specify the range in the data container that should be sorted. The assumptions that the algorithm makes are
  The container has random access to its elements between the two iterators. It is common to specify the range with the notation [first, last).
  The operator < is defined for the class. The sort algorithm uses this operator to determine the ordering in the container.

For the sort algorithm, you have to include the file algorithm, which works fine with Microsoft Visual C++. Other compilers might need algorithm.h, algo.h, or other header files.
4.  Testing the sorting
The following programming code shows how you can apply the simple version of the sort algorithm to sort vectors using the overloaded < operator to specify the sorting order.
// phbtest.cpp

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#include “phbook.h”

int main()
{

    vector <PhBook> MyPhoneBook;

    // initialize phone book lines
    PhBook PBLine1(“Smith”, “Robert”, “Portland, OR”, “503”,
                   ⇒”2027656”);
    PhBook PBLine2(“Smith”, “John”, “Seattle, WA”, “206”,
                   ⇒”7681290”);
    PhBook PBLine3(“Barney”, “Tom”, “Eugene, OR”, “541”,
                   ⇒”7682322”);
    PhBook PBLine4(“Anderson”, “Peter”, “San Diego, CA”, “619”,
                   ⇒”5451551”);
    PhBook PBLine5(“Smith”, “John”, “New York, NY”, “212”,
                   ⇒ “1234567”);

    // set up vector values
    MyPhoneBook.push_back (PBLine1);
    MyPhoneBook.push_back (PBLine2);
    MyPhoneBook.push_back (PBLine3);
    MyPhoneBook.push_back (PBLine4);
    MyPhoneBook.push_back (PBLine5);

    sort(MyPhoneBook.begin(), MyPhoneBook.end());

    // display the vector after sorting
    vector<PhBook>::iterator i;
    for (i = MyPhoneBook.begin(); i != MyPhoneBook.end(); ++i)
         i->DisplayPhone();
    return 0;
}

In addition to the algorithm header file, you have to include the vector header file, which supports vector containers.
The program defines the MyPhoneBook vector and adds five lines to it. The lines are added to the back of the container using push_back functions. To specify the sorting range, use begin and end functions that return iterators setting up all elements of the container for the sorting. The following line applies the sort algorithm:
sort(MyPhoneBook.begin(), MyPhoneBook.end());

The sorted vector is displayed using the DisplayPhone member function of the PhBook class:
vector<PhBook>::iterator i;
for (i = MyPhoneBook.begin(); i != MyPhoneBook.end(); ++i)
    i->DisplayPhone();

All elements of the vector are displayed in the natural order. The range is specified with iterators that start from MyPhoneBook.begin() and finish with MyPhoneBook.end(). The latter iterator points beyond the last element in the container.
The output of the program shows that the algorithm sorted the phone book entries using only the name fields:
Anderson, Peter: (619)545-1551
Barney, Tom: (541)768-2322
Smith, John: (206)768-1290
Smith, John: (212)123-4567
Smith, Robert: (503)202-7656

The output that was produced on my computer with Microsoft Visual C++ version 5 kept the original order of the equal records
Smith, John: (206)768-1290
Smith, John: (212)123-4567

However, this behavior is not necessary. To make sure that the relative order of equal elements remains unchanged, use the stable_sort algorithm instead of the sort algorithm:
stable_sort(MyPhoneBook.begin(), MyPhoneBook.end());
5.  Determine the ordering by other means than the < operator
Very often, you will want to sort the records in your phone book in another order. You might want to sort the records by cities first, and then by names or by area codes. Let’s write a program that sorts the phone book according to the LessThan function in the PhBook class.
The STL requires that the ordering function is defined with a function object—a class that defines the function-call operator (operator()).
class NameCompare:binary_function <PhBook, PhBook, bool>
{
public:
    bool operator()(const PhBook& a, const PhBook& b) {
        return (a.LessThan(b));
    }
};

In order to use binary_function, you have to include the <functional> header file.
Now you can change the sort statement in the phbtest program.
// phbtest.cpp

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

#include “phbook.h”

class NameCompare:binary_function <PhBook, PhBook, bool>
{
public:
    bool operator()(const PhBook& a, const PhBook& b) {
        return (a.LessThan(b));
    }
};

int main()
{
    vector <PhBook> MyPhoneBook;
    // initialize phone book lines
    PhBook PBLine1(“Smith”, “Robert”, “Portland, OR”, “503”,
                   ⇒”2027656”);
    PhBook PBLine2(“Smith”, “John”, “Seattle, WA”, “206”,
                   ⇒”7681290”);
    PhBook PBLine3(“Barney”, “Tom”, “Eugene, OR”, “541”,
                   ⇒”7682322”);
    PhBook PBLine4(“Anderson”, “Peter”, “San Diego, CA”, “619”,
                   ⇒”5451551”);
    PhBook PBLine5(“Smith”, “John”, “New York, NY”, “212”,
                   ⇒”1234567”);

    // set up vector values
    MyPhoneBook.push_back (PBLine1);
    MyPhoneBook.push_back (PBLine2);
    MyPhoneBook.push_back (PBLine3);
    MyPhoneBook.push_back (PBLine4);
    MyPhoneBook.push_back (PBLine5);

    // sort the vector
    sort(MyPhoneBook.begin(), MyPhoneBook.end(), NameCompare());
  // display the vector after sorting
  for (int i = MyPhoneBook.begin(); i != MyPhoneBook.end(); ++i)
      i->DisplayCity();

    return 0;
}


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.