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

本文介绍了C++ STL中vector容器的两种末尾元素访问方式:end()与back()的区别。end()返回指向向量末尾之后位置的迭代器,而back()则直接返回向量最后一个元素的引用。通过示例展示了如何使用这两种方法,并强调了在空vector上调用back()可能导致未定义行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
### 关于 `std::vector` 插入元素方法 #### 使用 `push_back()` 方法 向容器的末尾添加单个元素是一个常见的操作。这可以通过调用成员函数 `push_back()` 来实现[^1]。 ```cpp #include <iostream> #include <vector> int main() { std::vector<int> vec; // 向 vector 的结尾处插入三个整数 vec.push_back(10); vec.push_back(20); vec.push_back(30); for (auto& elem : vec) { std::cout << elem << " "; } } ``` #### 使用 `insert()` 成员函数 对于更复杂的插入需求,比如在指定位置插入一个或多个元素,则可以利用 `insert()` 函数。此函数支持多种形式的参数传递方式来完成不同场景下的插入工作。 - **插入单一元素** ```cpp // 假设已有初始化好的 vector 对象 'vec' vec.insert(vec.begin(), 5); // 在开头插入数值 5 ``` - **插入范围内的元素** 当需要一次性插入一系列连续的数据项时,可提供一对迭代器作为输入给定起始终止边界: ```cpp std::vector<int> sourceVec{7, 8, 9}; vec.insert(vec.end(), sourceVec.begin(), sourceVec.end()); // 将另一个 vector 中的内容追加到当前对象后面 ``` - **重复填充特定数量的新元素** 如果目标是在某一处填入相同值构成的一组新条目,那么可以直接指明要加入多少次以及对应的初始值是什么样的: ```cpp vec.insert(vec.begin() + 1, 3, 42); // 在索引为 1 处之前插入三个值均为 42 的元素 ``` 通过上述几种形式的应用实例可以看出,在实际编程过程中可以根据具体的需求灵活选用合适的插入手段以达到最佳效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值