Using accumulator for strings

Apr 5, 2011 at 4:25pm
For starter this is my first language and I am taking a class in C++. We have a homework assignment that asks use to input strings and draw a box and places the strings in the center. I would like to use an accumulator but the book does not provide anexample of it and this week I am not able to make it to my professor's office hours.

I have tried to goggle it and have looked at this http://www.cplusplus.com/reference/std/numeric/accumulate/

but i still need a little clarity.
My question is what is the proper way to use it and what is the correct syntax, meaning just like getline would look like this getline(cin, string), how would an accumulator look like? Or what parts does it consist of?

I apologize if the question is too vague or difficult to read but I did not know how else to ask it. Any help is greatly appreciated. Thanks in advance.
Apr 5, 2011 at 4:38pm
I'm not sure in what way you're trying to use accumulate for your problem.
As shown in the example on the reference, you can use it to e.g. sum a range of numbers in an array.

You could also use it to concatenate some strings:
1
2
string strs[]={"Hello ","World","!"};
cout << accumulate(strs,strs+3,string());


accumulate takes an iterator range like many other algorithms in the standard library.
I'm not sure if you've covered iterators yet, but they're essentially objects that you can dereference and advance (meaning calling operator++ on it) and are used to iterate over a range of values. So regular pointers also count as iterators.
Apr 5, 2011 at 4:41pm
I am trying have the user input as many string as they would like, and have the accumulate store them so I can check which string is the longest to help determine the size of the box I need. And no we have not covered iterators yet.
Last edited on Apr 5, 2011 at 4:41pm
Apr 5, 2011 at 4:42pm
You don't want an accumulator, then. You just need a container. Store them in a vector. http://www.cplusplus.com/reference/stl/vector/
Apr 5, 2011 at 4:45pm
I will see if he will let us use vector, since we have not covered vectors
Apr 5, 2011 at 4:51pm
Thank you for your help.

Just to know could you use a accumulator to store the strings and then use find to find \n to calculate how many characters are in each string entered.
Apr 5, 2011 at 4:54pm
You could, but it would be a clumsy way of doing it, since you don't need to concatenate all those strings to store them and figure out which one is the longest.
Apr 5, 2011 at 4:58pm
If you want to determine the longest string using an algorithm in the standard library, you can use max_element:

1
2
3
4
5
6
7
bool string_length_lt(const string& str1,const string& str2) {return str1.length()<str2.length();}
int main()
{
  vector<string> lines;
  //read user input
  int maxLength=max_element(lines.begin(),lines.end(),string_length_lt)->length();
}
Last edited on Apr 5, 2011 at 5:00pm
Apr 5, 2011 at 5:04pm
I figure it would be a clumsy way, but since I am new to this I only have limited amount of information to work with, and since we are beginning to work with accumulator I figure we need to use them.
Apr 5, 2011 at 6:50pm
Thanks, everyone for their input
Topic archived. No new replies allowed.