A few basic STL Problems

Hi everyone. I'm working on an assignment for my CS class and I'm having a few issues coming up with how to do my few remaining problems. The assignment specifies that we may not use recursion, loops, or switch statements; only the STL. Any help or hints would be greatly appreciated as I'm out of ideas on how to get the methods to work.

1. Find the total length of all strings in a set. The assignment suggests using the accumulate algorithm. Here's what I have so far:

1
2
3
4
5
6
7
8
int total_string_length( const set<string>& data )
{
  int result = accumulate(data.begin(), data.end(), 0, ...);
  //This is the line I'm having issues with, I thought that a 
  //string.length() for the 4th argument would work but it does not.

  return result;
}


2. Determine the number of lines in a file. The assignment suggests using the count algorithm and istreambuf_iterator<char>. Unfortunately I'm not even sure where to begin with this one, I guess I'm not sure how the istreambuf_iterator works and how to use it.


Thanks for any help that anyone is willing to give.
Last edited on
For the first problem, check out
http://www.sgi.com/tech/stl/accumulate.html

For the second problem, check out
http://www.sgi.com/tech/stl/istream_iterator.html

Especially look at the examples. They are close to what you need.
Thanks, that helped tremendously.
Topic archived. No new replies allowed.