c++ std標準庫 演算法<algorithm> for_each()用法
阿新 • • 發佈:2020-12-22
技術標籤:# 4.1 C++演算法algorithmfor_eachstdc++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 給vect賦值
template <class T>
void FillValue(T& vect, int first, int last)
{
if (last >= first)
{
for (int i = first; i <= last; ++i)
vect. insert(vect.end(), i);
}
else
{
cout << " The indexes is error: last < first. " << endl;
}
}
// 列印
void print(int elem)
{
cout << elem << " ";
}
void main()
{
vector <int> myvector;
FillValue(myvector, 1, 10);
for_each(myvector.begin(), myvector. end(), print);
cout << endl;
}