begin函数
语法:
1 2 |
iterator begin(); const_iterator begin() const; |
begin() 返回第一个元素的迭代器
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// iterators /** * Returns a read/write iterator that points to the first * element in the %vector. Iteration is done in ordinary * element order. */ iterator begin() { return iterator (this->_M_impl._M_start); } /** * Returns a read-only (constant) iterator that points to the * first element in the %vector. Iteration is done in ordinary * element order. */ const_iterator begin() const { return const_iterator (this->_M_impl._M_start); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1(5, 0); vector<int> v2(10, 2); vector<int>::iterator iter = v1.begin(); for(iter;iter!=v1.end();iter++){ cout<<(*iter)<<endl; } } |
测试结果:
1 2 3 4 5 |
0 0 0 0 0 |