vector::begin
返回一个随机访问迭代器到个矢量的第一个元素。
const_iterator begin() const;
iterator begin();
返回值
解决一个随机访问迭代器第一个元素在 vector 或给成功空 vector的位置。 应始终返回的值与 vector::end 比较确保它是有效的。
备注
如果 begin 的返回值赋给 vector::const_iterator,不能修改 vector 对象。 如果 begin 的返回值赋给 vector::iterator,可以修改 vector 对象。
示例
// vector_begin.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main()
{
using namespace std;
vector<int> c1;
vector<int>::iterator c1_Iter;
vector<int>::const_iterator c1_cIter;
c1.push_back(1);
c1.push_back(2);
cout << "The vector c1 contains elements:";
c1_Iter = c1.begin();
for (; c1_Iter != c1.end(); c1_Iter++)
{
cout << " " << *c1_Iter;
}
cout << endl;
cout << "The vector c1 now contains elements:";
c1_Iter = c1.begin();
*c1_Iter = 20;
for (; c1_Iter != c1.end(); c1_Iter++)
{
cout << " " << *c1_Iter;
}
cout << endl;
// The following line would be an error because iterator is const
// *c1_cIter = 200;
}
要求
标头: <vector>
命名空间: std