Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you'd accept that your loop is slightly changed, it is certainly doable:</p> <pre><code>for (auto item : myIterationClass(vector_var)) { item.index(); item.value(); item.erase(); item.replaceWith(42); } </code></pre> <p>The idea is to have <code>myIterationClass()</code> be a thin wrapper which return iterators with a fairly custom value. The use of <code>erase()</code> is a bit problematic, though, as you are not supposed to change the container while it is being iterated, i.e., if these are really needed it is necessary to record the elements to be erased and process them later.</p> <p>Although I don't this this is a good idea, below is a quick demo implementing <code>index()</code> and <code>value()</code>. Implementing <code>replaceWith()</code> would be trivial while implementing anything mutating the length of the sequence could be interesting. Given that the iterator controls the sequence it could probably be done by directly mutating the underlying sequence and adjusting the kept index appropriately. Note that there are also different approach how the iterators are represented. I randomly choose to use a combination of a pointer to the container and an index. If the sequence doesn't add or remove elements, it could also be done using two iterator and computing the index as the difference between the two.</p> <pre><code>#include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;vector&gt; template &lt;typename T&gt; class wrapped_iterator { T* container; typename T::size_type position; public: wrapped_iterator(T* container, typename T::size_type position) : container(container) , position(position) { } wrapped_iterator&lt;T&gt;&amp; operator*() { return *this; } wrapped_iterator&lt;T&gt;&amp; operator++() { ++position; return *this; } wrapped_iterator&lt;T&gt; operator++(int) { wrapped_iterator&lt;T&gt; rc(*this); ++*this; return rc; } bool operator== (wrapped_iterator&lt;T&gt; const&amp; other) const { return position == other.position; } bool operator!= (wrapped_iterator&lt;T&gt; const&amp; other) const { return !(*this == other); } typename T::size_type index() const { return position; } typename T::const_reference&amp; value() const { return (*container)[position]; } }; template &lt;typename T&gt; class wrapped { T* container; public: typedef wrapped_iterator&lt;T&gt; iterator; wrapped(T&amp; container): container(&amp;container) {} iterator begin() const { return iterator(container, 0u); } iterator end() const { return iterator(container, container-&gt;size()); } }; template &lt;typename T&gt; wrapped&lt;T&gt; wrapper(T&amp; container) { return wrapped&lt;T&gt;(container); } int main() { std::vector&lt;int&gt; v{ 7, 6, 5, 4, 3, 2, 1 }; for (auto item : wrapper(v)) { std::cout &lt;&lt; "index=" &lt;&lt; item.index() &lt;&lt; ' ' &lt;&lt; "value=" &lt;&lt; item.value() &lt;&lt; '\n'; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload