2013-10-07 193 views
0

好吧,基本上我需要使用std :: transform()函數。我試圖通過一個對象函數作爲最後一個參數,但它似乎並沒有工作。如何使用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()); 

上面我已經包含了類,函數和我用於變換的調用。任何人都知道爲什麼這不起作用?我無法弄清楚。

+2

什麼是'測試'?你有什麼錯誤? –

+0

「不起作用」是什麼意思? –

+0

爲什麼'it'是一個'bool'和一個迭代器? – Yakk

回答

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); 
} 
相關問題