我不熟悉c++语言。我已经开始使用向量,并注意到在我看到的所有通过索引遍历向量的代码中,for循环的第一个参数总是基于向量的值。在Java中,我可以对数组列表做这样的事情:

for(int i=0; i < vector.size(); i++){
   vector[i].doSomething();
}

我在c++中看不到这个的原因是什么呢?这是不好的做法吗?


当前回答

在STL中,程序员使用迭代器遍历容器,因为迭代器是一个抽象概念,在所有标准容器中实现。例如,std::list根本没有操作符[]。

其他回答

遍历vector最简洁的方法是通过迭代器:

for (auto it = begin (vector); it != end (vector); ++it) {
    it->doSomething ();
}

或(相当于上述)

for (auto & element : vector) {
    element.doSomething ();
}

在c++ 0x之前,必须将auto替换为迭代器类型,并使用成员函数而不是全局函数begin和end。

This probably is what you have seen. Compared to the approach you mention, the advantage is that you do not heavily depend on the type of vector. If you change vector to a different "collection-type" class, your code will probably still work. You can, however, do something similar in Java as well. There is not much difference conceptually; C++, however, uses templates to implement this (as compared to generics in Java); hence the approach will work for all types for which begin and end functions are defined, even for non-class types such as static arrays. See here: How does the range-based for work for plain arrays?

在STL中,程序员使用迭代器遍历容器,因为迭代器是一个抽象概念,在所有标准容器中实现。例如,std::list根本没有操作符[]。

不要忘记具有const正确性的例子-循环可以修改元素。这里的许多示例都没有这样做,应该使用cont迭代器。这里我们假设

class T {
  public:
    T (double d) : _d { d } {}
    void doSomething () const { cout << _d << endl; return; }
    void changeSomething ()   { ++_d; return; }
  private:
    double _d;
};

vector<T> v;
// ...
for (const auto iter = v.cbegin(); iter != v.cend(); ++iter) {
    iter->doSomething();
}

还要注意,在c++ 11表示法中,默认是复制元素。使用引用来避免这种情况,和/或允许修改原始元素:

vector<T> v;
// ...
for (auto t : v) {
    t.changeSomething(); // changes local t, but not element of v
    t.doSomething();
}
for (auto& t : v) {      // reference avoids copying element
    t.changeSomething(); // changes element of v
    t.doSomething();
}
for (const auto& t : v) { // reference avoids copying element
    t.doSomething();      // element can not be changed
}

这是一个更简单的方法来迭代和打印值在向量。

for(int x: A) // for integer x in vector A
    cout<< x <<" "; 
int main()
{
    int n;
    int input;
    vector<int> p1;
    vector<int> ::iterator it;

    cout << "Enter the number of elements you want to insert" << endl;
    cin >> n;

    for (int i = 0;i < n;i++)
    {
        cin >> input;
        p1.push_back(input);
    }
    for(it=p1.begin();it!=p1.end();it++)
    {
        cout << *it << endl;
    }
      //Iterating in vector through iterator it

    return 0;
}

迭代器的常规形式