1.for_each();定义如下
template<typename InputIterator, typename Function>
Function for_each(InputIterator beg, InputIterator end, Function f) {while(beg != end)
f(*beg++);
}
demo:
void print(int elem)
{
cout << elem << ' ';
}
int main()
{
vector<int> coll;
for(int i = 48; i <= 58; ++i)
{
coll.push_back(i);
}
for_each(coll.begin(), coll.end(), print);
cout << endl;
system("pause");
return 0;
}
2.copy()函数定义
template<class InputIterator, class OutputIterator>
OutputIterator copy(
InputIterator _First,
InputIterator _Last,
OutputIterator _DestBeg
);
demo:
int main(){
typedef vector<int> int_vector;
typedef istream_iterator<int> istream_itr;
typedef ostream_iterator<int> ostream_itr;
typedef back_insert_iterator< int_vector > back_ins_itr;
int src[]= {1, 2, 3, 4, 5, 6, 7};
int_vector srcVec(7, 0);
ofstream otext("F:\\test.txt");//定义文件输出流
copy (src, src + 7, srcVec.begin()); //从容器copy到容器
copy(srcVec.begin(), srcVec.end(), ostream_itr(cout," "));//从容器copy到标准输出流
copy(istream_itr(cin), istream_itr(), back_ins_itr(srcVec));//将标准输入流copy到容器
copy(srcVec.begin(), srcVec.end(), ostream_itr(otext," "));//从容器copy到文件输出流
copy(istream_itr(cin), istream_itr(), ostream_itr(cout," "));//将标准输入流copy到标准输出流
copy(istream_itr(cin), istream_itr(), ostream_itr(otext," "));//将标准输入流copy到文件输出流
cout << endl;
system("pause");
}