是否可以为可以从vector :: begin()返回的私有向量成员(“_First”)迭代器创建引用?

时间:2021-07-27 22:50:57

I tried to create a referenced pointer to a vector data object, but I couldn't. It's the member iterator _First; it has its own function vector::begin() which can return itself.

我试图创建一个指向矢量数据对象的引用指针,但我不能。它是成员迭代器_First;它有自己的函数vector :: begin(),它可以返回自己。

I have to obtain (achieve) something like this : (it just looks like making a reference)

我必须得到(实现)这样的东西:(它看起来像是一个参考)

 vector<type>::iterator *pit = &vector<type>::_First;

"It may instantly solve the problem but, the member is marked as private".

“它可以立即解决问题,但成员被标记为私人”。

In my opinion, it's called pointer-to-vector-array.

在我看来,它被称为指向矢量数组。

Is there any magical trick or code which can do something similar like this? Any help would be very greatly appreciated.

是否有任何神奇的技巧或代码可以做类似这样的事情?任何帮助将非常感激。

EDIT : I removed lots of context; Just question. If you need more information, please feel free to comment. :)

编辑:我删除了很多上下文;只是问题。如果您需要更多信息,请随时发表评论。 :)

3 个解决方案

#1


2  

The solution is not to use a pointer-to-iterator. An iterator is already, effectively, a pointer. Just return it by value.

解决方案不是使用指针迭代器。迭代器实际上已经是一个指针。只需按值返回即可。

#2


2  

You say you need a pointer to an iterator... but that's quite confusing and rarely needed.

你说你需要一个指向迭代器的指针......但这很混乱,很少需要。

Let's see your solutions, one at a time:

让我们一次看一个你的解决方案:

Solution 1:

vector<type>::iterator *it = &vector<type>::_First;

Not valid because _First is private, and also you have to use the vector_object, not the class name.

无效,因为_First是私有的,并且您还必须使用vector_object,而不是类名。

Solution 2:

vector<type>::iterator *it = &(vector<type>::begin());

Not valid because begin() is not a static method, so you have to use the object name, not the class name:

无效,因为begin()不是静态方法,因此您必须使用对象名称,而不是类名:

vector<type>::iterator *it = &(vector_object.begin());

But even then, not valid because begin returns an r-value, and you cannot get the address of such an expression. You have to use an auxiliary variable for that, which will be an l-value.

但即使这样,也无效,因为begin返回一个r值,而你无法得到这样一个表达式的地址。你必须使用一个辅助变量,它将是一个l值。

vector<type>::iterator *it = &&(vector<type>[0]);

Same as above about the staticness of member functions. It should be:

与上面相同的成员函数的静态性。它应该是:

vector<type>::iterator *it = &&(vector_object[0]);

But even then, to be fair, this code is crazy. && is the logical AND operator. You cannot apply operator & (address-of) twice in a row because it has to be applied to an l-values but it returns an r-value. Moreover operator[] returns a value of type not an iterator.

但即便如此,公平地说,这段代码很疯狂。 &&是逻辑AND运算符。您不能连续两次应用operator&(address-of),因为它必须应用于l值但返回r值。而且operator []返回的类型不是迭代器。

Solution 3:

This compiles but renders undefined behavior because you are returning the address of a local variable that is destroyed once you return from the function. Returning the address of a local variable is always an error, and most compilers will issue a warning about it.

这会编译但呈现未定义的行为,因为您返回从函数返回后销毁的局部变量的地址。返回局部变量的地址始终是一个错误,大多数编译器都会发出警告。

What you have to do is to keep the variable around as long as you need the pointer:

你需要做的是只要你需要指针就保持变量:

vector<type>::iterator it = vector_object.begin();
vector<type>::iterator *pit = &it;
// use pit, but keep it around

That is, your pointer is valid only as long as it variable exists. That's not something specific to iterators, but to pointers in general.

也就是说,只有存在变量,指针才有效。这不是迭代器特有的东西,而是一般的指针。

The big question here is: why on earth do you need a pointer to an iterator? I've been programming STL for a very long time and I have never needed a pointer to an iterator before (pointers-to-pointers yes, even pointers-to-pointers-to-pointers, but pointers-to-iterators... never).

这里最大的问题是:为什么你需要一个指向迭代器的指针?我已经编程STL很长一段时间了,我从来没有需要一个指向迭代器的指针(指针指向是的,甚至是指针到指针指针,但指针到迭代器......决不)。

#3


0  

