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

NULL/default value for Template vector<typename> iterators

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

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;}
};

您可以将此声明为您的 class:

的成员
private:
    static const Vector::iterator null = Vector::iterator();

wish to initialize them to a fixed default value. In the case of pointers, I would have initialized to NULL, not sure what should be done for vector iterators.

std::vector::iterator 没有这样的东西。 std::vector::iterator 的实例是不合理的,除非它们与 std::vector.

相关联

您可以保留 std::vector::iterator 默认初始化状态,但除非将其设置为与 std::vector.

的实例关联的合适值,否则您将无法使用它