- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
书是对的,我只是看错了一行。
正如 answer by @uneven_mark 明确指出的,以下问题取决于我的误读。
在阅读 Josuttis 的 C++ 标准库(第 2 版) 时,我以某种方式确信第 457 页的 coll
被声明为 std::deque
(相反,它被声明为 std::list
!),因此我问了这个问题。
希望能为读者提供思考的食粮。
原始问题:
在“The C++ Standard Library (2nd edition)”的第 456 页,Josuttis 评论道,在调用之前
copy(coll.begin(), coll.end(), back_inserter(coll));
在 std::vector
类的 coll
上,您必须确保 coll
有足够的空间(在这种情况下,它有容量
至少是其大小
的两倍),否则
the algorithm invalidates the passed source iterators while running.
相反,在第 458 页,他没有说任何类似的情况
copy(coll.begin(), coll.end(), front_inserter(coll));
应用于 std::deque
类的 coll
,尽管在第 286 页,以下是关于 std::deque< 的指定
容器:
[...] when elements are inserted at the front or the back. In this case, references and pointers to elements stay valid, but iterators don’t.
因此我怀疑。 (是的,我知道 std::deque
甚至不提供类似 reserve
的成员函数。)
只要我理解 this answer ,我的理解就是 front_inserter(coll)
迭代器可以导致指针数组的重新分配(这是一种合法的方式实现std::deque
),并且不能导致存储coll
实际元素的数组的重新分配,从而留下引用/指向元素的指针有效,同时使迭代器
无效,其正确行为(我正在考虑如何实现operator++
)依赖于指针数组和指向数组。
如果这是真的,那么我猜对应于 copy
的参数 coll.begin()
的参数在分配给它的那一刻会失效导致指针数组的重新分配。
最佳答案
本书第455/456页介绍了std::back_inserter
,第457/458页介绍了std::front_insert
。每种情况都有一个简短的解释,包括适用容器的列表。每个部分都有一个代码片段作为示例,仅选择一个适用的容器来举例说明用法。
对于std::back_inserter
,由于选择了容器std::vector
,并且代码片段中的注释提到,为什么需要先在其中预留足够的空间 vector 。
对于 std::front_inserter
,作者选择了 std::list
,而不是 std::deque
。 std::list
不会在插入时使引用或迭代器无效,因此
copy(coll.begin(), coll.end(), front_inserter(coll));
很好,请参阅当前 C++ 草案的 [list.modifiers]/1。
因此,在这两种情况下,作者的代码都没有错误。我想他从来没有打算完全解释复制到容器本身的危险,而是简单地选择了这些案例,因为它允许他编写更短的完整用法示例。
我认为对于 coll
是 std::deque
的情况,这显然是未定义的行为。 std::front_inserter
通过调用 push_front
(见 [front.insert.iter.ops]/2 )插入元素,这使所有迭代器无效(见 [deque.modifiers]/1 ):
同时 std::copy
的行为是 [alg.copy]/4 :
Effects: Copies elements in the range [first, last) into the range [result, result + N) starting from first and proceeding to last. For each non-negative integer n < N, performs *(result + n) = *(first + n).
第一次插入后,first
失效,会导致未定义的行为。
关于c++ - 如果双端队列不够大,为什么通过 std::copy 成功将 std::deque 对象附加到自身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57646965/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!