Solved

STL for_each and iterator implemmentation?

Posted on 2006-07-05
8
555 Views
Last Modified: 2013-11-17
Hi!

This is my first time that I am using STL and I have some question about it.
The thing that I am trying to do is to use for_each algorithm on a array of  structurs. So i have created a class that is handling this array and a iterator class to use for the for_each algorithm.

First my MessageHandler this is the class handling the structur
typedef struct{
        unsigned __int32 Param_code;
        unsigned __int32 Time;
        unsigned __int32 Measure_Value;
}S_record;
typedef struct{
        //--------HEADERS-------
        unsigned __int16 STX;
      unsigned __int16 CRC;
        unsigned __int16 MachID;
        unsigned __int16 ClinicalSWID;
        unsigned __int32 MsgCounter;
        unsigned __int32 CmdCode;
        unsigned __int32 MsgInfo;
        unsigned __int32 Flags;
        unsigned __int32 PatId;
        //----------------------
        S_record records[8];
}Message;

class MessageIterator;

class MessageHandler {
        friend class MessageIterator;
        public:
                MessageHandler(Message* mes);
                .....
                ..........
                MessageIterator begin();
                MessageIterator end();
        private:
                .....
                Message* message;
};
MessageIterator MessageHandler::begin() {
        return  MessageIterator(*this, 0);
}
MessageIterator MessageHandler::end() {
        int size = sizeof(message->records) / sizeof(S_record);
        return  MessageIterator(*this, size);
}

and then i have the iterator class
class MessageIterator {
        public:
                MessageIterator(MessageHandler& m, int p) : mh(m), pos(p) {};
                bool operator!=(const MessageIterator& messitr) const;
                MessageIterator& operator++();
                S_record operator*();
        private:
                MessageHandler mh;
                int pos;
};
bool MessageIterator::operator!=(const MessageIterator& messitr) const {
        return (pos != messitr.pos);
}
MessageIterator& MessageIterator::operator++() {
        ++pos;
        return *this;
}
S_record MessageIterator::operator*() {
        return mh.message->records[pos];
}

and then in a button even I would like to use for_each like this

std::for_each(mh.begin(), mh.end(), function to plot values);

the problem is that i dont know for sure how to use the for each. the function should plot two values from each record in the arry on a graph, something like graph1->plot_XY(each_record.x,each_record.y);

The closest i have com is following

template<class T> struct print : public std::unary_function<T, void>
{
  print(TGraph& g) : graph(g) {}
  void operator() (T x) { graph.plot_XY(x.Measure_Value, x.Time); }
  TGraph& graph;
};

void __fastcall TForm1::ConnectButtonClick(TObject *Sender)
{
    std::for_each(mh.begin(), mh.end(), print<S_record>(*graph1));
}

I appreciate all comments on this question even the small ones if i have used pointer or reference in the wrong way.

Thanks
0
Comment
Question by:mikrodidakt
  • 4
  • 3
8 Comments
 

Author Comment

by:mikrodidakt
ID: 17042331
The problem is that i dont get the result that i would like to get and i cant debug the template

Thanks
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17043794
Hi there,

the third argument to for_each is a reference to a function to be executed for_each item

//-----------------------------------------------
std::vector <CMyItem> Items;

//* if the function you would like to execute for each item is a member function .
//* you need to generate a pointer to that member function
//* mem_fun_ref will do that for you .. it will actually create a functor I think its called.

std::for_each(Items.begin(), Items.end(), std::mem_fun_ref( &CMyItem::DoSomehing));

/* otherwise - global function*/
std::for_each(Items.begin(), Items.end(),  &DoSomehingElse));


//** defined somwhere, declared prior to for_each()
void DoSomehingElse(CMyItem &Item)
{
      ;
}

Cheers Sebastian

0
 
LVL 14

Accepted Solution

by:
cwwkie earned 500 total points
ID: 17044806
> The problem is that i dont get the result that i would like to get and i cant debug the template

I needed to make some small modifications to make it compile, but I don't see anything wrong with your code. The following program compiles fine on c++builder5:

//---------------------------------------------------------------------------

#pragma hdrstop

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

#include <iostream>
#include <iomanip>

using namespace std;

struct S_record{
        unsigned __int32 Param_code;
        unsigned __int32 Time;
        unsigned __int32 Measure_Value;
        S_record(__int32 Param_code_, __int32 Time_, __int32 Measure_Value_)
             : Param_code(Param_code_), Time(Time_), Measure_Value(Measure_Value_) {};
};

class TGraph {
  public:
    void plot_XY(__int32 x,__int32 y){cout << "x="<<x<<", y="<<y<<endl;};
};

template<class T> struct print : public std::unary_function<T, void>
{
  print(TGraph& g) : graph(g) {}
  void operator() (T x) { graph.plot_XY(x.Measure_Value, x.Time); }
  TGraph& graph;
};


//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
        vector<S_record> mh;
        TGraph *graph1=new TGraph;
        mh.push_back(S_record(1,1,1));
        mh.push_back(S_record(2,2,2));
        std::for_each(mh.begin(), mh.end(), print<S_record>(*graph1));

        //while(1);
        return 0;
}
//---------------------------------------------------------------------------
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 17044822
and to debug a gui program, I would recommend adding OutputDebugString statements and http://www.sysinternals.com/Utilities/DebugView.html to view them.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:mikrodidakt
ID: 17048981
Thanks for the answers!

I got it to work i hade just misunderstud the result on the graph.
Do u have any suggestions on when to use STL and when it is overkill.
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 17052110
> Do u have any suggestions on when to use STL and when it is overkill.

I would just suggest to use it when you need it!
If you create something yourself, it costs time to create it, costs time to test and debug it. With the stl you get everything ready to use. And you might be able to write for example a for_each for your situation which runs faster, but on nowadays computers it is not worth the effort. It is much more important to write maintainable and reusable code.
0
 

Author Comment

by:mikrodidakt
ID: 17056750
But by using STL and templates is't the code more reusable then if i wore going to write a for-loop that iterator through the array?

Thanks for all your answers.
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 17059836
Yes, if you are doing something like below, it is not very reusable, but because the STL defines standard interfaces and templates, functions can be easier used for other datatypes/other situations.

S_record *records;
int recordsize;

recordsize=10;
records=new S_record[recordsize];

...

for(int i=0; i<recordsize; ++i)
    graph.plot_XY(records[i].Measure_Value, records[i].Time);
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Debugging a PHP Web Application 2 96
importing  maven Project 5 232
Object Data Source 4 122
PHPStorm, PHPUnit not found in include path 2 299
In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now