Accumulate, copy, vector: push_back (STL sample)

Source: Internet
Author: User

Accumulate, copy, vector: push_back (STL sample)
The sample code below uses strates how to use the accumulate, copy, and vector: push_back STL functions in Visual C ++.
Required header: <numeric>
Prototype:
Template <class inputiterator, class _ type> inline
_ Type accumulate (inputiterator first, inputiterator last, _ type init)

Template <class inputiterator, class _ type, class binaryoperator> inline
_ Type accumulate (inputiterator first, inputiterator last, _ type init, binaryoperator binary_op)

Note: The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.
Description: The accumulate function initializes an accumulator ACC with an initial value init and then modifies it with ACC = ACC + * I or ACC = binary_op (ACC, * I) for every iterator I in the range [first, last) in order. normally, the accumulate function is used to sum the numeric elements of a vector. however, it can also be used to do other useful work such as concatenating a vector of strings.
Sample Code:
//////////////////////////////////////// ///////////////////////////////
//
// Compile options needed:/GX
//
// Accumulate. cpp: demonstrates the use of accumulate ()
//
// Description of accumulate (first, last, init)
// Accumulate (first, last, init, binary_op ):
//
// Initializes the accumulator ACC with the initial value init
// ACC = init
// And then modifies it
// ACC = ACC + * I
// Or
// ACC = binary_op (ACC, * I)
// For every iterator I in the range [first, last) in order.
//////////////////////////////////////// ///////////////////////////////

// Turn off warning about symbols too long for Debugger
# Pragma warning (Disable: 4786)

# Include <iostream>
# Include <numeric>
# Include <functional>
# Include <vector>
# Include <iterator>
# Include <string>

Using namespace STD;

Typedef vector <float> floatarray;
Typedef vector <string> stringarray;
Typedef ostream_iterator <float, Char, char_traits <char> floatostreamit;

Void main ()
{
// A vector of floats
Floatarray rgfa;

// An ostream iterator that outputs a float to cout terminated
// By a space
Floatostreamit ostreamit (cout ,"");

// Initialize the array to 1, 1/2, 1/3 ,...
For (INT I = 0; I <10; I ++) rgfa. push_back (1.0f/(I + 1 ));

// Print the Array
Copy (rgfa. Begin (), rgfa. End (), ostreamit );
Cout <Endl;

// Sum the Array
Cout <"The sum of 1 + 1/2 + 1/3 +... + 1/10 is"
<Accumulate (rgfa. Begin (), rgfa. End (), 0.0f)
<Endl;

// Compute the product of the array
Cout <"the product of 1*1/2*1/3 *... * 1/10 is"
<Accumulate (rgfa. Begin (), rgfa. End (), 1.0f, multiplies <float> ())
<Endl;

// Initialize array of strings
Stringarray RGS;
RGS. push_back ("this ");
RGS. push_back ("is ");
RGS. push_back ("one ");
RGS. push_back ("sentence .");

// Concatenate the strings in the array and print the sentence
Cout <"the concatenated vector of strings :"
<Accumulate (RGS. Begin (), RGS. End (), string (""))
<Endl;
}

Program output is:
1 0.5 0.333333 0.25 0.2 0.166667 0.142857 0.125 0.111111 0.1
The sum of 1 + 1/2 + 1/3 +... + 1/10 is 2.92897
The product of 1*1/2*1/3 *... * 1/10 is 2.75573e-007
The concatenated vector of strings: This is one sentence.

 

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.