Obtain iterator from pointer or reference
<p>I would like to know if it is possible to obtain an iterator to an object inside a container (e.g. <code>std::vector&lt;...&gt;</code>) by only having access to the object inside the container, e.g. through a reference (which implies we have access to a pointer to it using the <code>&amp;</code> operator). For example, normally we declare an iterator as</p>

<pre><code>std::vector&lt;int&gt;::iterator = vec.begin();
</code></pre>

<p>or </p>

<pre><code>std::vector&lt;int&gt;::iterator = next(vec.begin(), idx);
</code></pre>

<p>but in the first example we are most probably about to iterate through the container, in order, while in the second example we know the index of the object we require. I would like to know if we can obtain the iterator to an object without knowing at which index it resides in the container, but if we do have a reference or a pointer to it, as explained above.</p>

<p>It might appear that this question has already been asked <a href="https://stackoverflow.com/questions/10209065/get-iterator-from-pointer">here</a>, but it seems more like the OP wanted others to fix his code, rather than answering the general question, so the answers are not so satisfactory in my opinion. Also, the answer <a href="https://stackoverflow.com/a/27948422">here</a> seems to say that we can initialize an iterator with a constructor, as shown below</p>

<pre><code>std::vector&lt;int&gt;::iterator it(...);
</code></pre>

<p>but I have not been able to find any evidence of a constructor for the std::iterator class in the official documentation (and neither have I been able to find any documentation on std::vector&lt;...>::iterator) so I am wary to use the constructor shown above, even if it compiles.</p>

<p><strong>NOTE</strong></p>

<p>I use <code>std::vector</code> as an example above, but ideally I would like this to work for any container, e.g. <code>std::list</code> or <code>std::deque</code></p>