读书人

【新手弱问】关于copy函数,该怎么解决

发布时间: 2012-02-13 17:20:26 作者: rapoo

【新手弱问】关于copy函数
试问algorithm库里的copy函数是个什么样的机理啊??

template<class Init, class T>
copy( Init beg, Init end, T a )
{...}

是实现*(a++) = * I(I 是beg 到end中的迭代器 )

那为什么下面这一段代码无法编译通过?

//MyMax 实现寻找序列中的最大值

#include<vector>
#include <iostream>
#include<algorithm>
using namespace std;
template <class T>
class MyMax
{
public:
T * pMax; //指向用于存放最大值的变量
MyMax (T * p):pMax(p) { };
MyMax & operator++(int k){return *this;}
T & operator*( MyMax & a ){ return *(a.pMax) }
T operator()(){
return *pMax;
}
};
class A {
public:
int i;
A( int n) :i(n) { };
A() { };
};
bool operator < ( const A & a1, const A & a2)
{
return a1.i < a2.i ;
}
ostream & operator<<( ostream & o, const A & a)
{
o << a.i;
return o;
}
int main()
{
A a[5] = {A(1),A(5),A(3),A(4),A(2)};
int b[9] = {1,5,30,40,2,136,80,20,6};
int nMax;
A aMax;
MyMax<A> outputa( & aMax);
copy(a,a+5,outputa);
cout << outputa() << endl;
MyMax<int> output( & nMax);
copy(b,b+9,output);
cout << output() << endl;
return 0;
}


[解决办法]
copy的参数应该是迭代器
[解决办法]
template <class InputIterator, class OutputIterator>
OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result );
关于参数:
first, last
Input iterators to the initial and final positions in a sequence to be copied. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
result
Output iterator to the initial position in the destination sequence. This shall not point to any element in the range [first,last).



[解决办法]
数组用直接用指针做迭代器是对的,但你自定义的类就不行了!
[解决办法]
给你改了几个地方,LZ对一些操作符的重载还不是很符合规则。
另外,要做一个output iterator的话,需要定义一些iterator_trans,最简单的方法是继承
iterator。
(BTW:only tested on GCC4.5)

C/C++ code
#include<vector>#include <iostream>#include<algorithm>using namespace std;class A{public:    int i;    A( int n) :i(n) { };    A() { };    // 为了实现A的大小比较    bool operator < (const A& left)    {        return i<left.i;    }    // 为了可以让A赋值    A& operator=(const A& left)    {        i = left.i;    }};template <class T>//继承iterator,告诉算法,我们的iterator是一个output的类型的iterator(也就是可以支持++,和赋值)class MyMax : public iterator<output_iterator_tag,T>{public:    T * pMax; //指向用于存放最大值的变量    MyMax (T * p):pMax(p) { };    MyMax & operator++() //这里可以什么都不做,因为LZ的意思应该是找到最大值    {        return *this;    }    MyMax& operator*()   //*outputa = A(0);这样的语法需要    {        return *this;    }    T& operator() ()     // outputa()需要,实际返回A    {        return *pMax;    }    MyMax& operator = (const A& val)  //LZ楼主的类命名,我猜赋值应该是为了记录最大值吧?    {        if(*pMax < val)            *pMax = val;        return *this;    }};ostream & operator<<( ostream & o, const A & a){    o << a.i;    return o;}int main(){    A a[5] = {A(1),A(5),A(3),A(4),A(2)};    int b[9] = {1,5,30,40,2,136,80,20,6};    int nMax;    A aMax;    MyMax<A> outputa( & aMax);    *outputa = A(1);    cout << outputa() << endl;    copy(a,a+5,outputa);    cout << outputa() << endl;    MyMax<int> output( & nMax);    copy(b,b+9,output);    cout << output() << endl;    return 0;} 


[解决办法]
C:\Program Files\Microsoft Visual Studio\VC98\Include\XUTILITY

C/C++ code
...        // TEMPLATE FUNCTION copytemplate<class _II, class _OI> inline    _OI copy(_II _F, _II _L, _OI _X)    {for (; _F != _L; ++_X, ++_F)        *_X = *_F;    return (_X); }... 

读书人网 >C++

热点推荐