                                                             ++   253

                         6.   

             ,    3,
        vector  string 
       ,    
    ++.       ,  
    4,     ,  
     .

          .
        (vector)   (list).
(      (deque)   
 ,   vector,     
    . deque  , , 
 ,      .   
      deque.)
          
 .       
(map)   (set). map    /,  
   ,     .
     :  
   ,      .
     set   ,  set  
   .    , , 
         - 
,    , ,  , , ,    .
       ,    
 .  ,      .
     map  set       . 
    multimap  multiset. ,
multimap       , 
     .
            
   .

                             6.1.   

         ,  , 
   ,    , ,  .
         ,    . 
   ,   .

                                                             ++   254

   ,       Civil War  Civil
Rights,     9:

   Civil && ( War || Rights )

    :

   Civil: 12 
   War: 48 
   Rights: 1 

   Civil && War: 1 
   Civil && Rights: 1 

   (8) Civility, of course, is not to be confused with
   Civil Rights, nor should it lead to Civil War

    (8)      .    
,   ,      (..
  7      9),   
    .
      :

      ,       ;
        ,  
        ,    , 
         ;
      .     
     :

    &&         
    ||       
    !     
    ()    

     ,  :

   Lincoln

   ,   Lincoln, 

   9 .     ,    
           .  ,  

     (War || Rights)
     Civil&&(War||Rights)

         .     
         ,  ,    
         .

                                                             ++   255

   ! Lincoln

  ,    ,  

   ( Abe || Abraham ) && Lincoln

   ,    Abe Lincoln  Abraham Lincoln.
       .        
    ,    ,   
     .      . (  17 
   ,     
     Query.) .
            (Stan
Lippman)10:

   Alice Emma has long flowing red hair. Her Daddy says when the
   wind blows through her hair, it looks almost alive, like a
   fiery bird in flight. A beautiful fiery bird, he tells her,
   magical but untamed. "Daddy, shush, there is no such thing,"
   she tells him, at the same time wanting him to tell her more.
   Shyly, she asks, "I mean. Daddy, is there?"

                               . 2.

           (
    ,    ,  
,    ,    
    ,  and, a, the):

   alice ((0,0))
   alive ((1,10))
   almost ((1,9))
   ask ((5,2))
   beautiful ((2,7))
   bird ((2,3),(2,9))
   blow ((1,3))
   daddy ((0,8),(3,3),(5,5))
   emma ((0,1))
   fiery ((2,2),(2,8))
   flight ((2,5))
   flowing ((0,4))
   hair ((0,6),(1,6))
   has ((0,2))
   like ((2,0))
   long ((0,3))
   look ((1,8))
   magical ((3,0))
   mean ((5,4))
   more ((4,12))
   red ((0,5))
   same ((4,5))
   say ((0,9))
   she ((4,0),(5,1))
   shush ((3,4))
   shyly ((5,0))
   such ((3,8))
   tell ((2,11),(4,1),(4,10))


  10    (Elena Driskill).

                                                             ++   256

   there ((3,5),(5,7))
   thing ((3,9))
   through ((1,4))
   time ((4,6))
   untamed ((3,2))
   wanting ((4,7))
   wind ((1,2))

       ,     
 (,   ,  ):

   please enter file name: alice_emma

   enter a word against which to search the text.
   to quit, enter a single character ==> alice

   alice occurs 1 time:

      ( line 1 ) Alice Emma has long flowing red hair. Her Daddy says

   enter a word against which to search the text.
   to quit, enter a single character ==> daddy

   daddy occurs 3 times:

   ( line 1 ) Alice Emma has long flow-ing red hair. Her Daddy says
   ( line 4 ) magical but untamed. "Daddy, shush, there is no such thing,"
   ( line 6 ) Shyly, she asks, "I mean, Daddy, is there?"

    enter a word against which to search the text.
    to quit, enter a single character ==> phoenix

   Sorry. There are no entries for phoenix.

   enter a word against which to search the text.
   to quit, enter a single character ==> .
   Ok, bye!

         ,   
     string,    3.

                             6.2.   ?

    ,     ,     
   .      string.
 :           
?
     ,       ,   ,
    .  map   
  .
             
  ,   ..    
   .    :   ?
            ++  ,  ,  ,
       . 
     ,   ,  
  ,      .

                                                                ++   257

         :  vector,  deque
   .       
  ,        
  .
       ,      . 
    ( , ,  5,  15,
 7  ..)    ,    
      .  , 
   ,  :     
  ,    .    
 . ( deque  ,    
       ;  
  ,     
   ,        
.)
        .   
    ,       
.     :   . 
 ,    :   
 ,    .  ,  
 ,         
.
          :

        ,  ;
       ,   ;
             ,
     ;
             
    ,  ,  deque.

    ,       ,  
/ ?  :     
      /.      
 ,     :    ?
(        
 .)         ,
 ,    ,  ,  .
      ,        ( 
 )         ,  
   ?      :  
? (        .)
      :       , 
       ,   
,   .      
   .      ,
     .   
         
  .

                                                                ++   258

            .
     .

                                    6.1

        : ,    ?
       ?
  1.          
     .
  2.    ,     
      .
  3.    .     , 
        .
  4.     .    .

                              6.3.   ?

      .   ?    
,     ,      
    ,     .   
    ,       
    .    
        ,
 ,       .
     .   .
           .   
   ,       
     ,     . (
  ,   .)     
    ,   
.       
 ++   Rogue Wave.     
   .
        ,    
   . (    , 
      ,  vector, deque  string.
  list    .)     
  capacity().      ,
     .      
size(). :

                                                             ++   259

   #include <vector>
   #include <iostream>

   int main()
   {
      vector< int > ivec;
      cout << "ivec: : " << ivec.size()
              << " : " << ivec.capacity() << endl;

      for ( int ix = 0; -ix < 24; ++ix ) {
         ivec.push_back( ix );
         cout << "ivec: : " << ivec.size()
                << " : " << ivec.capacity() << endl;
      }
   }

     Rogue Wave  ,   ivec     0.
       1,    256. 
,        ivec   256
.   256-    : 
       ,     
   .  :      
,        .   6.1 
       .

 6.1.       

	  				 
					 	 

	int 				4 		256
	double 			8 		128
	  #1 		12 		85
	string 			12 		85
	   	8000 		1
	   	8000 		1

   ,   Rogue Wave        1024
.       .  
,   ,  ,     
   ,   . (  
,     ,     
.)   6.2    ,   
         .  6.3 
,    10 000  (   
  ).

 6.2.      10 000 000 

	  		List 		Vector

	int 			10.38 	3.76

                                                          ++   260

	double 		10.72 	3.95
	  	12.31 	5.89
	string 		14.42 	11.80

 6.3.      10 000 

	  			List 		Vector

	   	0.36 		2.23
	   	2.37 		6.70

    ,         , 
,  .       
    .       ,
  .      .
?
       ,    ,   
,   . (   
  .   2.2   ,   
14.5     ).     
        .  
    (    
 ),          
.
            . 
,         (
    2.2).     ,
         
.
   ,          , 
    . ,  
   ,        ,  
 .        10 000   6.70
  0.82 . ?    1  256,   
  .  ,    
         
.
    reserve()      11.
:

   int main() {
      vector< string > svec;
      svec.reserve( 32 ); //    32
      // ...

   11 ,  deque    reserve()

                                                              ++   261


   }

   svec   32   0.   ,  
    ,        1,
   . ,    string  double
    reserve()   .   ,
        
,     6.4.

	 6.4.      10 000   
	*

	 		  
	1   	670
	4,096 		555
	8,192 		444
	10,000 		222
	*   8000  
	   

            string  
 ,      .   , 
     ,   .   
  , ,     .

                                   6.2

         .   
  ,      ,  
  ?

                                   6.3

              
,         ?

                                   6.4

   ,            
  (       
):.

   (a)  
   (b)     
   (c)   

                    6.4.    ?

         ,   
  :


                                                              ++   262

   #include <vector>
   #inclnde <list>
   #include <deque>
   #include <map>
   #include <set>

        ,     
    12. :

   vector< string > svec;
   list< int > ilist;

    svec   ,     string,
 ilist       int.     
