Get a pointer to STL container an iterator is referencing?

后端 未结 3 774
感动是毒
感动是毒 2020-12-11 01:06

For example, the following is possible:

std::set s;  
std::set::iterator it = s.begin();

I wonder if the opposite is

相关标签:
3条回答
  • 2020-12-11 01:24

    No. You must remember the container that an iterator came from, at the time that you find the iterator.

    A possible reason for this restriction is that pointers were meant to be valid iterators and there's no way to ask a pointer to figure out where it came from (e.g. if you point 4 elements into an array, how from that pointer alone can you tell where the beginning of the array is?).

    0 讨论(0)
  • 2020-12-11 01:36

    It is possible with at least one of the std iterators and some trickery. The std::back_insert_iterator needs a pointer to the container to call its push_back method. Moreover this pointer is protected only.

    #include <iterator>
    
    template <typename Container>
    struct get_a_pointer_iterator : std::back_insert_iterator<Container> {
        typedef std::back_insert_iterator<Container> base;
        get_a_pointer_iterator(Container& c) : base(c) {}
        Container* getPointer(){ return base::container;}
    };
    
    #include <iostream>
    int main() {
        std::vector<int> x{1};
        auto p = get_a_pointer_iterator<std::vector<int>>(x);
        std::cout << (*p.getPointer()).at(0);
    }
    

    This is of course of no pratical use, but merely an example of an std iterator that indeed carries a pointer to its container, though a quite special one (eg. incrementing a std::back_insert_iterator is a noop). The whole point of using iterators is not to know where the elements are coming from. On the other hand, if you ever wanted an iterator that lets you get a pointer to the container, you could write one.

    0 讨论(0)
  • 2020-12-11 01:44

    No, there is no portable way to do this.

    An iterator may not even have a reference to the container. For example, an implementation could use T* as the iterator type for both std::array<T, N> and std::vector<T>, since both store their elements as arrays.

    In addition, iterators are far more general than containers, and not all iterators point into containers (for example, there are input and output iterators that read to and write from streams).

    0 讨论(0)
提交回复
热议问题