Using a function object for operation counting, second version : Functor « Development « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Development » Functor 
5.15.4.Using a function object for operation counting, second version
//Revised from
//STL Tutorial and Reference Guide C++ Programming with the Standard Template L
ibrary, 2nd Edition
//by David R. Musser (Author), Atul Saini (Author)
//# Publisher: Addison-Wesley Pub (Sd) (March 1996)
//# Language: English
//# ISBN-10: 0201633981
//# ISBN-13: 978-0201633986

#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
template <typename T>
class less_with_count : public binary_function<T, T, bool> {
public:
  less_with_count() : counter(0), progenitor(0) { }
  // Copy constructor:
  less_with_count(less_with_count<T>& x: counter(0)
      progenitor(x.progenitor ? x.progenitor : &x) { }
  bool operator()(const T& x, const T& y) {
    ++counter;
    return x < y;
  }
  long report() const return counter; }
  ~less_with_count() {  // Destructor
    if (progenitor) {
      progenitor->counter += counter; 
    }
  }
private:
  long counter;
  less_with_count<T>* progenitor;
};
int main()
{
  cout << "Using a function object for operation counting, "
       << "second version." << endl;
  const long N1 = 1000, N2 = 128000;
  for (long N = N1; N <= N2; N *= 2) { 
    vector<int> vector1;
    for (int k = 0; k < N; ++k
      vector1.push_back(k);
    random_shuffle(vector1.begin(), vector1.end());
    less_with_count<int> comp_counter;
    sort(vector1.begin(), vector1.end(), comp_counter);
    cout << "Problem size " << setw(9<< N 
         << ",  comparisons performed: " 
         << setw(9<< comp_counter.report() << endl;
  }
  return 0;
}
Using a function object for operation counting, second version.
Problem size      1000,  comparisons performed:     11846
Problem size      2000,  comparisons performed:     26397
Problem size      4000,  comparisons performed:     56776
Problem size      8000,  comparisons performed:    125715
Problem size     16000,  comparisons performed:    271505
Problem size     32000,  comparisons performed:    596740
Problem size     64000,  comparisons performed:   1235889
Problem size    128000,  comparisons performed:   2727581
5.15.Functor
5.15.1.Functor
5.15.2.Functor compose 2
5.15.3.Functor compose 3
5.15.4.Using a function object for operation counting, second version
5.15.5.Using a function object for operation counting, first version
5.15.6.Demonstrating function pointer passing
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.