.    ,   - empty():

   if ( svec.empty() != true )
      ; // -  

         - push_back(),
     . :

   string text_word;
   while ( cin >> text_word )
      svec.push_back( text_word );

           text_word,  
      svec   push_back().
     - push_front(),      .
   :

   int ia[ 4 ] = { 0, 1, 2, 3 };

   12         
        .    allocator   
         .  ++     ,  
        .     new  delete.
           :  
          ,    ,  
             .
         ,     
       ,   :

     vector< string, allocator > svec;
     list< int, allocator > ilist;

                                                                 ++   263

    push_back()

   ilist.push_back( ia[ ix ] );
   ilist.push_back( ia[ ix ] );

  0, 1, 2, 3,  push_front()

   for ( int ix=0; ix<4; ++ix )
     ilist.push_front( ia[ ix ] );

  3, 2, 1, 0. 13
             ,  
 :

   #include <list>
   #include <vector>
   #include <string>

   extern int get_word_count( string file_name );
   const int list_size = 64;
   list< int > ilist( list_size );
   vector< string > svec(get_word_count(string("Chimera")));

        ,
  .  int  0.    
    string.
         :

   list< int > ilist( list_size, -1 );
   vector< string > svec( 24, "pooh" );

         ,   
    - resize(). :

   svec.resize( 2 * svec.size() );

    svec    .      
.      -  ,  
   - resize():

   13  - push_front()  ,   
      deque,   vector:  deque     .

                                                              ++   264
   //      "piglet"
   svec.resize( 2 * svec.size(), "piglet" );

   ,     svec  ,   
  24? , 24!        
 .    ,  ,  
          . :

   vector< string > svec2( svec );
   list< int > ilist2( ilist ) ;

         : ,
, , ,   ,   . 
   .       , 
  ;      .   
       .  
 ,   :

   ivecl: 1 3 5 7 9 12
   ivec2: 0 1 1 2 3 5 8 13
   ivec3: 1 3 9
   ivec4: 1 3 5 7
   ivec5: 2 4

   //   : 1, 
   // ivecl   ivec2
   ivecl < ivec2 //false
   ivec2 < ivecl //true

   //   : 5, 9
   ivecl < ivec3 //true

   //   ,  ivec4   
   // , ivec4 ,  ivecl
   ivecl < ivec4 //false

   //   : 1, 2
   ivecl < ivec5 //true
   ivecl == ivecl //true
   ivecl == ivec4 //false
   ivecl != ivec4 //true

   ivecl > ivec2 //true
   ivec3 > ivecl //true
   ivec5 > ivec2 //true

          (  
  ).    :

    ;
     (   ,  
     ,      );
      (      
    ).

                                                               ++   265

      ,      
 ++   .

                                   6.5

   ,    :

   #include <string>
   #include <vector>
   #include <iostream>

   int main()
   {
      vector<string> svec;
      svec.reserve( 1024 );

      string text_word;
      while ( cin >> text_word )
         svec.push_back( text_word );

      svec.resize( svec.size()+svec.size()/2 );
      // ...

   }

                                        6.6

          ?  ,  
  :     ? ?

                                        6.7

       6.5  256 ,    
    ?     512 ? 1000? 1048?

                                       6.8

           :

                                                               ++   266

  (a) class cl1 {
      public:
         c11( int=0 );
         bool operator==();
         bool operator!=();
         bool operator<=();
         bool operator<();
         // ...
     };

  (b) class c12 {
      public:
         c12( int=0 );
         bool operator!=();
         bool operator<=();
         // ...
     };

  () class c13 {
      public:
         int ival;
      };

  (d) class c14 {
      public:
         c14( int, int=0 );
         bool operator==();
         bool operator!=();
         // ...
     }

                                     6.5. 

           
 ,   .  iter   
- . 

   ++iter;

  ,       , 

   *iter;

 ,  ,    .
      - begin()  end().

   begin()  ,     .
   end()  ,   ,  
      .


    ,  :

   for ( iter = container. begin();
          iter != container.end(); ++iter )

                                                            ++   267

   do_something_with_element( *iter );

       .    
  string:

   // vector<string> vec;
   vector<string>::iterator iter = vec.begin();
   vector<string>::iterator iter_end = vec.end();

     vector   iterator  typedef. 

   vector<string>::iterator

  iterator,    typedef   vector,
   string.
         ,  :

   for( ; iter != iter_end; ++iter )
      cout << *iter << '\n';

     *iter  , ,  .
       iterator      const_iterator,
     ,   const.
const_iterator     :

   #include <vector>
   void even_odd( const vector<int> *pvec,
      vector<int> *pvec_even,
      vector<int> *pvec_odd )
   {
      // const_iterator     pvec
      vector<int>::const_iterator c_iter = pvec->begin();
      vector<int>::const_1terator c_iter_end = pvec->end();

      for ( ; c_iter != c_iter_end; ++c_iter )
         if ( *c_iter % 2 )
            pvec_even->push_back( *c_iter );
         else pvec_odd->push_back( *c_iter );
   }

    ,       , 
     ,     ? 
  ,  ,      
:

   vector<int>::iterator iter = vec->begin()+vec.size()/2;

iter       ,  

                                                             ++   268

   iter += 2;

 iter   .
           vector 
deque. list    ,    
    .    
:

   ilist.begin() + 2;

            ,
    next.   vector  deque   
   2     . (
    3.3.)
          ,
        
. (     ,   
.) ,  :

   #include <vector>
   #include <string>
   #include <iostream>

   int main()
   {
      vector<string> svec;
      string intext;
      while ( cin >> intext )
         svec.push_back( intext );

         //  svec ...

   }

        ,    
:

   int main() {
      vector<string> svec;
      // ...
      //  svec2   svec
      vector<string> svec2( svec.begin(), svec.end() );
      //  svec3   svec
      vector<string>::iterator it =
      svec.begin() + svec.size()/2;
      vector<string> svec3 ( svec.begin(), it );
      // ...

   }

                                                              ++   269

      istream_iterator (    
12.4.3)        svec:

   #include <vector>
   #include <string>
   #include <iterator>

   int mainQ
   {
      //  istream_iterator   
      istream_iterator<string> infile( cin );

      // istream_iterator,   
      istream_iterator<string> eos;

      //  svec ,   cin;
      vector<string> svec( infile, eos );

      // ...

   }

    ,    ,  ,
       .   
 :

   #include <string>

   string words[4] = {
         "stately", "plump", "buck", "mulligan"

   };

             
  ,   :

   vector< string > vwords( words, words+4 );

      : ,    ,  .
      :

   int ia[6] = { 0, 1, 2, 3, 4, 5 };
   list< int > ilist( ia, ia+6 );

     12.4          . 
   ,      
 .      ,  
 ,  .

                                      6.9

        :

                                                            ++   270

   const vector< int > ivec;
   vector< string > svec;
   list< int > ilist;

  (a) vector<int>::iterator it = ivec.begin();
  (b) list<int>::iterator it = ilist.begin()+2;
  (c) vector<string>::iterator it = &svec[0];
  (d) for ( vector<string>::iterator
            it = svec.begin(); it != 0; ++it )
      // ...

                                    6.10

       :

   int ia[7] = { 0, 1, 1, 2, 3, 5, 8 };
   string sa[6] = {
      "Fort Sumter", "Manassas", "Perryville", "Vicksburg",
      "Meridian", "Chancellorsvine" };

  (a) vector<string> svec( sa, &sa[6] );
  (b) list<int> ilist( ia+4, ia+6 );
  (c) list<int> ilist2( ilist.begin(), ilist.begin()+2 );
  (d) vector<int> ivec( &ia[0], ia+8 );
  (e) list<string> slist( sa+6, sa );
  (f) vector<string> svec2( sa, sa+6 );

                  6.6.    

   - push_back()      
.       ?  
 ?       .
,        :

   vector< string > svec;
   list< string > slist;
   string spouse( "Beth" );
   slist.insert( slist.begin(), spouse );
   svec.insert( svec.begin(), spouse );

     - insert() (,   
)  ,        . 
      .    
   :

                                                               ++   271

   string son( "Danny" );
   list<string>::iterator iter;
   iter = find( slist.begin(), slist.end(), son );
   slist.insert( iter, spouse );

    find()     ,   , 
 end(),    . (    find()  
 .)   , push_back()  
