辅助函数

本节跟以后几节将对所有STL算法逐一详细讨论。为了简化这些例子,我们使用了一些辅助函数,分别用于对容器进行输出跟插入操作。

 1 #ifndef ALGOSTUFF_HPP
 2 #define ALGOSTUFF_HPP
 3 #include <iostream>
 4 #include <vector>
 5 #include <deque>
 6 #include <list>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <algorithm>
11 #include <functional>
12 #include <numeric>
13 
14 template <class T>
15 inline void PRINT_ELEMENTS(const T& coll,const char* optcstr="")
16 {
17     typename T::const_iterator pos;
18     std::cout<<optcstr;
19     for(pos=coll.begin();pos!=coll.end();++pos)
20         std::cout<<*pos<<" ";
21     std::cout<<std::endl;
22 }
23 
24 template <class T>
25 inline void INSERT_ELEMENTS(T& coll,int first,int last)
26 {
27     for(int i=first;i<=last;++i)
28         coll.insert(coll.end(),i);
29 }
30 #endif
View Code

相关文章: