PROGRAM :
Code:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <numeric>
template <class S>
inline void PRINTELEMENTS (const S& cl, const char* optstr="")
{
typename S::const_iterator ps;
std::cout << optstr;
for (ps=cl.begin(); ps!=cl.end(); ++ps)
{
std::cout << *ps << ' ';
}
std::cout << std::endl;
}
template <class S>
inline void INSERTELEMENTS (S& cl, int fst, int lst)
{
for (int j=fst; j<=lst; ++j)
{
cl.insert(cl.end(),j);
}
}
using namespace std;
bool chkEvn (int elm, bool evn)
{
if (evn)
{
return elm 2 == 0;
}
else
{
return elm 2 == 1;
}
}
int main()
{
vector<int> cl;
INSERTELEMENTS(cl,1,9);
PRINTELEMENTS(cl,"coll: ");
bool chkEvnArgs[3] = { true, false, true };
vector<int>::iterator ps;
ps = search (cl.begin(), cl.end(),
chkEvnArgs, chkEvnArgs+3,
checkEvn);
while (ps != cl.end())
{
cout << "subrange found starting with element "
<< distance(cl.begin(),ps) + 1
<< endl;
ps = search (++ps, cl.end(),
chkEvnArgs, chkEvnArgs+3,
chkEvn);
}
}
OUTPUT :
Code:
coll: 1 2 3 4 5 6 7 8 9
subrange found starting with element 2
subrange found starting with element 4
subrange found starting with element 6
Bookmarks