:

   //  : slist.push_back( value );
    slist.insert( slist.end(), value );

     - insert()    
 ,    . ,   
   Anna   ,   :

   vector<string> svec;
   string anna( "Anna" );
   svec.insert( svec.begin(), 10, anna );

   insert()    ,     
. ,   :

   string sarray[4] = { "quasi", "simba", "frollo", "scar" };

               
:

   svec.insert( svec.begin(), sarray, sarray+4 );
   svec.insert( svec.begin() + svec.size()/2,
                sarray+2, sarray+4 );

          

   svec.begin(), svec.end() );

                                                              ++   272

  ,  :14

   list< string > slist;

   // ...

   //   svec
   //  ,  stringVal
   list< string >::iterator iter =
   find( slist.begin(), slist.end(), stringVal );
   slist.insert( iter, svec.begin(), svec.end() );

                              6.6.1. 

          - erase().
    ,   ,  
.      -
pop_back().
     erase()   ,    .
        find() 
  ,   ,    - erase().

   string searchValue( "Quasimodo" );
   list< string >::iterator iter =
   find( slist.begin(), slist.end(), searchValue );
   if ( iter != slist.end() )
      slist.erase( iter );

            
:

   //    
   slist.erase( slist.begin(), slist.end() );
   //  ,  
   list< string >::iterator first, last;
   first = find( slist. begin(), slist.end(), vail );
   last = find( slist.begin(), slist.end(), va12 );
   // ...  first  last
   slist.erase( first, last );

  14   insert() ,     
     -.         
     ++,       ,      
     ,    .

                                                               ++   273

       push_back()  - pop_back(), 
   ,    :

   vector< string >::iterator iter = buffer.begin();
   for ( ; iter != buffer.end(), iter++ )
   {
      slist.push_back( *iter );
      if ( ! do_something( slist ))
         slist.pop_back();

   }

                             6.6.2.   

    ,      ?  
   ,  ,  ,    
.       ? :

   // svecl  10 
   // svec2  24 
   //      24 
   svecl = svec2;

   - (svec1)     ,  -
 (svec2). 10 ,    svec1,  (
      string).
  swap()      
.   :

   svecl.swap( svec2 );

svec1     24 ,      
:

   svecl = svec2;

   svec2  10 ,    svec1. 
  .

                           6.6.3.  

   ,    ,  , 
  vector  deque. ,    
        find(), sort(), merge()  ..
         
,       ,   
  . (     12  
.)        

                                                             ++   274

        .  
   find()  ,     :

   #include <list>
   #include <vector>

   int ia[ 6 ] = { 0, 1, 2, 3, 4, 5 };
   vector<string> svec;
   list<double> dtist;

   //   
   #include <algorithm>
   vector<string>::iterator viter;
   list<double>::iterator liter;
   #int *pia;

   // find()     
   //     ...
   pia = find( &ia[0], &ia[6], some_int_value );
   liter = find( dlist.begin(), dlist.end(), some_double_value );
   viter = find( svec.begin(), svec.end(), some_string_value );

    list   , ,  sort()  merge(),
        . ( 
   12.6.)
        .

                                        6.11

 ,     :

   int ia[] = { 1, 5, 34 };
   int ia2[] = { 1, 2, 3 };
   int ia3[] = { 6, 13, 21, 29, 38, 55, 67, 89 };
   vector<int> ivec;

          ia, ia2  ia3,
  ivec ,    :

   { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 }

                                           6.12

    ,   :

   list<int> ilist( ia, ia+11 );

    - erase()   ,   ilist  
.

                                                                   ++   275

                            6.7.   

         ,     .
    :  ,     
,   .
       ?     
 getline():

   istream&
   getline( istream &is, string str, char delimiter );

getline()     ,  ,    
  string,        delimiter,   
         ,
 - max_size() string.
          .
     ,  ,  ,  retrieve_text().  
 pair        . (
     6.14.)
       :15

  15   ,   
        .      :

     vector<string,allocator> *lines_of_text;

      ,    ++, 
       :

     vector<string> *lines_of_text;

                                                              ++   276

   //   -    
   vector<string,allocator>*
   retrieve_text()
   {
      string file_name;

      cout << "please enter file name: ";
      cin >> file_name;

      //     ...
      ifstream 1nfile( file_name.c_str(), ios::in );
      if ( ! infile ) {
         cerr << "oops! unable to open file "
              << file_name << " -- bailing out!\n";
         exit( -1 );
      }
      else cout << '\n';

      vector<string, allocator> *1ines_of_text =
         new vector<string, allocator>;
      string textime;

      typedef pair<string::size_type, int> stats;
      stats maxline;
      int linenum = 0;

      while ( getline( infile, textline, '\n' )) {
         cout << "line read: " << textline << '\n';
         if ( maxline.first < textline.size() ) {
            maxline.first = textline.size() ;
            maxline.second = linenum;
         }

         1ines_of_text->push_back( textline );
         linenum++;
      }

      return lines_of_text;

   }

        (   , 
     ,      ,
,    ):

   please enter file name: a1ice_emma

   line read: Alice Emma has long flowing red hair. Her Daddy says
   line read: when the wind blows through her hair, it looks
      almost alive,
   line read: like a fiery bird in flight. A beautiful fiery bird,
      he tells her,
   line read: magical but untamed. "Daddy, shush, there is no such
      thing, "
   line read: she tells him, at the same time wanting him to tell
      her more.
   line read: Shyly, she asks, "I mean. Daddy, is there?"

   number of lines: 6
   maximum length: 66
   longest line: like a fiery bird in flight. A beautiful fiery
      bird, he tells her,

                                                              ++   277

         ,     .  
  . ,     Anna Livia Plurrabelle
 Finnegans Wake.

   "For every tale there's a telling,
   and that's the he and she of it."

         :

   "For
   there's
   telling,
   that's
   it."

      :

   For
   there
   telling
   that
   it

    , 

   there's

  

   there is

 -    :     
  , ,  is, that, and, it  ..    
  Finnegans Wake     : tale  telling, 
      . (   - 
   set,     
.)
             
,       , , :

   Home is where the heart is.
   A home is where they have to let you in.

   ,   home    .

                                                              ++   278

          : 
 ,   dog  dogs, love, loving  loved  
 .
            string 
    ,   , 
     .

                           6.8.    

          .    ,
       find(). ,  

   Alice Emma has long flowing red hair.

  , ,     .
    string    . find()     . 
 ,   ,       
,   ,    string::npos   .
:

   #include <string>
   #include <iostream>

   int main() {
      string name( "AnnaBelle" );
      int pos = name.find( "Anna" );
      if ( pos == string::npos )
         cout << "Anna  !\n";
      else cout << "Anna   : " << pos << endl;

   }

          int,    
  ,  find(), :

   string::size_type

   :

   string::size_type pos = name.find( "Anna" );

    find()    ,   .  
  find_first_of(),    
,      -.   
 ,  :

                                                            ++   279

   #include <string>
   #include <iostream>

   int main() {
      string numerics( "0123456789" );
      string name( "r2d2" );

      string:: size_type pos = name.find_first_of( numerics );
      cout << "   : "
           << pos << "\t  "
           << name[pos] << endl;

   }

      pos   1 (,     
0).
         ,    .  
   find_first_of()  , 
