chapter11-30.cpp

来自「STL程序员开发指南源码」· C++ 代码 · 共 31 行

CPP
31
字号
//文件名:CHAPTER11-30.cpp
#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 ()
{
    FloatArray rgFA;    // a vector of floats
    FloatOstreamIt OstreamIt(cout," ");
    for (int i=0; i<10; i++) rgFA.push_back(1.0f/(i+1));
    copy(rgFA.begin(),rgFA.end(),OstreamIt);
    cout << endl;
    cout << "The sum of 1 + 1/2 + 1/3 + ... + 1/10 is "<< accumulate(rgFA.begin(),rgFA.end(),0.0f) << endl;
    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. ");
cout << "The concatenated vector of strings: " << accumulate(rgs.begin(),rgs.end(),string(""));
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?