Question on using for_each

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yan

    Question on using for_each

    I have a vector of elements which I iterate through and call a method
    on each of the elements. I want to do it using std::for_each
    algorithm, but having a problem implementing it since the method that
    I call on each element takes an argument and I don't know how to pass
    this argument through. Here is the code using an old fashioned loop:

    .......
    std::vector<uns igned char bytes;
    std::vector<Tel ements;
    ....
    for (int i = 0; i < elements.size() ; ++i) {
    elements[i].serialize(byte s);
    }
    ..........

    could someone please help me out rewrite that using std::for_each?

    Thanks!
  • Barry

    #2
    Re: Question on using for_each

    On 11ÔÂ6ÈÕ, ÏÂÎç10ʱ47·Ö, Yan <yvinogra...@gm ail.comwrote:
    I have a vector of elements which I iterate through and call a method
    on each of the elements. I want to do it using std::for_each
    algorithm, but having a problem implementing it since the method that
    I call on each element takes an argument and I don't know how to pass
    this argument through. Here is the code using an old fashioned loop:
    >
    ......
    std::vector<uns igned char bytes;
    std::vector<Tel ements;
    ...
    for (int i = 0; i < elements.size() ; ++i) {
    elements[i].serialize(byte s);}
    >
    .........
    >
    could someone please help me out rewrite that using std::for_each?
    >
    struct Op
    {
    Op(std::vector< unsigned char>& bytes)
    : bytes_(&bytes)
    {}

    void operator() (T& t)
    {
    t.serialize(*by tes_);
    }

    private:
    std::vector<uns igned char>* bytes_;
    };

    std::for_each(e lements.begin() , elements.end(), Op(bytes));

    --
    Best Regards
    Barry

    Comment

    • Maxim Yegorushkin

      #3
      Re: Question on using for_each

      On Nov 6, 2:47 pm, Yan <yvinogra...@gm ail.comwrote:
      I have a vector of elements which I iterate through and call a method
      on each of the elements. I want to do it using std::for_each
      algorithm, but having a problem implementing it since the method that
      I call on each element takes an argument and I don't know how to pass
      this argument through. Here is the code using an old fashioned loop:
      >
      ......
      std::vector<uns igned char bytes;
      std::vector<Tel ements;
      ...
      for (int i = 0; i < elements.size() ; ++i) {
        elements[i].serialize(byte s);}
      >
      .........
      >
      could someone please help me out rewrite that using std::for_each?
      It may be better to use a straight forward loop, because this way it
      is much more easier to read and debug than for_each loops with complex
      functors.

      Compare your loop with:

      std::for_each(e lements.begin() , elements.end(),
      boost::bind(&T: :serialize, _1, boost::ref(byte s)));

      --
      Max

      Comment

      • sean_in_raleigh@yahoo.com

        #4
        Re: Question on using for_each

        On Nov 6, 9:47 am, Yan <yvinogra...@gm ail.comwrote:
        I have a vector of elements which I iterate through and call a method
        on each of the elements. I want to do it using std::for_each
        algorithm, but having a problem implementing it since the method that
        I call on each element takes an argument and I don't know how to pass
        this argument through. Here is the code using an old fashioned loop:
        >
        ......
        std::vector<uns igned char bytes;
        std::vector<Tel ements;
        ...
        for (int i = 0; i < elements.size() ; ++i) {
        elements[i].serialize(byte s);}
        >
        .........
        >
        could someone please help me out rewrite that using std::for_each?
        >
        Thanks!
        Here's the standard-C++ way, though the boost::bind method
        mentioned earlier is more flexible.

        class MySerializable
        {
        public:
        void serialize(vecto r<unsigned char*b)
        {
        b->push_back(my_b its);
        }
        private:
        unsigned char my_bits;
        };

        int
        main(int argc, char **argv)
        {
        vector<unsigned char bytes;
        vector<MySerial izableelements;

        // ... fill in elements ...

        for_each(elemen ts.begin(), elements.end(),
        bind2nd(mem_fun _ref(&MySeriali zable::serializ e),
        &bytes));
        }

        Sean

        Comment

        • Yan

          #5
          Re: Question on using for_each

          On Nov 6, 11:35 am, sean_in_rale... @yahoo.com wrote:
          On Nov 6, 9:47 am, Yan <yvinogra...@gm ail.comwrote:
          >
          >
          >
          I have a vector of elements which I iterate through and call a method
          on each of the elements. I want to do it using std::for_each
          algorithm, but having a problem implementing it since the method that
          I call on each element takes an argument and I don't know how to pass
          this argument through. Here is the code using an old fashioned loop:
          >
          ......
          std::vector<uns igned char bytes;
          std::vector<Tel ements;
          ...
          for (int i = 0; i < elements.size() ; ++i) {
            elements[i].serialize(byte s);}
          >
          .........
          >
          could someone please help me out rewrite that using std::for_each?
          >
          Thanks!
          >
          Here's the standard-C++ way, though the boost::bind method
          mentioned earlier is more flexible.
          >
          class MySerializable
          {
          public:
              void serialize(vecto r<unsigned char*b)
              {
                  b->push_back(my_b its);
              }
          private:
              unsigned char my_bits;
          >
          };
          >
          int
          Thanks everyone!

          Indeed, in this case the good old loop seems easier to write, read,
          and, if needed, to debug :)

          main(int argc, char **argv)
          {
              vector<unsigned char bytes;
              vector<MySerial izableelements;
          >
              // ... fill in elements ...
          >
              for_each(elemen ts.begin(), elements.end(),
                       bind2nd(mem_fun _ref(&MySeriali zable::serializ e),
          &bytes));
          >
          }
          >
          Sean

          Comment

          Working...