third_pw.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 1k
Development Platform:

C++ Builder

  1. //  If you use Visual C++, set the compile options to /GX
  2. #ifdef __BCPLUSPLUS__
  3. #include <iostream.h>
  4. #include <vector.h>
  5. #include <algorith.h>
  6. #else
  7. #include <iostream>
  8. #include <vector>
  9. #include <algorithm>
  10. #endif
  11. using namespace std;
  12. void PrintCube(int n)
  13. {
  14.     // prints the cube of integer n
  15.     cout << "The cube of " << n << " is " << n * n * n << endl;
  16. }
  17. void main(void)
  18. {
  19.     const int VECTOR_SIZE = 8 ;
  20.     typedef vector<int > IntVector ; // Define a vector of integers
  21.     typedef IntVector::iterator IntVectorIt ; // Define an iterator type
  22.     IntVector Numbers(VECTOR_SIZE) ; //vector containing numbers
  23.     IntVectorIt start, end, it ; // iterators
  24.     int i ;
  25.     // Initialize vector Numbers
  26.     for (i = 0; i < VECTOR_SIZE; i++)
  27.         Numbers[i] = i + 1 ;
  28.     start = Numbers.begin() ;   // location of first element of Numbers
  29.     end = Numbers.end() ;       // one past the location last element of Numbers
  30.     // print content of Numbers
  31.     cout << "Numbers { " ;
  32.     for(it = start; it != end; it++)
  33.         cout << *it << " " ;
  34.     cout << " }n" <<endl ;
  35.     // for each element in the range (first, last), print the element抯 cube 
  36.     for_each(start, end, PrintCube);
  37. }