容器的分配函数可能会溢出吗?
我今天遇到了这个问题,我认为我应该将其发布以供社区参考和/或意见。
标准 C++ 容器向量、双端队列、列表和字符串提供 assign
成员函数。有两个版本;我主要对接受迭代器范围的那个感兴趣。 Josuttis 书的描述有点含糊。从页。 237...
对 [beg,end) 范围内的所有元素进行赋值;也就是说,用 [beg,end) 元素的副本替换所有现有元素。
它没有说明如果受让人容器的大小与分配的范围不同会发生什么。它会被截断吗?会自动扩展吗?这是未定义的行为吗?
I ran into this question today and thought I should post it for the community's reference and/or opinions.
The standard C++ containers vector, deque, list, and string provide an assign
member function. There are two versions; I'm primarily interested in the one accepting an iterator range. The Josuttis book is a little ambiguous with its description. From p. 237...
Assigns all elements of the range [beg,end); this is, is replaces all existing elements with copies of the elements of [beg,end).
It doesn't say what happens if the size of the assignee container is different from the range being assigned. Does it truncate? Does it automagically expand? Is it undefined behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我发现的。事实证明我不必担心默默地做错事。标准再次给出了答案。来自第 23.2.6.1 节:
所以它实际上只是一个
clear()
的快捷方式,后跟一个完整范围的insert
。Here's what I found. It turns out I didn't have to worry about silently doing the wrong thing. Once again, the standard has the answer. From section 23.2.6.1:
So it's really just a shortcut for a
clear()
followed by aninsert
of the full range.