Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
Cpp - 4) for_each ( Vector )       Share
2008-11-03 |  RatheeshTR  | Viewed: 286  |    0

#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream.h>

class print_elements {

public:

   
void operator()( string elem ) {
      cout << elem

           << ( _line_cnt++%8 ? " " : "\n\t" );

   }

   static 
void reset_line_cnt() { _line_cnt = 1; }
        

private:

   static 
int _line_cnt;
};

        

int print_elements::_line_cnt = 1;

/*  generates:
     original  list  of  strings:
     The  light  untonsured  hair  grained  and  hued  like
     pale  oak
                     
     sequence  after  copy_backward(  begin+1,  end-3,  end  ):
     The  light  untonsured  hair  light  untonsured  hair  grained
     and  hued
*/

        

int main()
{

    string sa[] = {  "The", "light", "untonsured", "hair",

                                        "grained", "and", "hued", "like", "pale", "oak" };

   

    vector< string, allocator > svec( sa, sa+10 );

                

   cout << "original list of strings:\n\t";

   
for_each( svec.begin(), svec.end(), print_elements() );
   cout << "\n\n";

                

   copy_backward( svec.begin()+1, svec.end()-3, svec.end() );

   

   print_elements::reset_line_cnt();

   

   cout << "sequence after " << "copy_backward( begin+1, end-3, end ):\n";

   
for_each( svec.begin(), svec.end(), print_elements() );
   cout << "\n";    

}


Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software
Tech: C ,Cpp
Send Mail: malathi.v



Related topics
Stack Implementation
cpp stack example

General Topics
elemets of a defect report
measures of defect report
test metrics
agile methodology
test entry and exit criteria
severity and priority
race condtions and memory leak
performance testing

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us