,    .   .    ,
       ?

   #include <string>
   #include <iostream>

   int main() {
      string numerics( "0123456789" );
      string name( "r2d2" );

      string::size_type pos = 0;
      // -  !
      while (( pos = name.find_first_of( numerics, pos ))
               != string::npos )
         cout << "   : "
              << pos << "\t  "
              << name[pos] << endl;

   }

      pos  0,      .  
   1.       string::npos,
  .    find_first_of() pos
 1.    1- .  !  find_first_of() 
    ,  ,  ...   . 
  pos  1    :

                                                               ++   280

   //   
   while (( pos = name.find_first_of( numerics, pos ))
              != string::npos )
   {
      cout << "   : "
         << pos << "\t  "
         << name[pos] << endl;

      //   1 
      ++pos;

   }

        ( ,  ,  
   ),    numerics    ,
   .    ,    
   ,         :

   //  
   while (( pos = textline.find_first_of( ' ', pos ))
            != string::npos )
      // ...

      ,    :

   //  
   // pos:   1   
   // prev_pos:   

   string::size_type pos = 0, prev_pos = 0;

   while (( pos = textline.find_first_of( ' ', pos ))
            != string::npos )
   {
      // ...
      //    
      prev_pos = ++pos;

   }

      prev_pos    ,  pos  
    . ,   :

   pos - prev_pos; //  

        ,      . 
 ,      textline   prev_pos  pos -1.
 substr()    :

                                                               ++   281

   //  
   vector<string> words;

   while (( pos = textline.find_first_of( ' ', pos ))
            != string::npos )
   {
      words.push_back( textline.substr(
      prev_pos, pos-prev_pos));
      prev_pos = ++pos;

   }

    substr()   .     
,    . (   ,  
     ,    .)
    :       .
?  :

   seaspawn and seawrack

       .   
find_first_of()    .    
string::npos,   .  ,   
.
      ,   separate_words().  
   ,          
(    ).

                                                               ++   282

   typedef pair<short,short> location;
   typedef vector<location> loc;
   typedef vector<string> text;
   typedef pair<text* ,loc*> text_loc;

   text_loc*
   separate_words( const vector<string> *text_file )
   {
      // words:   
      // locations:      
      //  
      vector<string> *words = new vector<string>;
      vector<location> * locations = new vector<location>;

      short line_pos = 0; //   
      // iterate through each line of text
      for ( ; line_pos < text_file->size(); ++line_pos )
         // textline:  
         // word_pos:   
         short word_pos = 0;
         string textline = (*text_file) [ line_pos ];
         string::size_type pos = 0, prev_pos = 0;
         while (( pos = textline.find_first_of( ' ', pos ))
                  != string::npos )
         {
            //  
            words->push_back(
            textline.substr( prev_pos, pos - prev_pos ));
            //       

            locations->push_back(
            make_pair( line_pos, word_pos ));

            //     
            ++word_pos; prev_pos = ++pos;
         }

         //   
         words->push_back(
                  textline.substr( prev_pos, pos - prev_pos ));

         locations->push_back(
                  make_pair( line_pos, word_pos ));
      }
      return new text_loc( words, locations );

   }

  main()  :

   int main()
   {
      vector<string> *text_file = retrieve_text();
      text_loc *text_locations = separate_words( text_file );
      // ...
   }


                                                      ++   283

     ,    separate_words():

textline: Alice Emma has long flowing red hair. Her Daddy
says

   eol: 52 pos: 5 line: 0 word: 0 substring: Alice
   eol: 52 pos: 10 line: 0 word: 1 substring: Emma
   eol: 52 pos: 14 line: 0 word: 2 substring: has
   eol: 52 pos: 19 line: 0 word: 3 substring: long
   eol: 52 pos: 27 line: 0 word: 4 substring: flowing
   eol: 52 pos: 31 line: 0 word: 5 substring: red
   eol: 52 pos: 37 line: 0 word: 6 substring: hair.
   eol: 52 pos: 41 line: 0 word: 7 substring: Her
   eol: 52 pos: 47 line: 0 word: 8 substring: Daddy
      last word on line substring: says

   ...

   textline: magical but untamed. "Daddy, shush, there is no
   such thing,"

   eol: 60 pos: 7 line: 3 word: 0 substring: magical
   eol: 60 pos: 11 line: 3 word: 1 substring: but
   eol: 60 pos: 20 line: 3 word: 2 substring: untamed
   eol: 60 pos: 28 line: 3 word: 3 substring: "Daddy,
   eol: 60 pos: 35 line: 3 word: 4 substring: shush,
   eol: 60 pos: 41 line: 3 word: 5 substring: there
   eol: 60 pos: 44 line: 3 word: 6 substring: is
   eol: 60 pos: 47 line: 3 word: 7 substring: no
   eol: 60 pos: 52 line: 3 word: 8 substring: such
      last word on line substring: thing,":

   ...

   textline: Shy1y, she asks, "I mean, Daddy: is there?"
   eol: 43 pos: 6 line: 5 word: 0 substring: Shyly,
   eol: 43 pos: 10 line: 5 word: 1 substring: she
   eol: 43 pos: 16 line: 5 word: 2 substring: asks,
   eol: 43 pos: 19 line: 5 word: 3 substring: "I
   eol: 43 pos: 25 line: 5 word: 4 substring: mean,
   eol: 43 pos: 32 line: 5 word: 5 substring: Daddy,
   eol: 43 pos: 35 line: 5 word: 6 substring: is
      last word on line substring: there?":

        ,  
 -  string,   . 
rfind()  , ..  ,   :

   string river( "Mississippi" );
   string::size_type first_pos = river.find( "is" );
   string::size_type 1ast_pos = river.rfind( "is" );

   find()  1,      "is",  rfind()  4
(   "is").
find_first_not_of()   ,    ,  
. ,    ,   , 
:

                                                               ++   284

   string elems( "0123456789" );
   string dept_code( "03714p3" );
   //    'p'
   string::size_type pos = dept_code.find_first_not_of(elems) ;

   find_last_of()       .
   find_last_not_of()   ,       . 
             , 
  .

                                 6.13

 ,    

    "ab2c3d7R4E6"

,   ,   find_first_of(),  

    find_first_not_of().

                                    6.14

    ,         
      sentence:

   string linel = "We were her pride of 10 she named us --";
   string line2 = "Benjamin, Phoenix, the Prodigal"
   string line3 = "and perspicacious pacific Suzanne";
   string sentence = linel + line2 + line3;

       ,    ,  
.

                           6.9.   

           ,    
.   

   magical but untamed. "Daddy, shush, there is no such thing,"

     :

   magical
   but
   untamed.
   "Daddy,
   shush,
   there
   is
   no

                                                                ++   285

   such
   thing,"

         ?    ,
  ,    :

   string filt_elems( "\",.;:!?)(\\/" );

(     ,        
  ,     . , \" 
  ,    ,  \\     .)
      - find_first_of()    
 :

   while (( pos = word.find_first_of( filt_elems, pos ))
            != string::npos )

        - erase():

   word.erase(pos,1);

         ,     . 
  ,    pos.   
;   ,      pos   .
       filter_text().    :  
 ,  ,    ,   .

   void
   filter_text( vector<string> *words, string filter )
   {
      vector<string>::iterator iter = words->begin();
      vector<string>::iterator iter_end = words->end();

      //  filter  ,   
      if ( ! filter.size() )
         filter.insert( 0, "\".," );

      while ( iter != iter_end ) {
         string::size_type pos = 0;

      //    
      while (( pos = (*iter).find_first_of( filter, pos ))
               != string::npos )
         (*iter).erase(pos,1);
         iter++;
      }
   }

        pos   ?   ,   
:

                                                               ++   286

   while (( pos = (*iter).find_first_of( filter, pos ))
            != string::npos )
   {
      (*iter).erase(pos,1);
      ++ pos; // ...
   }

    

   thing,"

      pos   5 , .. ,    .
     

   thing"

     5-    .     pos, 
  .
        filter_text():

   filter_text( text_locations->first, filt_elems );

      ,    filter_text():

   filter_text: untamed.
   found! : pos: 7.
   after: untamed

   filter_text: "Daddy,
   found! : pos: 0.
   after: Daddy,
   found! : pos: 5.
   after: Daddy

   filter_text: thing,"
   found! : pos: 5.
   after: thing"
   found! : pos: 5.
   after: thing

   filter_text: "I
   found! : pos: 0.
   after: I

   filter_text: Daddy,
   found! : pos: 5.
   after: Daddy

   filter_text: there?"
   found! : pos: 5.
   after: there"
   found! : pos: 5.
   after: there

                                                          ++   287

                                 6.15

    ,    ,  STL  :

  "/.+(STL).$1/"

  erase(pos,count),   erase(iter,iter).

                                 6.16

    ,        

   string sentence( "kind of" );
   string s1 ( "whistle" )
   string s2 ( "pixie" )

 

   "A whistling-dixie kind of walk"

                         6.10.     

            
    , ,  cry, cries  cried, baby 
