在c / c ++项目中使用指针指针和/或引用指针最有用的时间是什么? [英] What was the most useful time you used a pointer-to-pointer and/or reference-to-pointer in a c/c++ project?

查看:95
本文介绍了在c / c ++项目中使用指针指针和/或引用指针最有用的时间是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指针指针和指针引用似乎过于复杂,特别是对于新手。在你使用的c / c ++项目中使用一个最好的和最有用的原因是什么?

Pointers-to-pointers and references-to-pointers seem overly complicated at first, especially for newbies. What is the best and most useful reason you used one in a c/c++ project you worked on?

这可以帮助人们更好地了解指针以及如何有效使用指针。

This can help people better understand pointers and how to use them effectively.

编辑:包括C作为C ++

Edited: Included C as well as C++

推荐答案

因为没有人提到这个,它们可能作为迭代器。

Since no one has mentioned this yet, they may be useful as iterators.

假设你有一些对象的列表。你需要以一些特定的顺序遍历它们中的一些。

Assume you have a list of objects of some sort. And you need to traverse some of them in some specific order.

所以你创建一个数组,保存指向你需要遍历的对象,然后对数组进行排序。

So you create an array holding pointers to the objects you need to traverse, and then you sort that array.

此数组中的迭代器是指向指针的指针。

An iterator into that array is a pointer to a pointer.

使用 std :: sort 需要一对迭代器,这意味着它需要一对指针指针。

Sorting it with std::sort requires a pair of iterators, which means that it requires a pair of pointers to pointers.

然后遍历它 std :: for_each 也需要一对迭代器。

And then traversing it with std::for_each also requires a pair of iterators.

这是我上次使用的指针指针的上下文。 (我实际上有一个最后一层抽象,所以我最后指向指针的指针,但这可能更不寻常)

That's the context in which I last used pointers to pointers. (I actually had one final layer of abstraction so I ended up with pointers to pointers to pointers, but that's probably more unusual)

这篇关于在c / c ++项目中使用指针指针和/或引用指针最有用的时间是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