Chinaunix首页 | 论坛 | 博客
  • 博客访问: 848058
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:10:59

#include
#include
#include
#include
#include
#include

using std::binary_function;
using std::unary_function;
using std::accumulate;
using std::cout;
using std::string;
using std::vector;
using std::for_each;
using std::ostream_iterator;
using std::copy;
using std::endl;
using std::sort;
using std::mem_fun_ref;

struct Member
{
   string Surname;
   string Name;
   int    VideosOut;
   int DaysOverdue;

   Member::Member(string surname, string name, int videosOut = 0,
      int daysOverdue = 0) : Surname(surname),
      Name(name), VideosOut(videosOut), DaysOverdue(daysOverdue) {};

   int Print()
   {
      cout << "Name: " << Surname <<", " << Name;

      if (VideosOut > 0) cout << "  Videos Out = " << VideosOut;

      if (DaysOverdue > 0) cout << "  Days overdue = " << DaysOverdue;

      cout << endl;

      return 0;
   }
};

void PrintItem(const Member & m)
{
   cout << "Name: " << m.Surname <<", " << m.Name;

   if (m.VideosOut > 0) cout << "  Videos Out = " << m.VideosOut;

   if (m.DaysOverdue > 0) cout << "  Days overdue = " << m.DaysOverdue;

   cout << endl;
}

struct CountOverdueDays : public unary_function
{
   CountOverdueDays(int nDayLimit) : m_nDayLimit(nDayLimit) { }
   int operator()(int nOverdueSoFar, const Member& m) 
   { 
      return m.DaysOverdue >= m_nDayLimit ?
                   nOverdueSoFar + m.VideosOut : nOverdueSoFar;
   }
private: 
   const int m_nDayLimit;
};

struct SortByVideosOut : public binary_function
{
   bool operator() (const Member & m1, const Member & m2)
   {
      return (m1.VideosOut < m2.VideosOut);
   }
};

bool SortMember(const Member m1, const Member m2)
{
   if (m1.Surname < m2.Surname)
      return true;
   else if (m1.Surname > m2.Surname)
      return false;

   return (m1.Name < m2.Name);
}

struct CountIntSort : public binary_function
{
public:
   static int nCount;

   bool operator() (const int n1, const int n2)
   {
      ++ nCount;
      return (n1 < n2);
   }
};

int CountIntSort::nCount = 0;

struct CountIntSort2 : public binary_function
{
public:
   int nCount;

   CountIntSort2() : nCount(0){};

   bool operator() (const int n1, const int n2)
   {
      ++ nCount;
      return (n1 < n2);
   }
};

struct CountIntSort3 : public binary_function
{
public:
   int nCount;
   CountIntSort3 * pParent;
   int nCopies;

   CountIntSort3() : nCount(0), nCopies(0), pParent(0) {};

   CountIntSort3(CountIntSort3 & c) : nCount(0), nCopies(1)
   {
      pParent = &c;
   }

   ~CountIntSort3()
   {
      pParent->nCount += nCount;
      pParent->nCopies += nCopies;
   }

   bool operator() (const int n1, const int n2)
   {
      ++ nCount;
      return (n1 < n2);
   }
};

int _tmain(int argc, _TCHAR* argv[])
{
   vector vecMem;

   vecMem.push_back(Member("Zaphir", "Jill"));
   vecMem.push_back(Member("Smith", "John", 5, 0));
   vecMem.push_back(Member("Smith", "Phil", 1, 3));
   vecMem.push_back(Member("Jones", "Susan"));
   vecMem.push_back(Member("Kaputnik", "Joe", 10, 5));
   vecMem.push_back(Member("Jones", "Bill", 3, 7));

   for_each(vecMem.begin(), vecMem.end(), PrintItem);

   cout << endl;

   sort(vecMem.begin(), vecMem.end(), SortMember);

   cout << "Now the same list sorted\n\n";

   for_each(vecMem.begin(), vecMem.end(), mem_fun_ref(&Member::Print));

   cout << endl;

   int nOverdue = accumulate(vecMem.begin(), vecMem.end(),
      0 /* initval = identity of addition */,
      CountOverdueDays(5));

   cout << "Number of videos overdue by five or more days: " << nOverdue;

   cout << "\n\nSorted by number of videos out:\n\n";

   SortByVideosOut v;

   sort (vecMem.begin(), vecMem.end(), v);

   for_each(vecMem.begin(), vecMem.end(), mem_fun_ref(&Member::Print));

   cout << endl;

   vector vecInt;
  
   for (int i = 0; i < 10000; ++i)
      vecInt.push_back(rand()% 10000);

   CountIntSort::nCount = 0;

   CountIntSort c;

   // Use a copy so it always requires the same number of iterations

   vector vecInt2 = vecInt;

       sort(vecInt2.begin(), vecInt2.end(), c);

   cout << "Sorting a vector of 10000 ints took "
        << CountIntSort::nCount << " calls to the sort function"
        << endl;

   CountIntSort2 c2;

   vecInt2 = vecInt;

   sort(vecInt2.begin(), vecInt2.end(), c2);

   cout << "Sorting a vector of 10000 ints second try took "
        << c2.nCount << " calls to the sort function" << endl;

   CountIntSort3 c3;

   vecInt2 = vecInt;

   sort(vecInt2.begin(), vecInt2.end(), c3);

   cout << "Sorting a vector of 10000 ints second try took "
       << c3.nCount
       << " calls to the sort function, and the predicate was copied "
       << c3.nCopies << " times." << endl;

   string s;
   std::cin >> s;

   return 0;
}


--------------------next---------------------

阅读(425) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~