babies, ,   ,     , 
home  Home.  ,  ,  ,  
     .     
:

   void
   strip_caps( vector<string,allocator> *words )
   {
      vector<string,allocator>::iterator iter=words->begin() ;
      vector<string,allocator>::iterator iter_end=words->end() ;
      string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
      while ( iter != iter_end ) {
         string::size_type pos = 0;
         while (( pos = (*iter).find_first_of( caps, pos ))
                  != string::npos )
            (*iter)[ pos ] = to1ower( (*iter)[pos] );
         ++iter;
      }
   }

   

   to1ower( (*iter)[pos] );

                                                            ++   288

    .      
.   tolower()    :

   #include <ctype.h>

(      , ,  isalpha(), isdigit(),
ispunct(), isspace(), toupper().     . [PLAUGER92].
  ++   ctype,   
   ,    ,  
,  toupper(), tolower()  ..     
 

   #include <locale>

           ctype,  
    .)
        ,       .
         
 . ,      ,    's' 
 :

   void suffix_text( vector<string,allocator> *words )
   {
      vector<string,allocator>::iterator
      iter = words->begin(),
      iter_end = words->end();
      while ( iter != iter_end ) {
         //       
         if ( (*iter).size() <= 3 )
         { ++iter; continue; }
         if ( (*iter)[ (*iter).size()-1 ] == 's' )
            suffix_s( *iter );
         //      
         // ed, ing, 1y
         ++iter;
      }
   }

          .     ,
, has, its, is  ..,   tv  tvs     
.
       "ies",  babies  cries,   "ies"  "y":

   string::size_type pos() = word.size()-3;
   string ies( "ies" );
   if ( ! word.compare( pos3, 3, ies )) {
      word.replace( pos3, 3, 1, '' );
      return;

                                                                 ++   289

   }

compare()  0,    .  , pos3, 
 ,      (   3). 
, ies,  -. (      
compare().      .)
   replace()    .     
 "ies"   3    'y'. ( 
   replace().     
 .)
       "ses",  promises  purposes,  
 "es"16:

   string ses( "ses" );
   if ( ! word.compare( pos3, 3, ses )) {
      word.erase( pos3+l, 2 );
      return;

   }

       "ous",  oblivious, fulvous, cretaceous,   "is", 
genesis, mimesis, hepatitis,     . (  .
,   kiwis    's'.)   ,
  "ius" (genius)   "ss" (hiss, lateness, less).  
   compare():

   string::size_type spos = 0;
   string::size_type pos3 = word.size()-3;
   // "ous", "ss", "is", "ius"
   string suffixes( "oussisius" );
   if ( ! word.compare( pos3, 3, suffixes, spos, 3 ) || // ous
      ! word.compare( pos3, 3, suffixes, spos+6, 3 ) || // ius
      ! word.compare( pos3+l, 2, suffixes, spos+2, 2 ) || // ss
      ! word.compare( pos3+l, 2, suffixes, spos+4, 2 ) ) // is
   return;

        's':

//   's'
word.erase( pos3+2 );

   16 ,       .  
        crises (.   crisis  . .)  cris. !

                                                                   ++   290

    ,  Pythagoras, Brahms, Burne-Jones,   
 .        ,  
   .
        ,    .

                                         6.17

         ed (surprised), ly (surprisingly) 
ing (surprisingly).       :

   (a) suffix_ed() (b) suffix_ly() (c) suffix_ing()


                             6.11.    

     - erase()      ,
  . , 

   string name( "AnnaLiviaPlurabelle" );

  "Annabelle":

   typedef string::size_type size_type;
   size_type startPos = name.find( 'L' )
      size_type endPos = name.find_1ast_of( 'b' );
   name.erase( name.begin()+startPos,
      name.begin()+endPos );

   ,     ,     .
          ;    
,       . :

   name.erase( name. begin()+4 );

  "Anna".
   - insert()        
 .    :

   string_object.insert( position, new_string );

position  ,    . new_string 
   string, C-  :

   string string_object( "Missisippi" );
   string::size_type pos = string_object.find( "isi" );
   string_object.insert( pos+1, 's' );

                                                              ++   291

         new_string:

   string new_string ( "AnnaBelle Lee" );
   string_object += ' '; //  
   //       new_string
   pos = new_string.find( 'B' );
   string::size_type posEnd = new_string.find( ' ' );
   string_object.insert(
      string_object.size(), //  
      new_string, pos, //    new_string
      posEnd //   new_string
   )

string_object   "Mississippi Belle".     
 new_string,   pos,    .
      :

   string sl( "Mississippi" );
   string s2( "Annabelle" );

         "Miss Anna"?
  - assign()  append():

   string s3;
   //   4  s1
   s3.assign ( s1, 4 );

s3    "Miss".

   //  
   s3 += ' ';

    s3  "Miss ".

   s3.append(s2,4);

s3   "Miss Anna".      :

   s3.assign(s1,4).append(' ').append(s2,4);

                                                             ++   292

     - assign()   :  
 ,    .    0.  , , 
"belle"  "Annabelle":

   string beauty;
   //  beauty  "belle"
   beauty.assign( s2, 4, 5 );

          :

   //  beauty  "belle"
   beauty.assign( s2, s2.begin()+4, s2.end() );

             ,
   .    
,      ,   . :

   string current_project( "C++ Primer, 3rd Edition" );
   string pending_project( "Fantasia 2000, Firebird segment" );

   - swap()        

   current_project.swap( pending_project );

    

   string first_novel( "V" );

  

   char ch = first_novel[ 1 ];

  :   first_novel  1,  
    0.      
  ,         
- size():

                                                               ++   293

   int
   elem_count( const string &word, char elem )
   {
      int occurs = 0;
      //     ix
      for ( int ix=0; ix < word.size(); ++-ix )
         if ( word[ ix ] == elem )
            ++occurs;
      return occurs;
   }

   ,     , :

   void
   mumble( const string &st, int index )
   {
      //  
      char ch = st[ index ];
      // ...
   }

   at(),    ,    
,   .     ,  
out_of_range:

   void
   mumble( const string &st, int index )
   {
      try {
         char ch = st.at( index );
         // ...
      }
      catch ( std::out_of_range ){...}
         // ...
   }

      . :

   string cobol_program_crash( "abend" );
   string cplus_program_crash( "abort" );

    cobol_program_crash  ,  cplus_program_crash:
     ,   e  
  ,  o.    -
compare(). 

   sl.compare( s2 );

                                                             ++   294

    :

    s1 ,  s2,  ;
    s1 ,  s2,  ;
    s1  s2,  0.

   ,

   cobol_program_crash.compare( cplus_program_crash );

  , 

   cplus_program_crash.compare( cobol_program_crash );

.    (<, >, !=, ==, <=, >=)  
   compare().
     - compare()   
     . (    
.)
   - replace()         (
   ).     replace()  
   :        
,            .   
:

   string sentence(
      "An ADT provides both interface and implementation." );
      string::size_type position = sentence.find_1ast_of( 'A' );
      string::size_type length = 3;
      //  ADT  Abstract Data Type
      sentence.repiace( position, length, "Abstract Data Type" );

position    ,  length   
.     .   
 . ,   string:

   string new_str( "Abstract Data Type" );
   sentence.replace( position, length, new_str );

         new_str:

                                                             ++   295

   #include <string>
   typedef string::size_type size_type;
   //    
   size_type posA = new_str.find( 'A' );
   size_type posD = new_str.find( 'D' );
   size_type posT = new_str.find( 'T' );
   // :  T  "Type"
   sentence.replace( position+2, 1, new_str, posT, 4 );
   // :  D  "Data "
   sentence.replace( position+1, 1, new_str, posD, 5 );
   // :  A  "Abstract "
   sentence.replace( position, 1, new_str, posA, 9 );

           ,  
 :

   string hmm( "Some celebrate Java as the successor to C++." );
   string:: size_type position = hmm.find( 'J' );
   //  Java  xxxx
   hmm.repiace( position, 4, 'x', 4 );

             
