Return iterator to beginning
Returns an iterator pointing to the first element in the
vector
.
该方法返回一个指向该vector中第一个元素的iterator.
Notice that, unlike member
vector::front
, which returns a reference to the first element, this function returns a
random access iterator
pointing to it.
需要注意的是
,和front()方法不同,front是返回第一个元素的引用,而begin返回的是一个指向第一个元素的随机访问迭代器。
If the container is
empty
, the returned iterator value shall not be dereferenced.
如果vector是空的,使用begin返回的迭代器不应该被解除引用。
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
vector<int> vi;
vector<int>::iterator vb=vi.begin();
cout<<*vb<<endl;
}
编译运行结果:
可以看到,如果对其进行解除引用就会引发错误。
Parameters
//以后这种超级简单的语句我就不翻译了
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
.
如果该vector是const属性的,那么返回值也将是const属性的,否则,就是返回一个普通的iterator。
Member types
iterator
and
const_iterator
are
random access iterator
types (pointing to an element and to a const element, respectively).
返回值迭代器的类型
属于随机访问迭代器
。
Example
// vector::begin/end
#include <iostream>
#include <vector>
int main ()
std::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
Edit & Run
Data races
数据的竞争性?:(这里可能不太准确)
The container is accessed (neither the const nor the non-const versions modify the container).
//容器需要支持被访问?(不管返回值是哪个迭代器,都不会修改容器)
No contained elements are accessed by the call, but the iterator returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.
//该调用不会访问容器的元素(??),但是返回的这个iterator可以用来访问或者是修改元素,同时通过该iteraotr访问或者是修改元素是安全的。
Exception safety
异常安全性:
No-throw guarantee: this member function never throws exceptions.
该方法不会抛出异常。
The copy construction or assignment of the returned iterator is also guaranteed to never throw.
//这句不知道怎么翻译才最好。
复制构造器或者(assignment of the returned iterator)一样不会抛出异常。
-----------------------------------------------------------------------------------------------------------
//已修正该句://2014-8-9
利用复制构造器或者是赋值运算符得到的该iterator也不会抛出异常。
----------------------------------------------------------------------------------------------------
//翻译的不好的地方请指出来或者联系我修改,谢谢。
//2014-8-9 于GDUT
vector动态增长的基本原理
当插入新元素时,如果空间不足,那么vector会重新申请更大的一块内存空间,将原来空间拷贝到新空间上,释放旧空间的数据,再把新元素插入新申请的空间。
默认会申请两倍的新空间,但当空间达到一定大小时,会动态调整其增长策略
#include
#include
using namespace std;
void PrintVector(vector& v) {
for (vector::iterator it = v.begin(); it != v.end(); it++) {
cout << the va
看到篇文章,内容很基础,但是反应的问题是值得思考的。遍历vector的标准方法是什么?之前按照书中的理论,认为stl标准的迭代器或者stl的for_each算法应该是最快的。但是如果不经过实际程序的检验,那么理论永远是理论。实践表明operator[]的访问速度是最快的(至少不比迭代器更慢)。之前我洁癖性的把所有使用operator[]进行vector遍历的地方都改成迭代器,认为这样更高效,...
之前看见std::vector 容器的begin()、end()、front()、back()用法,了解begin()和end(),不了解front()和back()方法,今天没事查了下博客,验证了下,留个随手笔记。
一、begin函数
函数原型:
iterator begin();
const_iterator begin();
返回一个当前vector容器中起始元素的迭代器。
二、end函数
函数原型:
iterator end();
const_iterator end();
同事遇见个bug,在一个容器中使用std::find寻找指定元素,找到后将一个标志位置位。但是log数据找到了指定元素,但实际容器中却不含有此元素。
void findElement(const vector<int>& vec, int key)
auto it = find(vec.begin(), vec.end(), key);
if (it != vec.end()) {
// setFlag(true);
本文目录1 vector介绍2 vector使用注意3 基本操作4 常用函数详细解释4.1 begin函数:4.2 end函数:4.3 front函数:4.4 back函数:5 vector中insert()的用法详解6 vector的reverse和sort的使用7 输出vector中的元素注意:
1 vector介绍
向量容器(vector)是一种顺序容器,是一块连续分配的内存,支持随机访问,和数组极其相似.
数组跟vector的区别在于:数组是静态分配空间,一旦分配了空间的大小,就不可以再改变
参考博客:http://blog.csdn.net/kjing/article/details/6936325
迭代器和反向迭代器使用时:
end()永远指向最后一个元素的下一个位置;
若容器为空时,迭代器的begin()和end()指向位置相同,而后即使再给容器赋值,迭代器的begin()指向,仍然为空。
错误范例:
#include
#include
void pri
https://blog.csdn.net/qq_44778120/article/details/122775457?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167738918016800213089919%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167738918016800213089919&biz_id=0&utm_med