Read text file into list without using direct while loop using std::list c++

I have a program that reads a list of assignments and removes the "bad" assignments, then writes into a new file. My function that removes the bad assignments works correctly. The code I currently am using to read the text file works correctly also, but is doing so with a while loop. How can I utilize the std::list library to accomplish the same thing without the while loop? Below is the code I am using for the removing of the bad assignments (Prune) along with reading the text file.

std::string m_Name;
std::list<Assignment> m_Assignments;

void Prune()
{
m_Assignments.remove_if([](const Assignment& assignment){ return !assignment.IsGood();});

}

void Read(std::istream& is)
{
std::string s;

std::getline(is, s);
m_Name = s;

Assignment a;
while (is >> a)
{
m_Assignments.push_back(a);
}

}
Thank you in advance.
1
2
using it = std::istream_iterator<Assignment>; 
std::copy(it{my_input_stream}, it{}, std::back_inserter(my_list)); 
Last edited on
Why so complicated?
Just read the assignments one by one and if it is not bad write to the output file.
No point in storing them in a list if you don't do anything else with them.
This ended up working for me thanks!

std::istream_iterator<Assignment> start(is);
std::istream_iterator<Assignment> stop;
std::copy(start,stop,std::back_inserter(m_Assignments));

and to answer Thomas1965's question, I had to do it this way for the assignment. I originally did it the easier way you mentioned but have to use a list.
Topic archived. No new replies allowed.