Main Page   File List   File Members  

erase_demo.cpp File Reference

#include <stdio.h>
#include <strings.h>
#include <vector>

Go to the source code of this file.

Typedefs

typedef std::vector< const
char *> 
strVec

Functions

strVec::iterator find (strVec::iterator first, strVec::iterator last, const char *value)
void test_vector_erase (const char *searchStr)
 main (int argc, const char *argv[])


Detailed Description

This code demonstrates how to use the STL vector erase() function. The erase() can be used to remove an element from a vector container without leaving a "hole".

Definition in file erase_demo.cpp.


Function Documentation

strVec::iterator find strVec::iterator   first,
strVec::iterator   last,
const char *   value
 

Iterate over the container vector<const char *> searching for an element equal to the string pointed to by value. The function returns the iterator value where the element was found or last if it was not found. Note that last is also equal to the value returned by the vector function end().

This function is a specialized version of the STL global find function template shown below.

     template <class InputIterator, class T>
     InputIterator 
        find (InputIterator first, InputIterator last, const T& value)
     {
        while (first != last && *first != value) 
           ++first;
        return first;
     }

Definition at line 45 of file erase_demo.cpp.

Referenced by test_vector_erase().

00046 {
00047    while (first != last && strcmp(*first, value) != 0)
00048    {
00049            ++first;
00050    }
00051    return first;
00052 } // find

main int   argc,
const char *   argv[]
 

\fn Test program for the STL vector erase function.

The program is passed a string from the command line.

Definition at line 139 of file erase_demo.cpp.

00140 {
00141    if (argc != 2) {
00142       const char *progName = argv[0];
00143 
00144       printf("Usage: %s <search-string>\n", progName );
00145       printf("example: %s zorch\n", progName);
00146       printf("In this example, %s will search the vector for \"zorch\"\n", progName);
00147    }
00148    else {
00149       test_vector_erase( argv[1] );
00150    }
00151 }

void test_vector_erase const char *   searchStr
 

Demonstrate the STL vector erase function.

The STL vector container is an array like container. Elements are stored in sequential order from 0..N-1. In a standard array if an element is removed from the array, conceptually, a "hole" will be left where the element was. The stl::vector erase() class function allows an element to be removed from a vector container. The other elements are closed up around the hole.

It would be nice if one could simply call erase() with an integer value and as long as the value was in range, the corresponding element would be removed from the array. For example:

        v.erase( 5 );  // You can't do this!
    

Of course with STL things are never so easy. The argument to erase is an iterator. The element is found by searching through the vector container using an iterator (see find() in this file) and the iterator for the element is returned. This iterator value is passed as an argument to erase.

This function demonstrates this by building a vector of const char strings. A string, passed from the command line, searched for in the vector container. If it is found the string is removed from the table.

Definition at line 85 of file erase_demo.cpp.

Referenced by main().

00086 {
00087    static const char *stringz[] = { "one",
00088                                     "two",
00089                                     "three",
00090                                     "four",
00091                                     "five",
00092                                     "six",
00093                                     "seven",
00094                                     "eight",
00095                                     "nine",
00096                                     "ten",
00097                                     0 };
00098    
00099    strVec v;
00100    strVec::iterator where;
00101    int i;
00102    const char **pStr = stringz;
00103 
00104    while (*pStr != 0) {
00105       v.push_back( *pStr );
00106       pStr++;
00107    }
00108 
00109    size_t len;
00110    len = v.size();
00111    printf("before erase v.size() = %d\n", len );
00112    for (i = 0; i < len; i++) {
00113       printf("v[%2d] = %s\n", i, v[i] );
00114    }
00115    printf("\n");
00116 
00117    // iterate over v searching for searchStr
00118    where = find( v.begin(), v.end(), searchStr );
00119 
00120    if (where != v.end()) {
00121       v.erase( where );
00122       len = v.size();
00123       printf("after erase v.size() = %d\n", len );
00124       for (i = 0; i < len; i++) {
00125          printf("v[%2d] = %s\n", i, v[i] );
00126       }
00127    }
00128    else {
00129       printf("The search string \"%s\" was not found\n", searchStr );
00130    }
00131 } // test_vector_erase


Generated at Thu Jun 6 15:39:38 2002 for STL vector erase function example code by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001