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

news/2024/7/5 20:45:31 标签: iterator, vector, end, back

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

vectorback-返回的是vector最后一个元素">vector::back( )返回的是vector最后一个元素

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

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

vector iterator not dereferencable

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


vectorend">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

vectorback">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

http://www.niftyadmin.cn/n/1186813.html

相关文章

vector不是模板 list不是模板

解决方法很简单&#xff0c;也很出乎意料 使用vector和list只是简单地 #include<vector> #include<list> 是不够的 vector和list在命名空间std里&#xff0c;还需要添加声明 using namespace std;

IP地址与子网掩码做逐位与运算

在写简单路由器程序的时候&#xff0c;需要将捕获的IP数据报中的目的IP地址与每一个路由表项中的子网掩码作逐位与运算再与对应的目的IP地址作比较。 作逐位与运算有两种比较简单的方式 1. 移位 unsigned long IPandMask(unsigned long ul_ip, unsigned long ul_mask){unsign…

回车 换行 CR LF

回车与换行源于打字机的两种操作 013 CR 回车 Carriage Return&#xff08;从句尾回到句首&#xff09; 010 LF 换行 Line Feed&#xff08;往下一行&#xff09;

C++基本类型及派生类型的字长及值域

类型名字长/B取值范围bool{false,true}(signed) char1-128~127unsigned char10~255(signed) short (int)2-32768~32767unsigned short (int)20~65535(signed) int4-2147483648~2147483647unsigned (int)40~4294967295(signed) long (int)4-2147483648~2147483647unsigned long …

C++符号优先级

注&#xff1a;图片源自网络

C++中的register变量

用register说明的局部变量称为寄存器变量&#xff0c;该变量将可能以寄存器作为存储空间。 register说明仅能建议&#xff08;而非强制&#xff09;系统使用寄存器&#xff0c;这是因为寄存器虽然存取速度快&#xff0c;但个数有限&#xff0c;当寄存器不够用时&#xff0c;该…

C++中基于范围的for语句

// 输出array数组中全部元素 for(int &x:array){cout<<x;}

搜集的各种c#数组的操作

int[] A new[] { 1, 2, 3, 4, 5 };int[] B new[] { 2, 4, 9, 10 }; //交集 int[] C A.Intersect(B).ToArray(); //差集 int [] DA.Except(B)).ToArray(); //并集 int [] E A.Union(B).).ToArray();例子&#xff1a;int[] id1 { 44, 26, 92, 30, 71, 38 }; int[] id2 { 39,…