Main Page   File List   File Members  

erase_demo.cpp

Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include <strings.h>
00004 #include <vector>
00005 
00020 typedef std::vector<const char *> strVec;
00021  
00044 strVec::iterator
00045 find( strVec::iterator first, strVec::iterator last, const char * value)
00046 {
00047    while (first != last && strcmp(*first, value) != 0)
00048    {
00049            ++first;
00050    }
00051    return first;
00052 } // find
00053 
00054 
00085 void test_vector_erase( const char *searchStr )
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
00132                      
00133 
00139 main(int argc, const char *argv[])
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 }
00152 

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