如何使用 std::transform() 函数
How to use std::transform() function
好吧,基本上我需要使用 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());
上面我包含了我用于转换的类、函数和调用。有人知道为什么这不起作用吗?我想不通。
如果您在某处提供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);
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 在 transform() 中调用成员函数
- 如何在 std::transform 中使用 std::p ow 作为一元函数?
- 在集合上运行的函数模板,该集合具有默认的"transform"函数,该函数不执行任何操作
- 为什么在 std::transform 中使用函数对象参数失败并且需要 lambda 表达式
- 没有匹配函数用于调用 std::transform,未解析的重载函数类型
- 如何使用 std::transform() 函数
- KEIL 错误:没有与参数列表匹配的重载函数"std::transform"实例
- 使用 std::transform 调用对象的函数
- 获取CUDA推力::transform运算符()函数内向量的索引
- 我可以默认这个Transform模板函数的返回类型吗?
- 使用for_each或std::transform时,c++函函数构造函数是如何被调用的?
- c++的transform和lambda函数