Guysl
April 22nd, 2004, 05:42 PM
I came up with my version for FIND that was discussed in one of the previous posts.
I would like to get feedbacks, but first a few words:
a reminder about the original post:
say we have a vector of integers. by using syntax like:
vector<int> vec;
vec.push_back(...)
find(vec > 2 && vec < 5 || vec >12 && vec<14)
we would like to get all the values that between 2 to 5, or between 12 to 14.
I'm sure there are better ways to design and implement it since
I'm not familiar enough with templates nor with STL.
for now, I used only 2 operators, just for a demonstration,
and a container of vector, so here we go:
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream.h>
using namespace std;
template<typename T>
class containrWrapper{
private:
struct result{
vector<T> vResults;
friend result operator &&(const result& lh, const result& rh)
{
result totalResult;
if( !lh.vResults.empty() && !rh.vResults.empty() )
{
result lhSorted = lh, rhSorted = rh ;
sort( lhSorted.vResults.begin(), lhSorted.vResults.end());
sort( rhSorted.vResults.begin(), rhSorted.vResults.end());
set_intersection( lhSorted.vResults.begin(), lhSorted.vResults.end(),
rhSorted.vResults.begin(), rhSorted.vResults.end(),
back_inserter(totalResult.vResults) );
}
return totalResult;
}
friend result operator ||(const result& lh, const result& rh)
{
result totalResult = lh;
totalResult.vResults.insert(totalResult.vResults.end(), rh.vResults.begin(), rh.vResults.end());
unique( totalResult.vResults.begin(), totalResult.vResults.end());
return totalResult;
}
};
public:
vector<T> vData;
containrWrapper(const vector<T>& vec) : vData(vec) {}
result operator>(const T& val)
{
result res;
remove_copy_if( vData.begin(), vData.end(), back_inserter(res.vResults),
bind2nd(less_equal<T>(), val) );
return res;
}
result operator<(const T& val)
{
result res;
remove_copy_if( vData.begin(), vData.end(), back_inserter(res.vResults),
bind2nd(greater_equal<T>(), val));
return res;
}
vector<T> Query(const result& query)
{
return query.vResults;
}
};
int main(int argc, char* argv[])
{
vector<int> Vect;
Vect.push_back(8);
Vect.push_back(10);
Vect.push_back(2);
Vect.push_back(22);
Vect.push_back(7);
containrWrapper<int> Wrapper(Vect);
vector<int> result = Wrapper.Query( Wrapper > 5 && Wrapper < 9 || Wrapper > 12);
for( int i=0; i < result.size(); i++)
cout << result[i] << " ";
return 0;
}
I would like to get feedbacks, but first a few words:
a reminder about the original post:
say we have a vector of integers. by using syntax like:
vector<int> vec;
vec.push_back(...)
find(vec > 2 && vec < 5 || vec >12 && vec<14)
we would like to get all the values that between 2 to 5, or between 12 to 14.
I'm sure there are better ways to design and implement it since
I'm not familiar enough with templates nor with STL.
for now, I used only 2 operators, just for a demonstration,
and a container of vector, so here we go:
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream.h>
using namespace std;
template<typename T>
class containrWrapper{
private:
struct result{
vector<T> vResults;
friend result operator &&(const result& lh, const result& rh)
{
result totalResult;
if( !lh.vResults.empty() && !rh.vResults.empty() )
{
result lhSorted = lh, rhSorted = rh ;
sort( lhSorted.vResults.begin(), lhSorted.vResults.end());
sort( rhSorted.vResults.begin(), rhSorted.vResults.end());
set_intersection( lhSorted.vResults.begin(), lhSorted.vResults.end(),
rhSorted.vResults.begin(), rhSorted.vResults.end(),
back_inserter(totalResult.vResults) );
}
return totalResult;
}
friend result operator ||(const result& lh, const result& rh)
{
result totalResult = lh;
totalResult.vResults.insert(totalResult.vResults.end(), rh.vResults.begin(), rh.vResults.end());
unique( totalResult.vResults.begin(), totalResult.vResults.end());
return totalResult;
}
};
public:
vector<T> vData;
containrWrapper(const vector<T>& vec) : vData(vec) {}
result operator>(const T& val)
{
result res;
remove_copy_if( vData.begin(), vData.end(), back_inserter(res.vResults),
bind2nd(less_equal<T>(), val) );
return res;
}
result operator<(const T& val)
{
result res;
remove_copy_if( vData.begin(), vData.end(), back_inserter(res.vResults),
bind2nd(greater_equal<T>(), val));
return res;
}
vector<T> Query(const result& query)
{
return query.vResults;
}
};
int main(int argc, char* argv[])
{
vector<int> Vect;
Vect.push_back(8);
Vect.push_back(10);
Vect.push_back(2);
Vect.push_back(22);
Vect.push_back(7);
containrWrapper<int> Wrapper(Vect);
vector<int> result = Wrapper.Query( Wrapper > 5 && Wrapper < 9 || Wrapper > 12);
for( int i=0; i < result.size(); i++)
cout << result[i] << " ";
return 0;
}