Accumulate of "turn" STL template

Source: Internet
Author: User

See Chao Shin code has this thing, went to check, ORZZLD, the following is reproduced.

=============================================================

The algorithm is defined in the numeric header file .

Assuming that VEC is a vector object of type int, the following code:

1 // sum the elements in VEC starting the summation with the value 2 int );

Set sum to the element of VEC plus 42.

The accumulate has three formal parameters : The first two parameters specify the range of elements to accumulate , and the third parameter is the cumulative initial value .

The accumulate function sets one of its internal variables to the specified initial value, and then accumulates the values of all elements in the input range on this initial. The accumulate algorithm returns the cumulative result whose return type is the type of its third argument .

The third parameter that specifies the cumulative starting value is necessary because accumulate is ignorant of the type of element to be added, and there is no other way to create an appropriate starting value or associated type.

The fact that accumulate is ignorant of the types of elements to be added has two meanings. First, you must pass an initial value when calling the function, otherwise accumulate will not know what initial value to use. Second, the element type inside the container must match the type of the third argument, or it can be converted to a type of the third argument. Within accumulate, the third argument is used as the starting point for the accumulation, and the elements within the container are sequentially added to the synthesis. Therefore, the element type must be able to be added to the sum type.

Assuming that V is an object of type vector<double>, call Accumulate<v.begin (), V.end (), 0> is there a mistake? If so, where is the mistake?

There is no error from the function call.
The criteria that must be met to invoke the accumulate function include: the element type inside the container must match the type of the third argument, or the type that can be converted to the third argument. the third argument in the above call is of type int, and the element in the vector object is of type double and can be converted to an int type.

But the result of the calculation is inaccurate. Because converting a double type to an int type truncates the fractional part, the resulting summation is the sum of the integer parts of each element, which is a value of type int, which has a larger error than the sum of the actual element values.

Consider the following example, you can use accumulate to connect the elements in a string vector container.

1 // concatenate elements from V and store in sum 2 string string (""));

The effect of this function call is to start with an empty string and concatenate each element in the VEC into a string.

Accumulate of "turn" STL template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.