1

基本的に、std::transform() 関数を使用する必要があります。ただし、最後の引数としてオブジェクト関数を渡そうとしていますが、うまくいかないようです。

class isprime {
  public:
    // declares the constructor and the operator() overloading
    isprime(){ number = 0, primes.push_back(2);};
    bool operator()(int);

  private:
    // various private functions and variables
   int number;
   list<int> primes;    //creats a list via the stl library
   void morePrimes(int);    //function to generate more prime numbers
   bool it; // Iterator for the morePrimes list
   bool primeCheck;         // Bool used in the morePrimes function
};


bool isprime::operator()(int number)
{
    if(number == 1)             //returns false for 1
        return false;

    if(number > primes.back()){     //Tests to see if the list of primes range would include the number, if not it runs the morePrimes function
        morePrimes(number);
    }

    it = binary_search(primes.begin(),primes.end(),number); //uses stl find to see if the number is in the list of primes.

    if(it)  //if the returned iterator points to a value = to number then number is prim.
        return true;

    return false;       //returns false if the number wasnt found
};

 transform(random_list.begin(), random_list.end(), isprime_list.begin(), test());

上記には、変換に使用したクラス、関数、および呼び出しが含まれています。これが機能しない理由を知っている人はいますか?私はそれを理解することはできません。

4

1 に答える 1

0

void isprime::more_primes(int)どこかに提供する場合は、括弧を削除するtest()だけで十分です。

int main()
{
    auto gen = bind(uniform_int_distribution<>(), mt19937());
    list<int> random_list;

    for (int i=0; i<5; i++) random_list.push_back(gen());

    list<int> isprime_list(random_list.size());

    isprime test;
    transform(random_list.begin(), random_list.end(), isprime_list.begin(), test);
}

Coliru で「実行中」(まあ、コンパイル中) を参照してください: http://coliru.stacked-crooked.com/a/3d908cb5cdc5c543

#include <list>
#include <algorithm>
using namespace std;

class isprime {
    public:
        // declares the constructor and the operator() overloading
        isprime(){ number = 0, primes.push_back(2);};
        bool operator()(int);

    private:
        // various private functions and variables
        int number;
        list<int> primes;     // creats a list via the stl library
        void morePrimes(int); // function to generate more prime numbers
        bool it;              // Iterator for the morePrimes list
        bool primeCheck;      // Bool used in the morePrimes function
};

bool isprime::operator()(int number)
{
    if(number == 1)              // returns false for 1
        return false;

    if(number > primes.back()){ // Tests to see if the list of primes range would include the number, if not it runs the morePrimes function
        morePrimes(number);
    }

    it = binary_search(primes.begin(),primes.end(),number); // uses stl find to see if the number is in the list of primes.

    if(it)                      // if the returned iterator points to a value = to number then number is prim.
        return true;

    return false;               // returns false if the number wasnt found
}

void isprime::morePrimes(int) {}

#include <random>
#include <functional>
#include <iostream>
#include <iterator>

int main()
{
    auto gen = bind(uniform_int_distribution<>(), mt19937());
    list<int> random_list;

    for (int i=0; i<5; i++) random_list.push_back(gen());

    list<int> isprime_list(random_list.size());

    isprime test;
    transform(random_list.begin(), random_list.end(), isprime_list.begin(), test);
}
于 2013-10-07T21:02:02.770 に答える