CHAPTER11-30.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:1k
- //文件名: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(""));
- }