accumulate, copy, vector::push_back(STL Sample)
来源:互联网 发布:场矩阵布列松陈 编辑:程序博客网 时间:2024/05/16 16:17
accumulate, copy, vector::push_back(STL Sample)
The sample code below illustrates 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 with
// 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.
- accumulate, copy, vector::push_back(STL Sample)
- GNU STL vector push_back insert
- STL vector中的push_back方法(17)
- STL algorithm accumulate vector<string> demo
- vector push_back
- vector::push_back
- STL-----------accumulate
- STL:accumulate
- STL accumulate
- 研究STL之vector的push_back函数解析
- STL中vector容器begin(),end(),push_back()等相关函数
- stl的vector和list的push_back效率比较
- STL:vector中push_back实现斐波纳契数列计算
- Sample of STL(Vector):begin and rbegin
- vector 的 push_back[转]
- vector::push_back函数解析
- 关于 vector push_back 实现
- vector的push_back
- gdb查看内存
- 在 Android 中使用 SQLite, ContentProvider
- 在Winform 中拖动控件
- 该做什么样的选择
- oracle建立学生,课程以及选课表并进行插入查询操作
- accumulate, copy, vector::push_back(STL Sample)
- 大话设计模式----装饰模式笔记
- 内核对象
- integrated Windows authentication is not enabled
- C# WinForm控件的拖动和缩放的实现
- 【TCP-IP详解卷一:协议】一、概述
- div图片img与div容器下有距离的处理办法
- JavaScript prototype属性与修改对象
- FireFox下载文件时文件名乱码的解决办法