-1

I am trying to complete a homework assignment I have, which needs to be completed using only the algorithm library in c++, without any custom loops of any sort. I need to populate a string type vector called m_Players from class Player, from an input file that contains a couple of names, and I am having trouble identifying which functions I need to use from the algorithm library to achieve that, and how to syntactically use them to reach my goal.

std::vector<Player> m_Players;
void Glicko::LoadPlayers(std::istream& is)
{
        // TODO Load the m_Players variable without using a custom loop
}
  • What specifically is unclear about the syntax for [std::for_each](https://en.cppreference.com/w/cpp/algorithm/for_each)? – walnut Sep 20 '19 at 02:32
  • 1
    For the best Stack Overflow experience, show your attempt at solving the problem along with the output you received and the expected output. It's very hard to show you what you're doing wrong if we can't see what you did wrong. – user4581301 Sep 20 '19 at 02:34
  • @user4581301 well the thing is if I cant figure out this, I wont be getting any outputs, and plus I dont need to show you what I am doing, my question is on how to use one of the already existing functions of the algorithm library because I do not understand the description well enough – trelomania7 Sep 20 '19 at 02:43
  • @uneven_mark well i dont quite understand how to structure the command using the for_each function to fill a string vector reading from an input file. How do I modify this function, for reading a file and filling a vector template constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f) – trelomania7 Sep 20 '19 at 02:46
  • [Is this documentation page easier to follow?](http://www.cplusplus.com/reference/algorithm/for_each/) cppreference is generally higher-quality information, but often that results in harder to read. Cplusplus tries to keep it simple, but it's not always right. Sometimes it's worth starting with Cplusplus and then going to cppreference when something just doesn't work right. – user4581301 Sep 20 '19 at 02:52
  • @trelomania7 There is an example of usage in the last code block on the page I linked. You should already have learned about iterators, general structure of the algorithm library and probably lambdas as well if you are supposed to use `std::for_each`. If you haven't, this is not a good place to teach you. – walnut Sep 20 '19 at 02:57

1 Answers1

1

You may want to consider using an std::istream_iterator. https://en.cppreference.com/w/cpp/iterator/istream_iterator

This would allow you to iterate over Player objects read from the input stream. Since this is a homework assignment, I won't post explicit code, but hopefully the pointer is helpful.

NicholasM
  • 4,557
  • 1
  • 20
  • 47
  • Thank you for your pointer, but it does not ease my confusion. The proplem is not which fucntion to use, but how. How would you write a hypothetical statement using this function if you had to fill a string vector from an input file – trelomania7 Sep 20 '19 at 02:52
  • Thank you for your pointer, I will check it out, but I do not really need to use for_each, it was just an idea I had, we just learned about the algorithm library in class and I have trouble using the fucntions it has – trelomania7 Sep 20 '19 at 03:02
  • I have to use the algorithm library only, if I was able to use the iterator library, that would work things out. – trelomania7 Sep 20 '19 at 03:25
  • couldnt agree more with you, but since I have no control over that, the only thing I can do is follow the rules. To that extend I would still appreciate it if you had any ideas as to help me with this academic bullshit. – trelomania7 Sep 20 '19 at 03:51
  • Once you have a pair of `istream_iterator` objects, you can pass them to an algorithm like `std::for_each`, which you mentioned. Have you tried that? See also: https://en.cppreference.com/w/cpp/iterator/back_insert_iterator – NicholasM Sep 21 '19 at 05:24