front() 和 begin() 之间的区别
许多 STL 容器中出现的 front()
和 begin()
函数有什么区别?
What is the difference between the front()
and begin()
functions that appears in many STL containers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
begin()
返回一个迭代器,可用于迭代集合,而front()
仅返回对第一个元素的引用的集合。begin()
returns an iterator that can be used to iterate through the collection, whilefront()
just returns a reference to the first element of the collection.front()
返回对第一个元素的引用,begin()
返回它的迭代器。请注意,您不应在空容器上调用
front
,但只要不取消引用begin
的迭代器,就可以调用begin
代码> 返回。front()
returns a reference to the first element,begin()
returns an iterator to it.Note that you shouldn't call
front
on an empty container, but it's OK to callbegin
as long as you don't dereference the iterator thatbegin
returns.front
成员返回对列表或向量的第一个成员的引用。 begin 函数返回一个迭代器(更像是一个指针),该迭代器被初始化为列表、映射或向量的第一个成员。The
front
member returns a reference to the first member of a list or vector. Thebegin
function returns an iterator (which is more like a pointer) initialized to the first member of a list, map, or vector.来自 http://www.cplusplus.com/reference/stl/vector/begin/< /a> (实际上是“vector::begin”的第一个谷歌结果):
From http://www.cplusplus.com/reference/stl/vector/begin/ (literally the first google result for "vector::begin"):
front()
begin()
front()
begin()