Container V1: 1 2 3 4 5 6 7

I want to print the elements which are greater than equal to 3 but less than 6. How to do this using Standard template library functions..

We have logical_and, greater_equal and less_equal Predefined Function Objects. But i dont know how to implement this. Could any one please solve this problem

Recommended Answers

All 6 Replies

what container are you using? vector? Yes, then treat a vector just as you would a simple array of ints. If you can do this this an array of ints then you can do it with a vector -- same syntax.

i want to write code by using any of the algorithm not using array subscript. What is compose2 algorithm. What is the syntax for this? i want to use like this. But the following code is not working.could any one plz tell me errors in this.

vector<int>::iterator pos;
       pos = remove_if (coll.begin(),coll.end(),
                           compose2(logical_and<bool>(),
                                           bind2nd(greater<int>(),4),
                                           bind2nd(less<int>(),7)));


       //remove "removed" elements in coll
       coll.erase(pos,coll.end());

> But the following code is not working.c
Did you include the code for compose2? It's not a standard STL function, but Edward would guess that you mean to use the one defined in Nicolai Josuttis' STL book:

#include <functional>

/* class for the compose_f_gx_hx adapter
 */
template <class OP1, class OP2, class OP3>
class compose_f_gx_hx_t
 : public std::unary_function<typename OP2::argument_type,
                              typename OP1::result_type>
{
  private:
    OP1 op1;    // process: op1(op2(x),op3(x))
    OP2 op2;
    OP3 op3;
  public:
    // constructor
    compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
     : op1(o1), op2(o2), op3(o3) {
    }

    // function call
    typename OP1::result_type
    operator()(const typename OP2::argument_type& x) const {
        return op1(op2(x),op3(x));
    }
};

/* convenience function for the compose_f_gx_hx adapter
 */
template <class OP1, class OP2, class OP3>
inline compose_f_gx_hx_t<OP1,OP2,OP3>
compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3) {
    return compose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
}
// remove all elements that are greater than four and less than seven
// - retain new end
vector<int>::iterator pos;
pos = remove_if (coll.begin(),coll.end(),
                 compose_f_gx_hx(logical_and<bool>(),
                                 bind2nd(greater<int>(),4),
                                 bind2nd(less<int>(),7)));

// remove ``removed'' elements in coll
coll.erase(pos,coll.end());

> But the following code is not working.c
Did you include the code for compose2? It's not a standard STL function, but Edward would guess that you mean to use the one defined in Nicolai Josuttis' STL book:

#include <functional>

/* class for the compose_f_gx_hx adapter
 */
template <class OP1, class OP2, class OP3>
class compose_f_gx_hx_t
 : public std::unary_function<typename OP2::argument_type,
                              typename OP1::result_type>
{
  private:
    OP1 op1;    // process: op1(op2(x),op3(x))
    OP2 op2;
    OP3 op3;
  public:
    // constructor
    compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
     : op1(o1), op2(o2), op3(o3) {
    }

    // function call
    typename OP1::result_type
    operator()(const typename OP2::argument_type& x) const {
        return op1(op2(x),op3(x));
    }
};

/* convenience function for the compose_f_gx_hx adapter
 */
template <class OP1, class OP2, class OP3>
inline compose_f_gx_hx_t<OP1,OP2,OP3>
compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3) {
    return compose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
}
// remove all elements that are greater than four and less than seven
// - retain new end
vector<int>::iterator pos;
pos = remove_if (coll.begin(),coll.end(),
                 compose_f_gx_hx(logical_and<bool>(),
                                 bind2nd(greater<int>(),4),
                                 bind2nd(less<int>(),7)));

// remove ``removed'' elements in coll
coll.erase(pos,coll.end());

I think this code is too complicated. I want to do logic operations by using simple algorithms. Is there any simple methods to show logical opertaions ie logical_and, logical_or and logical_not

> I think this code is too complicated.
The usage code is identical to yours except for the name. This is the code you posted that didn't work:

vector<int>::iterator pos;
pos = remove_if (coll.begin(),coll.end(),
                 compose2(logical_and<bool>(),
                                 bind2nd(greater<int>(),4),
                                 bind2nd(less<int>(),7)));


//remove "removed" elements in coll
coll.erase(pos,coll.end());

This is the code Ed posted that would work if you also included the implementation of compose2 that Ed also provided:

// remove all elements that are greater than four and less than seven
// - retain new end
vector<int>::iterator pos;
pos = remove_if (coll.begin(),coll.end(),
                 compose_f_gx_hx(logical_and<bool>(),
                                 bind2nd(greater<int>(),4),
                                 bind2nd(less<int>(),7)));

// remove ``removed'' elements in coll
coll.erase(pos,coll.end());

The STL is there to make the usage simple. The implementation that facilitates simple usage is very rarely itself simple. If you want to keep it "simple", you shouldn't be using non-standard templates that require you to look at the implementation.

> Is there any simple methods to show logical opertaions ie logical_and, logical_or and logical_not
The simple methods all take into account the fact that logical_and and logical_or aren't designed to be composed in the current C++ standard. The simple methods are straight boolean transformations:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

int main()
{
  bool a[] = {0,1,1,0,1,1,1,0};
  bool b[] = {0,0,0,1,1,1,0,0};
  std::vector<bool> va(a, a + 8);
  std::vector<bool> vb(b, b + 8);
  std::vector<bool> vc(8);

  std::transform(va.begin(), va.end(),
    vb.begin(), vc.begin(),
    std::logical_and<bool>());
  std::copy(vc.begin(), vc.end(),
    std::ostream_iterator<bool>(std::cout, " "));

  std::cout << '\n';

  std::transform(va.begin(), va.end(),
    vb.begin(), vc.begin(),
    std::logical_or<bool>());
  std::copy(vc.begin(), vc.end(),
    std::ostream_iterator<bool>(std::cout, " "));

  std::cout << '\n';
}

the C++ Standard Library doesn't provide any facility to compose function objects using logical connectives. (SGI STL does have the adapters compose1 and compose2; perhaps that is what you intended to use).
such a facility is not planned for c++09 either; lambda functions and the bind library provide equivalent functionality with simpler syntax.

> is there any simple method...
yes. use the boost.lambda library http://www.boost.org/doc/libs/1_35_0/doc/html/lambda.html in c++98, lambda functions in c++09.

#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <vector>

int main()
{
  using namespace boost::lambda ;
  std::vector<int> v( 5U, 8 ) ;
  std::remove_if( v.begin(), v.end(), _1<6 && _1>2 ) ;
}
#include <algorithm>
#include <vector>

int main()
{
  std::vector<int> v( 5U, 8 ) ;
  std::remove_if( v.begin(), v.end(),
                  []( int x ) { return x>2 && x<6 ; } ) ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.