vector::begin

本文介绍了C++标准库中vector容器的begin()方法,该方法返回指向容器首元素的随机访问迭代器。通过示例代码展示了如何使用迭代器遍历vector容器中的所有元素。

Return iterator to beginning
Returns an iterator pointing to the first element in the vector.

Notice that, unlike member vector::front, which returns a reference to the first element, this function returns arandom access iterator pointing to it.

If the container is empty, the returned iterator value shall not be dereferenced.

Parameters(参数)

none

Return Value

An iterator to the beginning of the sequence container.

If the vector object is const-qualified, the function returns a const_iterator. Otherwise, it returns an iterator.

Member types iterator and const_iterator are random access iterator types (pointing to an element and to a const element, respectively).

// vector::begin/end
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
  vector<int> myvector;
  for (int i=1; i<=5; i++) myvector.push_back(i);

  cout << "myvector contains:";
  for (vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
   cout << ' ' << *it;
    cout << endl;
  return 0;
}


---------------------------------------------------

// vector::begin/end
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
  vector<int> myvector;
  for (int i=1; i<=5; i++) myvector.push_back(i);

  cout << "myvector contains:";
  vector<int>::iterator it;
  
  for (it = myvector.begin() ; it != myvector.end(); ++it)
   cout << ' ' << *it;
    cout << endl;
  return 0;
}


Output:

myvector contains: 1 2 3 4 5


----------------------------------


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值