Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
今天看到如下代码:
options.push_back(&*i);
i迭代器在哪里,容器options存储指向*i. 结果&*i有点难看,我开始怀疑:
i
options
*i
&*i
添加转换运算符以转换为 的指针是个好主意*i吗?
你怎么看?
这并不难看,但可能很危险:如果迭代器的源i是动态分配的并在 container 之前被删除options,这将导致容器无效指针。 如果不是,这是完全有效的代码。为了使其更具可读性,您可以添加大括号:
options.push_back(&(*i));
如果您没有在整个代码中使用此构造,我不会在这里使用转换运算符。