Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:34:44+00:00 2026-06-13T09:34:44+00:00

I’ve got a program that populates a STL linked list with structs, and I’m

  • 0

I’ve got a program that populates a STL linked list with structs, and I’m trying to pass struct members from a node on the list (one that I’m currently on via an iterator). One of the things I’m trying to accomplish is a function that will calculate a running average. Rather than storing the count and total in the struct, and then computing the average upon output, I’d like to store the count and average in the struct, thus discarding my quantity value after the average has been recalculated. My struct looks like this:

struct mystruct 
{
    string item;
    long avg;
    short count;
} data;

These structs are stored in a list, with an iterator, it, that allows me to move through the list. Would this be the proper way to call my average function, provided I’ve traversed through the list and it equals the node who’s data I want to calculate the average with?

// prior to running the code below, the `count` and `avg` members for the 
// struct at iterator location `it` are both 1 and 100 respectively

long qty = 50;
calc_average(it->count, it->avg, qty);

cout << "The current count is " << it->count << endl; 
  // Outputs 'The current count is 2'
cout << "The current average is " << it->avg << endl; 
  // Outputs 'The current average is 75'


void calc_average(short &count, long &avg, long quant)
{
    avg = ( (avg * count) + quant ) / (count + 1);
    count++;
}

Does this seem correct? I’m trying to implement this using the STL list, but it seems more confusing than just implementing my own linked list class. I think I’m just getting confused with the structure and how the iterators actually work, and what/how things are actually being passed around. Coding is still fairly new to me so much of this is a learning process…

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:34:45+00:00Added an answer on June 13, 2026 at 9:34 am

    Assuming that X is the type of the objects in the list, then you can do something like this:

    void DoSomething(X& object) {
      object.count, object.avg;
    }
    void DoSomethingElse(int& count, int& average) {
      ...
    }
    
    int main() {
       ...
       for(std::list<X>::iterator it=myList.begin(), end=myList.end(); it != end; ++it) {
         DoSomething(*it); // how I'd do it
         DoSomethingElse(it->count, it->avg); // Equally valid way that you did it
       }
       ...
    }
    

    Just remember:

    • container.begin() is the a pointer to the first element
    • container.end() is a pointer to one-past-the-last element
    • *it is a reference to the pointed-to element
    • it != container.end() is how you tell if you are at the end
    • it->x is a member of the pointed-to element
    • removing an object from a container might invalidate outstanding iterators, depending upon several factors.
    • ++it is probably more efficient than it++

    EDIT: OP asks:

    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 ?

    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:

       std::list<X>::iterator it, end;
       for(it=myList.begin(), end=myList.end(); it != end; ++it) {
         // Look for the special node:
         if( it->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 << "Could not find special node!\n";
       }
       if( it != end ) {
          DoSomething(*it);
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have a small JavaScript validation script that validates inputs based on Regex. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.