共用方式為


vector::cbegin

傳回 const 隨機存取 Iterator 至容器 (Container) 中的第一個項目。

const_iterator cbegin() const;

傳回值

解決 const 隨機存取 Iterator 第一個項目 vector Class 是成功或空的 vector的位置。 您一定要比較之值傳回 vector::cend 或確定自己的 vector::end 是有效的。

備註

cbegin的傳回值,因此無法修改 vector 物件。

範例

// vector_cbegin.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    vector<int> c1;
    vector<int>::const_iterator c1_Iter;

    c1.push_back(1);
    c1.push_back(2);

    cout << "The vector c1 contains elements:";
    c1_Iter = c1.cbegin();
    for (; c1_Iter != c1.cend(); c1_Iter++)
    {
        cout << " " << *c1_Iter;
    }
    cout << endl;

    // The following line would be an error because iterator is const
    // *c1.cbegin() = 200;
}
  

需求

標題: <vector>

命名空間: std

請參閱

參考

vector Class

標準樣板程式庫