什么是“最后一个元素"?在向量中意味着什么? [英] What does"one-past-the-last-element" mean in vectors?
问题描述
我正在学习向量,并且对数组如何复制到这里的向量感到困惑
I'm learning vectors and am confused on how the array is copying to thevector here
double p[] = {1, 2, 3, 4, 5};
std::vector<double> a(p, p+5);
我也知道 std :: vector< double>a(3,5);
的意思是为3腾出空间并用5初始化它们".
I also know std::vector<double> a(3,5);
means `make room for 3 and initialize them with 5. How does the above code work?
第二点是我从复制上面的代码的地方阅读了该段.
The second point is I read the paragraph from where I copied the above code.
使用向量或任何其他标准容器.受控顺序始终是以[first,last-past-last)的形式表示-不仅适用于ctor,而且适用于对于在一系列元素上运行的每个函数也是如此.
Understanding the second point is crucial when working with vectors or any other standard containers. The controlled sequence is always expressed in terms of [first, one-past-last)—not only for ctors, but also for every function that operates on a range of elements.
我不知道 [第一个,最后一个)
是什么意思?我在数学上知道,但是不知道向量为什么/如何以这种方式复制数组?
I don't know what is the meant by [first, one-past-last)
?
I know mathematically but don't know why/how vector copy the array in this way?
已编辑
另一个相关问题
成员函数
end()
返回一个迭代器,该迭代器指向"序列中的last-last-element
.请注意,取消引用由end()
返回的迭代器是非法的,并且结果不确定.
The member function
end()
returns an iterator that "points" toone-past-the-last-element
in the sequence. Note that dereferencing the iterator returned byend()
is illegal and has undefined results.
您能解释一下最后一个元素
是什么吗?为什么?
Can you explain this one-past-the-last-element
what is it? and why?
推荐答案
请勿从STL容器中取消引用 end()
或 rend()
,因为它们不会指向有效元素.
下面的图片可以帮助您形象化.
Never dereference end()
or rend()
from STL containers, as they do not point to valid elements.
This picture below can help you visualize this.
半开范围的优势是:
1.当begin()== end()
时,将处理空范围2.通过检查直到迭代器等于end(),可以直观地完成元素的迭代.
The advantage of an half open range is:
1. Handling of empty ranges occur when begin() == end()
2. Iterating over the elements can be intuitively done by checking until the iterator equals end().
这篇关于什么是“最后一个元素"?在向量中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!