STLarray中的end方法(11)


STLarray中的end方法(11)

原文地址:http://www.cplusplus.com/reference/array/array/end/
public member function
<array>

std::array::end

      iterator end() noexcept;
const_iterator end() const noexcept;
Return iterator to end

Returns an iterator pointing to the past-the-end element in the array container.

返回array容器的超尾迭代器。


The past-the-end element is the theoretical element that would follow the last element in the array.
It does not point to any element, and thus shall not be dereferenced.

超尾元素是理论上是跟在array最后一个元素的元素,超尾迭代器不指向任何元素,也不应该被解除引用。

例子:

#include <iostream>
#include <array>
using namespace std;
int main()
{
	array<int,5> ai{10,20,30,40,50};
	cout<<"array is "<<endl;
	for(int i:ai)
		cout<<i<<" ";
	cout<<endl;
	cout<<"*ai.end() is "<<*ai.end()<<endl;
	cout<<endl;
}

运行截图:


看到对ai.end()解除引用居然是50!是不是很神奇?一开始我也觉得好惊悚!

哈哈哈,其实这是个误会!其实只是刚好ai.end()那个位置的值就是50!

用gdb调试一下就知道了。


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 array::begin to
specify a range including all the elements in the container.

因为标准库里面指示范围的方法不包括他们指向的闭迭代器,因此该方法经常和array::begin方法一起使用用以指示一个容器的范围。


In zero-sized arrays, this function returns the same as array::begin.

如果是空array,那么该方法的返回值和begin的返回值是一样的。


Parameters

none


Return Value

An iterator to the element past the end of the sequence.

返回一个迭代器指向超尾元素。


If the array object is const-qualified, the function returns a const_iterator. Otherwise, it
returns an iterator.

如果array对象是cosnt,那么返回的迭代器也是const的。


Member types iterator and const_iterator are random access iterator types
(pointing to an element and to a const element, respectively).

迭代器的类型属于随机访问迭代器。



Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// array::end example
#include <iostream>
#include <array>

int main ()
{
  std::array<int,5> myarray = { 5, 19, 77, 34, 99 };

  std::cout << "myarray contains:";
  for ( auto it = myarray.begin(); it != myarray.end(); ++it )
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}

Output:

myarray contains: 5 19 77 34 99



Complexity

Constant.


Iterator validity

No changes.


Data races

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.

容器的元素不会被访问,但是返回的迭代器可以用来访问以及修改元素,并且访问或者修改不同的元素是安全的。


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.

该方法不会抛出异常。

通过复制或者赋值获得的该迭代器副本也不会抛出异常。



——————————————————————————————————————————————————————————————————

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双

Email:coderguang@gmail.com

2014-8-30

于GDUT

——————————————————————————————————————————————————————————————————


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注