在使用vector容器时,我们可以通过调用其end函数来获取指向容器最后一个元素之后位置的迭代器。end函数返回的迭代器可以用于遍历容器的元素,或者用于指示容器的结束位置。需要注意的是,end函数返回的迭代器并不指向容器的最后一个元素,而是指向最后一个元素之后的位置。 下面我们通过一个简单的例子来进一步理解vector的end函数的用法。假设
一、Vector end函数的用法 Vector end函数用于获取Vector中最后一个元素的迭代器,其语法如下: vector_name.end() 其中,vector_name是Vector的名称。end函数返回的是一个迭代器,指向Vector中最后一个元素的下一个位置。需要注意的是,如果Vector为空,则end函数返回的迭代器和begin函数返回的迭代器相同。 下面是一个简...
vector<int>::iterator it; it = vector1.end()-1; cout << *it << endl; Output: 5 C++ STL程序演示vector::end()函数的例子 //C++ STL program to demonstrate example of//vector::end() function#include<iostream>#include<vector>usingnamespacestd;intmain(){vector<int> v1; v1.push_back(...
可以通过使用 * vector.begin() 或 *( vector.end() - 1) 来获得 vector 中第一个或最后一个的值; 也可以直接使用 vector.front() 、vector.back() 来得到 vector 首尾的值。
vector中begin(),end()和front(),back()的区别 一、begin函数 函数原型: iterator begin(); const_iterator begin(); 功能: 返回一个当前vector容器中起始元素的迭代器。 二、end函数 函数原型: iterator end(); const_iterator end(); 功能: 返回一个当前vector容器中末尾元素的迭代器。
The latest version of this topic can be found at vector::end (STL/CLR).Designates the end of the controlled sequence.SyntaxCopy iterator end(); RemarksThe member function returns a random-access iterator that points just beyond the end of the controlled sequence. You use it to obtain an ...
#include"iostream"using namespace std;#include"vector"intmain(){// 创建空的 vector 容器std::vector<int>vec{1,2,3};// 获取末尾迭代器vector<int>::iterator it=vec.end();// 该迭代器指向 容器中 最后一个元素 之后一个位置// 下面的代码会造成异常 , 不能获取对应的元素值cout<<*it<<endl;...
Returns the past-the-end iterator. 复制 iterator end( ); const_iterator end( ) const; Return Value The past-the-end iterator for the vector. If the vector is empty, vector::end() == vector::begin(). Remarks If the return value of end is assigned to a variable of type const_...
vector<int> data(10); auto temp_begin = data.begin(), temp_end= data.end(); for(;temp_begin!=temp_end;){ data.erase(temp_begin); } 产生这个问题的原因是:当我们调用erase方法删除元素的时候,erase方法会返回下一个元素的迭代器。当删除到最后一个元素的时候,这个时候返回的是最后一个元素之后...
vector(begin,end):复制[begin,end)区间内另一个数组的元素到vector中 2.增加函数 void push_back(const T& x):向量尾部增加一个元素X iterator insert(iterator it,const T& x):向量中迭代器指向元素前增加一个元素x iterator insert(iterator it,int n,const T& x):向量中迭代器指向元素前增加n个相同的...