You want to get a pointer to internal array owned by a vector? So you could just do:

你想获得一个指向矢量所拥有的内部数组的指针吗?所以你可以这样做:

type* t = &vec[0];

Getting a pointer to the iterator, using internal vector's structure is not good, even if it wasn't failing because of private members. Internal structure of the vector object is not the same in all STL implementations, I think that the only thing you can be sure of is that the memory uses underlying array of continuous memory...

使用内部向量的结构获取指向迭代器的指针并不好,即使它没有因为私有成员而失败。矢量对象的内部结构在所有STL实现中都不一样,我认为你唯一能确定的是内存使用底层连续内存数组......

#1


2  

The solution is not to use a pointer-to-iterator. An iterator is already, effectively, a pointer. Just return it by value.

解决方案不是使用指针迭代器。迭代器实际上已经是一个指针。只需按值返回即可。

#2


2  

You say you need a pointer to an iterator... but that's quite confusing and rarely needed.

你说你需要一个指向迭代器的指针......但这很混乱,很少需要。

Let's see your solutions, one at a time:

让我们一次看一个你的解决方案:

Solution 1:

vector<type>::iterator *it = &vector<type>::_First;

Not valid because _First is private, and also you have to use the vector_object, not the class name.

无效,因为_First是私有的,并且您还必须使用vector_object,而不是类名。

Solution 2:

vector<type>::iterator *it = &(vector<type>::begin());

Not valid because begin() is not a static method, so you have to use the object name, not the class name:

无效,因为begin()不是静态方法,因此您必须使用对象名称,而不是类名:

vector<type>::iterator *it = &(vector_object.begin());

But even then, not valid because begin returns an r-value, and you cannot get the address of such an expression. You have to use an auxiliary variable for that, which will be an l-value.

但即使这样,也无效,因为begin返回一个r值,而你无法得到这样一个表达式的地址。你必须使用一个辅助变量,它将是一个l值。

vector<type>::iterator *it = &&(vector<type>[0]);

Same as above about the staticness of member functions. It should be:

与上面相同的成员函数的静态性。它应该是:

vector<type>::iterator *it = &&(vector_object[0]);

But even then, to be fair, this code is crazy. && is the logical AND operator. You cannot apply operator & (address-of) twice in a row because it has to be applied to an l-values but it returns an r-value. Moreover operator[] returns a value of type not an iterator.

但即便如此,公平地说,这段代码很疯狂。 &&是逻辑AND运算符。您不能连续两次应用operator&(address-of),因为它必须应用于l值但返回r值。而且operator []返回的类型不是迭代器。

Solution 3:

This compiles but renders undefined behavior because you are returning the address of a local variable that is destroyed once you return from the function. Returning the address of a local variable is always an error, and most compilers will issue a warning about it.

这会编译但呈现未定义的行为,因为您返回从函数返回后销毁的局部变量的地址。返回局部变量的地址始终是一个错误,大多数编译器都会发出警告。

What you have to do is to keep the variable around as long as you need the pointer:

你需要做的是只要你需要指针就保持变量:

vector<type>::iterator it = vector_object.begin();
vector<type>::iterator *pit = &it;
// use pit, but keep it around

That is, your pointer is valid only as long as it variable exists. That's not something specific to iterators, but to pointers in general.

也就是说,只有存在变量,指针才有效。这不是迭代器特有的东西,而是一般的指针。

The big question here is: why on earth do you need a pointer to an iterator? I've been programming STL for a very long time and I have never needed a pointer to an iterator before (pointers-to-pointers yes, even pointers-to-pointers-to-pointers, but pointers-to-iterators... never).

这里最大的问题是:为什么你需要一个指向迭代器的指针?我已经编程STL很长一段时间了,我从来没有需要一个指向迭代器的指针(指针指向是的,甚至是指针到指针指针,但指针到迭代器......决不)。

#3


0  

You want to get a pointer to internal array owned by a vector? So you could just do:

你想获得一个指向矢量所拥有的内部数组的指针吗?所以你可以这样做:

type* t = &vec[0];

Getting a pointer to the iterator, using internal vector's structure is not good, even if it wasn't failing because of private members. Internal structure of the vector object is not the same in all STL implementations, I think that the only thing you can be sure of is that the memory uses underlying array of continuous memory...

使用内部向量的结构获取指向迭代器的指针并不好,即使它没有因为私有成员而失败。矢量对象的内部结构在所有STL实现中都不一样,我认为你唯一能确定的是内存使用底层连续内存数组......