:

   const char *lang = "EiffelAda95JavaModula3";
   int index[] = { 0, 6, 11, 15, 22 };
   string ahhem(
      "C++ is the language for today's power programmers." );
   ahhem.replace(0, 3, lang+index[1], index[2]-index[1]);

        :

   string sentence(
      "An ADT provides both interface and implementation." );
   //   'A'  ADT
   string: iterator start = sentence. begin()+3;
   //  ADT  Abstract Data Type
   sentence.repiace( start, start+3, "Abstract Data Type" );

            
string, ,  N ,    C-.
     ,        .   
     ++ [ISO-C++97].

                                                                 ++   296

                           6.18

    ,    - assign()  append() 


   string quote1( "When lilacs last in the dooryard bloom'd" );
   string quote2( "The child "is father of the man" );

 

   "The child is in the dooryard"

                                   6.19

    :

   string generate_salutation( string generic1,
      string lastname,
      string generic2,
      string::size_type pos,
      int length );

  

   string generic1( "Dear Ms Daisy:" );

 Daisy  Ms ().  Daisy   lastname,  
Ms 

   string generic2( "MrsMsMissPeople" );

 length,   pos.
   , 

   string lastName( "AnnaP" );
   string greetings =
      generate_salutation( generici, lastName, generic2, 5, 4 );

 :

   Dear Miss AnnaP:

                                                               ++   297

                     6.12.    

         (map),    
      ,    . (
      set.)   
 set ,    ,     
 ,  map       - .
    map   /.       
   .       
  ,    ,   ( ,
 ).      . :

   string query( "pickle" );
   vector< location > *locat;
   //  location<vector>*,   "pickle"
   locat = text_map[ query ];

      ,     location<vector>*.
         
:

   #include <map>

         ? 
       .  
  ,    /     
 .   ,       
 ,   .

               6.12.1.   map   
                              

       map,   ,  ,   
. :

   map<string,int> word_count;

      word_count  map,      
string,         int. 

   map<int,employee*> personnel;

 personnel     int (  )
 ,    employee.

                                                              ++   298

         :

   typedef pair<short,short> location;
   typedef vector<location> loc;
   map<string,loc*> text_map;

            
   ,     
:

   map<string,loc*, // , 
      less<string>, //  
      allocator> //    
   text_map;

          
 .        (.  12.3
 -).
       ,    
/.     :

   #include <map>
   #include <string>
   map<string,int> word_count;
   word_count[ string("Anna") ] = 1;
   word_count[ string("Danny") ] = 1;
   word_count[ string("Beth") ] = 1;
      //    ...

     :

   word_count[ string("Anna") ] = 1;

    :

  1.     string   "Anna" 
        ,    map.
  2.      "Anna"   word_count. 
      .
  3.  word_count    /.  , ,
      "Anna".   0,   1.
  4.      1.

                                                                ++   299

            
,           
.      0.
   ,      
,        ,    
  .     ,  
        
,    ,   .
       :

   //     
   word_count.insert(
      map<string,i nt>::
      value_type( string("Anna"), 1 )
   );

     map   value_type      
/. 

   map< string,int >::
   value_type( string("Anna"), 1 )

  pair,      map.  
   typedef:

   typedef map<string,int>::value_type valType;

       :

   word_count.insert( valType( string("Anna"), 1 ));

        ,    insert(),
     . :

   map< string, int > word_count;
   // ... 
   map< string,int > word_count_two;
   //    /
   word_count_two.insert(word_count.begin(),word_count.end());

         ,    
:

                                                                ++   300

   //     /
   map< string, int > word_count_two( word_count );

   ,        . 
separate_words(),    6.8,   :  ,
   ,   ,   ( , 
)   .  ,     
   ,       
.
   separate_words()        pair, 
  .      build_word_map(), 
        :

   // typedef   
   typedef pair< short,short > location;
   typedef vector< location > loc;
   typedef vector< string > text;
   typedef pair< text*,loc* > text_loc;
   extern map< string, loc* >*
   build_word_map( const text_loc *text_locations );

         map    -
  :

   map<string,loc*> *word_map = new map< string, loc* >;
   vector<string> *text_words = text_locations->first;
   vector<location> *text_locs = text_locations->second;

         ,   :

     .    map   /;
     .     , 
      ( ,  ).

     :

                                                                 ++   301

   register int elem_cnt = text_words->size();
   for ( int ix=0; ix < elem_cnt; ++ix )
   {
      string textword = ( *text_words )[ ix ];

      //     
      //     -
      if ( textword.size() < 3 ||
           exclusion_set.count( textword ))
          continue;

         // ,     
         //  count()  0 - :  
         if ( ! word_map->count((*text_words)[-ix] ))
         {
            loc *ploc = new vector<location>;
            ploc->push_back( (*text_locs) [ix] );
            word_map->insert(value_type((*text_words)[ix],ploc));
         }
         else
           //   
           (*word_map)[(*text_words)[ix]]->
              push_back((*text_locs)[ix]);
         }

     

         (*word_map)[(*text_words)[ix]]->
            push_back((*text_locs)[ix]);

     ,      :

   //  ,   
   string word = (*text_words) [ix];
   //     
   vector<location> *ploc = (*word_map) [ word ];
   //   -  
   loc = (*text_locs)[ix];
   //   
   ploc->push_back(loc);

       ,      .
     :

   string word = text_words[ix]; // 

      :

   string word = (*text_words) [ix]; // 

                                                                  ++   302

      build_word_map()   :

   return word_map;

          main():

   int main()
   {
      //     
      vector<string, allocator> *text_file = retrieve_text();
      text_loc *text_locations = separate_words( text_file );
      //  
      // ...
      //      
      map<string,l*,less<string>,allocator>
      *text_map = build_word_map( text_locatons );
      // ...
   }


                    6.12.2.     

          .
:

   // map<string,int> word_count;
   int count = word_count[ "wrinkles" ];

       ,  ,   ,  
    .     
     .     word_count  

   string( "wrinkles" ), 0

    map     ,  ,    
  .

   count(keyValue): - count()    
     . (     0  1).  count()
     1,     :

    int count = 0;
    if ( word_count.count( "wrinkles" ))

                                                               ++   303

   find(keyValue): - find()  ,  
    ,   ,   end()   . :

    int count = 0;
    map<string,int>::iterator it = word_count.find( "wrinkles" );
    if ( it != word_count.end() )
    count = (*it).second;

         pair,   first 
,  second  . (      .)

                     6.12.3.    

        ,     . 
  ,  ,     
   - begin()  end().   
display_map_text():

   void
   display_map_text( map<string,loc*> *text_map )
   {
      typedef map<string,loc*> tmap;
      tmap::iterator iter = text_map->begin(),
      iter_end = text_map->end();

      while ( iter != iter_end )
      {
         cout << "word: " << (*iter).first << " (";
         int loc_cnt = 0;
         loc *text_locs = (*iter).second;
         loc::iterator liter = text_locs->begin(),
            liter_end = text_locs->end();

         while (liter != liter_end ) {
            if ( loc_cnt )
               cout << ',';
            else ++loc_cnt;
               cout << '(' << (*liter).first
                    << ',' << (*liter).second << ')';
               ++liter;
            }

         cout << ")\n";
         ++iter;
      }
      cout << endl;
   }

        ,    . ,
  ,    - size():

                                                               ++   304

   if ( text_map->size() )
      display_map_text( text_map );

      ,   ,   -
empty():

   if ( ! text_map->empty() )
      display_map_text( text_map );

                                    6.12.4. 

     ,   ,    
 .    . ,  
 ,     string.   ,
    ,    ,  . 
    /     (
   ,        
 ).       . 
   :

                                                               ++   305

   #include <map>
   #include <vector>
   #include <iostream>
   #include <string>

   int main()
   {
      map< string, string > trans_map;
      typedef map< string, string >::value_type valType;

      //  :
      //   
      trans_map.insert( va1Type( "gratz", "grateful" ));
      trans_map.insert( va1Type( "'em", "them" ));
      trans_map.insert( va1Type( "cuz", "because" ));
      trans_map.insert( va1Type( "nah", "no" ));
      trans_map.insert( va1Type( "sez", "says" ));
      trans_map.insert( va1Type( "tanx", "thanks" ));
      trans_map.insert( va1Type( "wuz", "was" ));
      trans_map.insert( va1Type( "pos", "suppose" ));

      //  
      map< string,string >::iterator it;

      cout << "  : \n\n";
      for ( it = trans_map.begin();
            it != trans_map.end(); ++it )
         cout << ": " << (*it).first << "\t"
              << ": " << ("it).second << "\n";

      cout << "\n\n";

      //  :   
      string textarray[14]={ "nah", "I", "sez", "tanx",
                            "cuz", "I", "wuz", "pos", "to", "not",
                            "cuz", "I", "wuz", "gratz" };

      vector< string > text( textarray, textarray+14 );
      vector< string >::iterator iter;

      //  
      cout << "  :\n\n";
      int cnt = 1;
      for ( iter = text-begin(); iter != text.end();
            ++iter,++cnt )
         cout << *iter << ( cnt % 8 ? " " : "\n" );
       cout << "\n\n\n";

      // map   
      map< string,int > stats;
      typedef map< string,int >::value_type statsValType;
      //    
      for ( iter=text.begin(); iter != text.end(); ++iter )
          if (( it = trans_map.find( *iter ))
                != trans_map.end() )
          {
             if ( stats.count( *iter ))
                stats [ *iter ] += 1;
             else stats.insert( statsVa1Type( *iter, 1 ));
                *iter = (*it).second;
           }

      //   
      cout << "  :\n\n";
      cnt = 1;
      for ( iter = text.begin(); iter != text.end();
            ++iter, ++cnt )
          cout << *iter << ( cnt % 8 ? " " : "\n" );
      cout << "\n\n\n";

      //  
      cout << "  :\n\n";
      map<string,int,less<string>,allocator>::iterator siter;
      for (siter=stats begin(); siter!=stats end(); ++siter)

                                                              ++   306

   }

      :

     :

   key: 'em value: them
   key: cuz value: because
   key: gratz value: grateful
   key: nah value: no
   key: pos value: suppose
   key: sez value: says
   key: tanx value: thanks
   key: wuz value: was

     :
   nah I sez tanx cuz I wuz pos
   to not cuz I wuz gratz

     :

   no I says thanks because I was suppose
   to not because I was grateful

     :

   cuz   2 ()
   gratz   1 ()
   nah   1 ()
   pos   1 ()
   sez   1 ()
   tanx   1 ()
   wuz   2 ()

                          6.12.5.   map

      - erase()    .
    erase()      
,         .
   ,        text_map  :

   string removal_word;
   cout << "  : ";
   cin >> removal_word;
   if ( text_map->erase( remova1_word ))
      cout << "ok: " << remova1_word << " \n";
   else cout << ": " << remova1_word << "  !\n";

     :      text_map?

                                                             ++   307

   map<string,loc*>::iterator where;
   where = text_map.find( remova1_word );

   if ( where == text_map->end() )
      cout << ": " << remova1_word << "  !\n";
   else {
      text_map->erase( where );
      cout << "ok: " << remova1_word << " !\n";
   }

      text_map      , 
     .       
   .   map    . 
    multimap,    
6.15.

                                      6.20

    ,    ,      
.      .   
  ,     .

                                    6.21

        ,     
   :  -       .

                                    6.22

       ,     . 
  map        
   .

                         6.13.   -

       /.  (set), , 
  . ,    
 bad_checks,   ,     
  .   ,   ,
     . ,  ,  
 -,  ,      bad_checks.
          -  , 
   (, , ), ,  the, and, into,
with, but  .. (   ,       
    : To be or not to be?).  
   word_map, ,       -. 
,  .

                  6.13.1.   set   
                                   

      set    
:

                                                                ++   308

       -:

   #include <set>
   set<string> exclusion_set;

           insert(). :

   typedef set< string >::difference_type diff_type;
   set< string > exclusion_set;

    insert()  ,     .
,        -.  
  ,      :

   ifstream infile( "exclusion_set" );
   if ( ! infile )
   {
      static string default_excluded_words[25] = {
         "the","and","but","that","then","are","been",
         "can"."can't","cannot","could","did","for",
         "had","have","him","his","her","its","into",
         "were","which","when","with","would"
      };
      cerr << "!    -! -- "
           << "    \n";
      copy( default_excluded_words, default_excluded_words+25,
            inserter( exclusion_set, exclusion_set.begin() ));
   }
   else {
      istream_iterator<string,diff_type> input_set(infile),eos;
      copy( input_set, eos, inserter( exclusion_set,
            exclusion_set.begin() ));
      exclusion_set.insert( "and" );
   }

         ,      
:  difference_type   inserter. difference_type   
       .   
     istream_iterator.
   copy()    . (     12  
.)          
.     ,    
,    .
        ,    :
         -
.   ,  copy()   ,   

                                                            ++   309

   .      
 .      ,   
copy()  .      inserter ( 
   12.4).

                                6.13.2.  

    ,      ,  
find()  count(). find()  ,   
,  ,  end(),   . count()  1
    0   .   
   build_word_map():

   if ( exclusion_set.count( textword ))
      continue;
   //   

                        6.13.3.   

         ,   
  (       17).  
,     ,    .  
  , :

   tomorrow and tomorrow and tomorrow

       .
             
 ,      :

   //     
   loc ploc = (*text_map)[ query_text ];
   //   
   //      
   set< short > occurrence_lines;
   loc::iterator liter = ploc->begin(),
      liter_end = ploc->end();
   while ( liter != liter_end ) {
      occurrence_lines.insert( occurrence_lines.end(),
      (*liter).first );
      ++liter;
   }

                                                             ++   310

    set    .   , 
occurrence_lines   .     
,     ,    :

   register int size = occurrence_lines.size();
   cout << "\n" << query_text
        << "  " << size
        << " ():")
        << "\n\n";
   set< short >::iterator it=occurrence_lines.begin();
   for ( ; it != occurrence_lines.end(); ++it ) {
      int line = -it;
      cout << "\t(  "
           << line + 1 << " ) "
           << (*text_file)[line] << endl;
   }

(  query_text()    .)
    set   size(), empty()  erase()    ,
   map,  .  ,   
    ,  set_union() () 
set_difference() (). (      
 17.)

                                  6.23

       ,    's'  
     .      Pythagoras,
Brahms  Burne_Jones.    suffix_s()   6.10 
 .

                                  6.24

    ,   ,     
   ,  ,   
 .  ,      
  ,      .   
    .     ;
,        
.       
  .

                           6.14.  

       ,    ,  
:         
TextQuery (       ), 
,   ,       ++ 
.

                                                              ++   311

   ,  iostream    .  
    . ,   
-   ,      .

                                                                ++   312

   //    ++
   #include <algorithm>
   #include <string>
   #include <vector>
   #include <utility>
   #include <map>
   #include <set>

   //   iostream,   
   #include <fstream.h>

   //   
   #include <stddef.h>
   #include <ctype.h>

   // typedef   
   typedef pair<short,short> location;
   typedef vector<location,allocator> loc;
   typedef vector<string,allocator> text;
   typedef pair<text*,loc*> text_loc;

   class TextQuery {
   public:
      TextQuery() { memset( this, 0, sizeof( TextQuery )); }
      static void
      filter_elements( string felems ) { filt_elems = felems; }

      void query_text();
      void display_map_text();
      void display_text_locations();
      void doit() {
         retrieve_text();
         separate_words();
         filter_text();
         suffix_text();
         strip_caps();
         build_word_map();
      }

   private:
      void retrieve_text();
      void separate_words():
      void filter_text();
      void strip_caps();
      void suffix_textQ;
      void suffix_s( string& );
      void build_word_map();

   private:
      vector<string,allocator> *lines_of_text;
      text_loc *text_locations;
      map< string,loc*,
           less<string>,allocator> *word_map;
      static string filt_elems;
   };

   string TextQuery::filt_elems( "\", ;: !?)(\V" );

   int main()
   {
      TextQuery tq;
      tq.doit();
      tq.query_text();
      tq.display_map_text();
   }

   void
   TextQuery::
   retrieve_text()
   {
      string file_name;
      cout << "please enter file name: ";
      cin >> file_name;

                                                                ++   313

   }

                                    6.25

   ,      inserter  
 - (    6.13.1,     12.4.1).

   set<string> exclusion_set;
   ifstream infile( "exclusion_set" );
   copy( default_excluded_words, default_excluded_words+25,
         inserter(exclusion_set, exclusion_set.begin() ));

                                    6.26

         : 
      . 
    ,    
    TextQuery.   .    
?

                                    6.27

             .  
       ;   7  ,  
.       ?

                      6.15.  multimap  multiset

    map  set     ,  multimap
()  multiset ()    
 . ,    
      .    
    ,        
  .   multimap  multiset 
     map  set:

   #include <map>
   multimap< key_type, value_type > multimapName;

   //  - string,  - list< string >
   multimap< string, list< string > > synonyms;

   #include <set>
   multiset< type > multisetName;

          
 ,   find() (   
 ),  ,   count(). ( , 
        
). :

                                                               ++   314

   #include <map>
   #include <string>

   void code_fragment()
   {
      multimap< string, string > authors;
      string search_item( "Alain de Botton" );
      // ...
      int number = authors.count( search_item );
      mu1timap< string,string >::iterator iter;

      iter = authors.find( search_item );
      for ( int cnt = 0; cnt < number; ++cnt, ++-iter )
         do_something( *iter );
      // ...
   }

            
 - equal_range(),    . 
      ,       
.         ,
   ,  end():

                                                                ++   315

   #include <map>
   #include <string>
   #include <utility>

   void code_fragment()
   {
      multimap< string, string > authors;
      // ...
      string search_item( "Haruki Murakami" );

      while ( cin && cin >> search_item )
         switch ( authors.count( search_item ))
         {
            //  
           case 0:
              break;

           //  1,  find()
           case 1: {
              multimap< string, string >: iterator iter;
              iter = authors.find( search_item );
              //   ...
              break;
           }
           //   ...
           default:
           {
              typedef multimap<string,string>::iterator iterator;
              pair< iterator, iterator > pos;

              // pos.first -  1- 
              // pos.second -  1- 
              //  
              pos = authors.equa1_range( search_item );
              for (; pos.first != pos.second; pos.first++ )
                 //   ...
           }
         }
   }

        multimap  multiset    
    map  set.  equal_range() 
 ,    :

   #include <multimap>
   #include <string>

   typedef multimap< string, string >::iterator iterator;
   pair< iterator, iterator > pos;
   string search_item( "Kazuo Ishiguro" );

   // authors - multimap<string, string>
   // 
   // authors.erase( search_item );
   pos = authors.equa1_range( search_item );
   authors.erase( pos.first, pos.second );

                                                              ++   316

      - insert()   ,   
       . :

   typedef multimap<string,string>::value_type valType;
   multimap<string,string> authors;

   //     Barth
   authors.insert( valType (
   string( "Barth, John" ),
   string( "Sot-Weed Factor" )));

   //     Barth
   authors.insert( va1Type(
   string( "Barth, John" ),
   string( "Lost in the Funhouse" )));

    multimap     .  
 :

   authors[ "Barth, John" ]; // : multimap

                                     6.28

         6.14   multimap
   .       ?
    ? ?

                                 6.16. 

     4.5        
  .        
   ,      
   ,    . 
     ,    ++  
stack,       :

   #include <stack>

         ,   .   
,            
  top()  pop().         6.5.

    6.5.   

	 	

	empty() 	 true,   ,  false 
			 
	size() 	    
	pop() 	    ,  
			  
	top() 	    

                                                                   ++   317

			,    
	push(item) 	    

          :

   #include <stack>
   #include <iostream>

   int main()
   {
      const int ia_size = 10;
      int ia[ia_size ]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      //  
      int ix = 0;
      stack< int > intStack;
      for ( ; ix < ia_size; ++ix )
         intStack.push( ia[ ix ] );

      int error_cnt = 0;
      if ( intStack.size() != ia_size ) {
         cerr << "!   IntStack: "
              << intStack.size()
              << "\t : " << ia_size << endl,
         ++error_cnt;
      }

      int value;
      while ( intStack.empty() == false )
      {
         //    
         value = intStack.top();
         if ( value != --ix ) {
            cerr << "!  " << ix
                 << "  " << value << endl;
            ++error_cnt;
         }
         //  
         intStack.pop();
      }

      cout << "     "
           << error_cnt << " " << endl;
   }

   

   stack< int > intStack;

 intStack   ,     
int.       , 
      .    deque, 
         , 
vector    .       
,     :

                                                                ++   318

   stack< int, list<int> > intStack;

   ,   ,     .  
        ,  
   .        
. :

   #include <stack>
   class NurbSurface { /* mumble */ };
   stack< NurbSurface* > surf_Stack;

           : ,
, , ,   ,   ,  
   .   .  
       .
             17.7 
   

   Civil && ( War || Rights )

                      6.17.     

        FIFO (first in, first out   ,
 ):     ,    .
      :  FIFO,
  ,    ,   
   .        , 
     . ,  
,    .     
, ,     .  , ,  
 15 ,    ,     .
       ,
   .
     queue  priority_queue    :

   #include <queue>

        queue  priority_queue   
6.6.

	 6.6.   queue  priority_queue

	 	

	empty() 	 true,   , 
			false   

                                                                  ++   319

	size() 	    
	pop() 	   ,  
			  .   
			    
			 
	front() 	   
			,    . 
			   
	back() 	   
			,    . 
			   
	top() 	   
			 ,    .
			     
	push(item) 	     .
			    
			   .

    priority_queue     . 
     ,   
. ,        -,
    . (  12.3   
      .)

                      6.18.    iStack

     iStack,     4.15,  :

       int.     
    .   ,       Stack;
      .     :
       ,      
            .
          .   ,
     ,         .

       iStack:

                                                                ++   320

   #include <vector>
   class iStack {
   public:
       iStack( int capacity )
          : _stack( capacity ), _top( 0 ) {};

      bool pop( int &value );
      bool push( int value );

      bool full();
      bool empty();
      void display();

      int size();

   private:
      int _top;
      vector< int > _stack;
   };

       .   
          
-.  _top   :  push_back()  pop_back()
    .     pop()
 push():

   bool iStack::pop( int &top_value )
   {
      if ( empty() )
         return false;
         top_value = _stack.back(); _stack.pop_back();
         return true;
     }

   bool iStack::push( int value )
   {
      if ( full() )
         return false;
         _stack.push_back( value );
         return true;
   }

   - empty(), size()  full()    :   
        .

   inline bool iStack::empty(){ return _stack.empty(); }
   inline bool iStack::size() { return _stack.size(); }
   inline bool iStack::full() {
      return _stack.max_size() == _stack.size(); }

      - display(),  _top    
   .

                                                               ++   321

   void iStack::display()
   {
      cout << "( " << size() << " )( bot: ";
      for ( int ix=0; ix < size(); ++ix )
         cout << _stack[ ix ] << " ";
      cout << " stop )\n";
   }

        iStack. 
     .      :

   inline iStack::iStack() {}

           .     
   iStack,        ,
      .   
       int:

   class iStack {
   public:
      iStack( int capacity = 0 );
      // ...
   };

      ,   ?      :

   inline iStack::iStack( int capacity )
   {
      if ( capacity )
         _stack.reserve( capacity );
   }

        ,   ,    
   .   :

   #include <vector>

   template <class elemType>
   class Stack {
   public:
      Stack( int capacity=0 );
      bool pop( elemType &value );
      bool push( elemType value );

      bool full();
      bool empty();
      void display();

      int size();
   private:
      vector< elemType > _stack;

                                                          ++   322

   };

       ,    
iStack,   typedef:

   typedef Stack<int> iStack;

          .

                                       6.29

     peek() ( 4.23   4.15)   
Stack.

                                       6.30

        Stack.    
 4.15   

                                      6.31

       List   5.11.1     Stack
   Primer_Third_Edition
