pr23015.cpp
来自「《标准C++宝典》源码」· C++ 代码 · 共 49 行
CPP
49 行
////////////////////////////////////////
// File Name: pr23015.cpp
////////////////////////////////////////
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
////////////////////////////////////////
// The function to be called by
// for_each().
////////////////////////////////////////
void show_val(int val)
{
std::cout << val << std::endl;
}
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
// Create the vector objects.
std::vector<int> intVector1;
std::vector<int> intVector2(5);
// Populate the vector.
for (int x=2; x<7; ++x)
intVector1.push_back(x);
// Display the contents of the vector.
std::cout << "Contents of vector1: " << std::endl;
std::for_each(intVector1.begin(),
intVector1.end(), show_val);
std::cout << std::endl;
// Calculate the partial sum.
std::partial_sum(intVector1.begin(),
intVector1.end(), intVector2.begin());
// Display the contents of the resultant vector.
std::cout << "Contents of vector2: " << std::endl;
std::for_each(intVector2.begin(),
intVector2.end(), show_val);
std::cout << std::endl;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?