CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #1
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Arrow implementation of FIND (part II)

    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:

    Code:
    #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;
    }
    Last edited by Guysl; April 23rd, 2004 at 03:28 PM.
    **** **** **** **** **/**

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured