vector::end( )与vector::back( )

vector::end( )返回的是vector最后一个元素后的”结束元素“

vector::back( )返回的是vector最后一个元素

类比字符串,end( )返回的是’\0’,back( )返回的是字符串的最后一个字符

如果试图对vector::end( )返回的元素(结束元素)进行操作,将导致如下错误:

vector iterator not dereferencable

下面详细介绍两个函数并举例说明——


vector::end

Returns an iterator referring to the past-the-end element in the vector container.

The past-the-end element is the theoretical element that would follow the last element in the vector. It does not point to any element, and thus shall not be dereferenced.

Because the ranges used by functions of the standard library do not include the element pointed by their closing iterator, this function is often used in combination with vector::begin to specify a range including all the elements in the container.

If the container is empty, this function returns the same as vector::begin.

Example

// 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 << '\n';

  return 0;
}

Output

myvector contains: 1 2 3 4 5

vector::back

Returns a reference to the last element in the vector.

Unlike member vector::end, which returns an iterator just past this element, this function returns a direct reference.

Calling this function on an empty container causes undefined behavior.

Example

// vector::back
#include <iostream>
#include <vector>
using namespace std;

int main ()
{
  vector<int> myvector;

  myvector.push_back(10);

  while (myvector.back() != 0)
  {
    myvector.push_back ( myvector.back() -1 );
  }

  cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size() ; i++){
    cout << ' ' << myvector[i];
  }
  cout << '\n';

  return 0;
}

Output

myvector contains: 10 9 8 7 6 5 4 3 2 1 0
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
std::array和std::vector都是C++中的容器,用于存储和管理一系列的元素。它们有一些相似之处,但也有一些不同之处。 1. std::array: std::array是一个固定大小的数组,它在编译时就确定了大小。它的大小是固定的,不能动态改变。std::array的元素在内存中是连续存储的,可以通过索引访问元素。以下是std::array的一些常用操作[^1]: - 创建一个空的std::array对象:std::array<T, N> arr; - 获取std::array的大小:arr.size(); - 访问std::array中的元素:arr[i]或arr.at(i); - 清空std::array中的所有元素:arr.fill(value); 2. std::vector: std::vector是一个动态数组,它的大小可以在运行时动态改变。std::vector的元素在内存中也是连续存储的,可以通过索引访问元素。以下是std::vector的一些常用操作: - 创建一个空的std::vector对象:std::vector<T> vec; - 创建一个指定大小的std::vector对象:std::vector<T> vec(n); - 在std::vector的末尾添加元素:vec.push_back(value); - 获取std::vector的大小:vec.size(); - 清空std::vector中的所有元素:vec.clear(); - 删除std::vector中的最后一个元素:vec.pop_back(); - 删除std::vector中的指定元素:vec.erase(vec.begin() + i); - 迭代访问std::vector中的元素:for (auto it = vec.begin(); it != vec.end(); ++it) { ... } std::array适用于大小固定且不需要频繁创建销毁的情况,而std::vector适用于大小不确定且需要频繁创建销毁的情况。在性能方面,std::array在大部分情况下与std::vector相比没有明显的差距。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值