GO
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


Technique

The List and other sequential containers such as vector and deque have member functions that make container maintenance simple. These functions can be used to create the container, and add elements to the back, front (except vector), and middle of the container. You can get the elements from the back or front of the container and perform a few other operations. When creating a new class based on such a container, use member functions to support procedures specific to the class.

However, a few operations are not supported in the sequential containers. To find a certain element in a list of words or to count all elements that start with letter s, algorithms have to be used. In the Standard Template Library, algorithms are separated from the containers. During software design and development, these generic algorithms are applied to the specific needs of our class.

This How-To shows the creation of a class containing names. The class implementation is based on the list container. This How-To also compares the usage of certain algorithms and the usage of member functions.

Steps

1.  Create a class declaration
Imagine you are developing a big application for text processing. The program is going to process English text. For the parsing and the keyword search, you would need a few data sets, such as prepositions or English names. To maintain the names, you must create a separate class that allows you to perform a few operations on the names, such as adding, searching, sorting, and removing duplicates.
First of all, you have to decide on the appropriate container for your needs. You want to do sequential operations that will process ranges of the data, such as “all names from Richard to Tom.” Therefore, select sequential containers for the program. STL has three types of sequential containers: lists, vectors, and deques. (Some STL implementations also have slist and bit-vector containers; however, they are not common.) Choose the list container because it inserts the data faster than the vector or the deque. In STL, the list container represents a doubly linked list. An element on this list contains the pointers both to the next element and the preceding one.
The following code is the class declaration:
//persname.h

#include <list>
#include <string>
using namespace std;

class persname
{

public:
    persname();                // constructor
    ~persname();            // destructor

    // Add a new name
    void Add(string NewName);
    // Add a new name after an existing one
    void AddBefore(string NewName, string BeforeName);
    // Sort the names in alphabetical order
    void Sort(void);
    // Remove duplicate names from the sorted list
    void RemoveDuplicates(void);
    // Check is the name exists in the list
    bool NameExists(string TestName);

    // Count ‘TestName’ names
    int CountNames(string TestName);
    // Display all names
    void DisplayAll(void);

private:
    list <string> PName;

};

The private part of the class contains the list of strings. To support the strings as well as the STL lists, you must include the header files:
#include <list>
#include <string>

The Microsoft Visual C++ 5 compiler was used for this example. Depending on your compiler, you might change the header file names to <list.h> and <string.h> or some other names. You have to check your compiler reference to get the correct names.
In addition to the constructor and the destructor member functions, the public part of the class contains the following functions:
  void Add(string NewName) adds a new name of the string type to the end of the list.
  void AddBefore(string NewName, string BeforeName) adds a new name of the string type before the first occurrence of a specified name or to the back of the list if the specified name does not exist.
  void Sort(void) sorts all names in the list in ascending alphabetical order.
  void RemoveDuplicates(void) removes consecutive duplicate names from the list. The function is not supposed to remove all duplicates; it can do so only if the list is sorted.
  bool NameExists(string TestName) is one of the most important functions in the class. During a parsing procedure the function is executed to check words against the names in the list.
  int CountName(string TestName) counts the occurrences of TestName in the list.
  void DisplayAll(void) displays all names in the list to the computer screen.

These functions are just the first step in the class development. Later, you are going to create more functions to satisfy more needs of the class.
2.  Class implementation
Your class implementation will be very simple. You can improve the class functionality by adding validation routines or improve the performance by changing the implementation according to the specifics of your compiler. One of the changes can be done if your version of STL supports the slist container (singly linked list). The singly linked list would perform better in this case because you don’t need backward operations.
// persname.cpp

#include <iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;

#include “persname.h”

persname::persname()
{

}

persname::~persname()
{

}

// Add a new name
void persname::Add(string NewName)
{
    PName.push_back(NewName);

}

// Add a new name before an existing one or to the end
// of the list
void persname::AddBefore(string NewName, string BeforeName)
{
    list <string>::iterator SResult;

    SResult = find(PName.begin(), PName.end(), BeforeName);
    PName.insert(SResult, NewName);
}

// Sort the names in alphabetic order
void persname::Sort(void)
{
    PName.sort();
}

// Remove duplicate names from the sorted list
void persname::RemoveDuplicates(void)
{

    list <string>::iterator new_end;
    new_end = unique(PName.begin(), PName.end());
    PName.erase(new_end, PName.end());
}

// Check is the name exists in the list
bool persname::NameExists(string TestName)
{
    list <string>::iterator SResult;

    SResult = find(PName.begin(), PName.end(), TestName);
    if (SResult == PName.end()) return false;
    return true;

}

// Count all names
int persname::CountNames(string TestName)
{

    return count (PName.begin(), PName.end(), TestName);
}

// Display all names
void persname::DisplayAll(void)
{
    list <string>::iterator i;

    for (i = PName.begin(); i!=PName.end(); i++)
        cout << *i << endl;
}


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.