C++ 输入_迭代器,查找_if和模
我实现了一个迭代器,它的输出是斐波那契数。在我的main()中,我想找到一个可以除以17的数字(它是34)。为什么我的find_if语句不起作用 谢谢大家!C++ 输入_迭代器,查找_if和模,c++,boost,stl,iterator,C++,Boost,Stl,Iterator,我实现了一个迭代器,它的输出是斐波那契数。在我的main()中,我想找到一个可以除以17的数字(它是34)。为什么我的find_if语句不起作用 谢谢大家! #include <boost/operators.hpp> #include <algorithm> #include <tr1/functional> struct FibIter: boost::input_iterator_helper<FibIter,unsigned
#include <boost/operators.hpp>
#include <algorithm>
#include <tr1/functional>
struct FibIter: boost::input_iterator_helper<FibIter,unsigned long long> {
//typedef value_type unsigned long long;
FibIter(value_type l=0):counter(0), fib0(0),fib1(1){
while(l!=counter)
++(*this);
}
value_type operator*() const {return fib0;}
FibIter& operator++() {
++counter;
value_type fnew = fib0+fib1;
fib0 = fib1;
fib1 = fnew;
return *this;
}
bool operator==(FibIter const &other)const {
return counter == other.counter;
}
private:
value_type counter;
value_type fib0, fib1;
};
using namespace std;
using namespace tr1;
using namespace placeholders;
int main() {
FibIter found = find_if(FibIter(), FibIter(100),bind(not2(modulus<unsigned long long>()),_1,17ULL));
}
#包括
#包括
#包括
结构FibIter:boost::输入迭代器\u助手{
//typedef value_type无符号长;
FibIter(值类型l=0):计数器(0)、fib0(0)、fib1(1){
while(l!=计数器)
++(*本条);
}
值类型运算符*()常量{return fib0;}
FibIter&运算符++(){
++计数器;
值\类型fnew=fib0+fib1;
fib0=fib1;
fib1=fnew;
归还*这个;
}
布尔运算符==(光纤常数和其他)常数{
返回计数器==other.counter;
}
私人:
值型计数器;
fib0、fib1型值;
};
使用名称空间std;
使用名称空间tr1;
使用名称空间占位符;
int main(){
FibIter found=查找if(FibIter(),FibIter(100),bind(not2(模数()),_1,17ULL));
}
绑定(非2(模数()),\u 1,17ULL)
应该是
not1(bind(modulus<unsigned long long>(),_1,17ULL))
not1(绑定(模数(),_1,17ULL))
我想如果你从第一个数字0开始,find将返回它(模(0,17)=0)。好的,我用!boost::bind(…)而不是tr1::bind。首先,您可以使用std::advance(*this,l)代码>而不是构造函数中的循环。但是您的方法是非常低效的,因为它需要计算两次斐波那契序列:一次是为了知道最后一个斐波那契数是什么,另一次是在客户机代码使用++
时实际到达那里
我认为更好的方法是将构造函数中最后一个Fibonacci数的索引存储在私有成员中,并在每次++
之后将计数器与它进行比较。它可以通过以下方式从已经存在的索引字段中重新使用:
让默认构造函数将斐波那契迭代器初始化为fib0=0
和fib1=1
,以及index=0
。另一个构造函数(参数化)将创建一个伪迭代器,该迭代器只保存序列的最后一个索引,其他字段无效。此一迭代器将是“结束后的一个”迭代器,仅用于索引比较,不用于读取值
因此,我的解决方案如下:
#include <iterator>
class Fibonacci : public iterator<input_iterator_tag, int>
{
unsigned index;
value_type fib0, fib1;
public:
Fibonacci(): index(0), fib0(0), fib1(1) { }
explicit Fibonacci(unsigned last): index(last), fib0(0), fib1(0) { }
bool operator == (const Fibonacci& other) { return index == other.index; }
bool operator != (const Fibonacci& other) { return !(*this == other); }
value_type operator * () const { return fib0; }
Fibonacci& operator ++ () {
++index;
value_type fnew = fib0 + fib1;
fib0 = fib1; fib1 = fnew;
return *this;
}
Fibonacci operator ++ (int) {
Fibonacci current(*this);
++(*this);
return current;
}
};
#include <iostream>
#include <algorithm>
int main()
{
using namespace std;
ostream_iterator<int> out(cout, "; ");
cout << "Fibonacci numbers from 0th to 19th:\n";
copy(Fibonacci(), Fibonacci(20), out);
cout << endl;
}
正如您所看到的,在我的示例中还有更多的代码重用:我通过调用其他操作符来重用其他操作符中的操作符
当然,它只适用于适合int
的数字。最好重写它以使用多精度数字(如GMP库)
#include <iterator>
class Fibonacci : public iterator<input_iterator_tag, int>
{
unsigned index;
value_type fib0, fib1;
public:
Fibonacci(): index(0), fib0(0), fib1(1) { }
explicit Fibonacci(unsigned last): index(last), fib0(0), fib1(0) { }
bool operator == (const Fibonacci& other) { return index == other.index; }
bool operator != (const Fibonacci& other) { return !(*this == other); }
value_type operator * () const { return fib0; }
Fibonacci& operator ++ () {
++index;
value_type fnew = fib0 + fib1;
fib0 = fib1; fib1 = fnew;
return *this;
}
Fibonacci operator ++ (int) {
Fibonacci current(*this);
++(*this);
return current;
}
};
#include <iostream>
#include <algorithm>
int main()
{
using namespace std;
ostream_iterator<int> out(cout, "; ");
cout << "Fibonacci numbers from 0th to 19th:\n";
copy(Fibonacci(), Fibonacci(20), out);
cout << endl;
}
Fibonacci numbers from 0th to 19th:
0; 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; 233; 377; 610; 987; 1597; 2584; 4181;