Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming that <code>X</code> is the type of the objects in the list, then you can do something like this:</p> <pre><code>void DoSomething(X&amp; object) { object.count, object.avg; } void DoSomethingElse(int&amp; count, int&amp; average) { ... } int main() { ... for(std::list&lt;X&gt;::iterator it=myList.begin(), end=myList.end(); it != end; ++it) { DoSomething(*it); // how I'd do it DoSomethingElse(it-&gt;count, it-&gt;avg); // Equally valid way that you did it } ... } </code></pre> <p>Just remember:</p> <ul> <li><code>container.begin()</code> is the a pointer to the first element</li> <li><code>container.end()</code> is a pointer to one-past-the-last element</li> <li><code>*it</code> is a reference to the pointed-to element</li> <li><code>it != container.end()</code> is how you tell if you are at the end</li> <li><code>it-&gt;x</code> is a member of the pointed-to element</li> <li>removing an object from a container might invalidate outstanding iterators, depending upon several factors.</li> <li><code>++it</code> is probably more efficient than <code>it++</code></li> </ul> <hr> <p><strong>EDIT</strong>: OP asks:</p> <blockquote> <blockquote> <p>I'm not iterating through the list and running calc_average on every single node, but rather iterating through the list looking for a specific item value. Once I find the one of interest, I'm calling the calc_average function on that specific node. I just don't need to have the for loop. Instead I would arrive at my desired iterator, and pass that via *it to void DoSomething ?</p> </blockquote> </blockquote> <p>I think you understand how it works now. You'd have some code to search for the indicated node, and then some other code to invoke your function:</p> <pre><code> std::list&lt;X&gt;::iterator it, end; for(it=myList.begin(), end=myList.end(); it != end; ++it) { // Look for the special node: if( it-&gt;magicValue == 42 ) { // We found it! break; } } // Either it is equal to end (boo!) or it points to the special node (yay!) if( it == end ) { std::cerr &lt;&lt; "Could not find special node!\n"; } if( it != end ) { DoSomething(*it); } </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