模板向量<typename>迭代器的NULL /默认值

iju*_*eja 2 c++ iterator stdvector

我希望将矢量迭代器作为c ++中类定义的一部分,并希望将它们初始化为固定的默认值.在指针的情况下,我会初始化为NULL,不知道应该为vector迭代器做什么.

class ball{
private:
    double x;       
    double v;   
    double timeToNext;  
    vector<ball>::iterator lball, rball;//How should they be initialized?
public:
    ball()  {   //default constructor
    x=-1*INF;
    v=0;
    cout<<"Ball added"<<endl;
    //want to initialize iterators
    }
    void setx(double &pos){x=pos;}
    void setv(double &vel){v=vel;}
    void setTimeToNext()    {
        double tl, tr;
    }
    double getx(){return x;}
    double getv(){return v;}
};
Run Code Online (Sandbox Code Playgroud)

R S*_*ahu 5

希望将它们初始化为固定的默认值.在指针的情况下,我会初始化为NULL,不知道应该为vector迭代器做什么.

没有这样的事情std::vector::iterator.std::vector::iterator除非与a相关,否则实例不合理std::vector.

您可以保留std::vector::iterator默认值,但是除非将其设置为与实例关联的合适值,否则不能使用它std::vector.