//afxstuff.hpp
#ifndef ALGOSTUFF_HPP
#define ALGOSTUFF_HPP
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <functional>
#include <numeric>
using namespace std;
template<class T>
void PRINT_ELEMENTS(const T& collection, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout<< optcstr;
for(pos = collection.begin(); pos != collection.end();++pos)
{
std::cout<<*pos<< ' ';
}
std::cout<<std::endl;
}
template<class T>
void INSERT_ELEMENTS(T& col, int first, int last)
{
for (int i = first; i <= last; ++i){
col.insert(col.end(), i);
}
}
#endif
//search1.hpp
#ifndef SEARCH1_HPP
#define SEARCH1_HPP
#include "afxstuff.hpp"
void search_test1()
{
deque<int> dqi;
list<int> li;
INSERT_ELEMENTS(dqi, 1, 7);
INSERT_ELEMENTS(dqi, 1, 7);
INSERT_ELEMENTS(li, 3, 6);
PRINT_ELEMENTS(dqi, "deque:");
PRINT_ELEMENTS(li,"list:");
deque<int>::iterator dii_pos;
dii_pos = search(dqi.begin(), dqi.end(), li.begin(), li.end());
while (dii_pos != dqi.end())
{
cout<< " sub coll found starting with element " << distance(dqi.begin(), dii_pos) +1 << endl;
++dii_pos;
dii_pos = search(dii_pos, dqi.end(), li.begin(), li.end());
}
}
#endif
测试结果:
deque:1 2 3 4 5 6 7 1 2 3 4 5 6 7
list:3 4 5 6
sub coll found starting with element 3
sub coll found starting with element 10
请按任意键继续. . .
//search2.hpp
#ifndef SEARCH2_HPP
#define SEARCH2_HPP
#include "afxstuff.hpp"
bool CheckEven(int n, bool is_even){
if (is_even){
return n%2==0;
}
else {
return n%2 == 1;
}
}
void search_test2()
{
deque<int> dqi;
list<int> li;
INSERT_ELEMENTS(dqi, 1, 9);
PRINT_ELEMENTS(dqi, "deque:");
bool check_even_params[] = {true, false, true};
deque<int>::iterator dii_pos;
dii_pos = search(dqi.begin(), dqi.end(), check_even_params, check_even_params+3, CheckEven );
while (dii_pos != dqi.end())
{
cout<< " sub coll found starting with element " << distance(dqi.begin(), dii_pos) +1 << endl;
++dii_pos;
dii_pos = search(++dii_pos, dqi.end(), check_even_params, check_even_params+3, CheckEven );
}
}
#endif
测试结果:
deque:1 2 3 4 5 6 7 8 9
sub coll found starting with element 2
sub coll found starting with element 4
sub coll found starting with element 6
请按任意键继续. . .