⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 The source code samples for chapter 2, 4, 6, and 8 are contained in the EvenChapters project. Those
💻 CPP
字号:
// client.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "..\primesvr.h"
#include "..\primesvr_i.c"
#include "enum_iterator.h"

struct OutputPrime {
  void operator()(const long& nPrime) {
    cout << nPrime << " ";
  }
};

void main() {
  CoInitialize(0);

  CComPtr<IPrimeNumbers>  spPrimes;
  if( SUCCEEDED(spPrimes.CoCreateInstance(CLSID_PrimeNumbers)) )
  {
    // Populate the collection
    HRESULT hr;
    hr = spPrimes->CalcPrimes(0, 1000);

    // Count the number of items in the collection
    long  nPrimes;
    hr = spPrimes->get_Count(&nPrimes);
    cout << "Primes: " << nPrimes << endl;

    // Enumerate over the collection using sequencial access
    CComPtr<IEnumPrimes>  spEnum;
    hr = spPrimes->get_Enum(&spEnum);

    // Using an STL algorithm
    typedef enum_iterator<IEnumPrimes, &IID_IEnumPrimes, long> enum_primes;
    for_each(enum_primes(spEnum), enum_primes(), OutputPrime());

    /*
    // Using an STL-like iterator
    typedef enum_iterator<IEnumPrimes, &IID_IEnumPrimes, long> enum_primes;
    for( enum_primes it = enum_primes(spEnum, 64); it != enum_primes(); ++it ) {
      cout << *it << " ";
    }
    cout << endl;
    */

    /*
    // Yucky, raw way
    const size_t  MAX_PRIMES = 64;
    long          rgnPrimes[MAX_PRIMES];

    do {
      ULONG cFetched;
      hr = spEnum->Next(MAX_PRIMES, rgnPrimes, &cFetched);
      if( SUCCEEDED(hr) ) {
        if( hr == S_OK ) cFetched = MAX_PRIMES;
        for( long* pn = &rgnPrimes[0]; pn != &rgnPrimes[cFetched]; ++pn ) {
          cout << *pn << " ";
        }
      }
    }
    while (hr == S_OK);
    cout << endl;
    */

    spPrimes.Release();
  }

  CoUninitialize();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -