472,798 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,798 software developers and data experts.

Iterators - how ?

Greetings! I believe someone can spare me a minute and give me some
ideas.

Once upon a time I had a C program:

double propert[100];
char symb[100];
char* descript[100];

But now I want to make it in C++. So:

class SingleData {
public:
double propert;
char symbol;
string descript;

void doSomething(...);
};

vector<SingleData> my_data;

But I need to iterate over SingleData as well as over each field of the
class. The efficiency is crucial. And now I came to the problem:

How should I implement iterators for symbol, propert and descript? I
want them (eg. iterator over SingleData::propert) to behave like
vector<double>propert::iterator. It would be nice, if I could use
algorithms from STL (eg. max, min) for those iterators. Can it be as
fast as accesing raw data in the C implementation?

Implementation like:
vector<double> propert;
vector<string> descript;
vector<char> symb;
is not an option, because sometimes I have to pass a whole SingleData*
object
Dominik Gront

Jul 23 '05 #1
2 1401
dgront wrote:

How should I implement iterators for symbol, propert and descript? I
want them (eg. iterator over SingleData::propert) to behave like
vector<double>propert::iterator. It would be nice, if I could use
algorithms from STL (eg. max, min) for those iterators. Can it be as
fast as accesing raw data in the C implementation?


Yes, it can be as fast. You need an iterator adaptor:

template <class Iter>
struct PropertyIterator
{
PropertyIterator(Iter it) : iter(it) {}
double operator*() const { return iter->property; }
private:
Iter it;
};

There's more code needed to create a complete iterator, but that should
give you the idea.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2
dgront wrote:
Greetings! I believe someone can spare me a minute and give me some
ideas.

Once upon a time I had a C program:

double propert[100];
char symb[100];
char* descript[100];

But now I want to make it in C++. So:

class SingleData {
public:
double propert;
char symbol;
string descript;

void doSomething(...);
};

vector<SingleData> my_data;

But I need to iterate over SingleData as well as over each field of the
class. The efficiency is crucial. And now I came to the problem:

How should I implement iterators for symbol, propert and descript? I
want them (eg. iterator over SingleData::propert) to behave like
vector<double>propert::iterator. It would be nice, if I could use
algorithms from STL (eg. max, min) for those iterators. Can it be as
fast as accesing raw data in the C implementation?
I think what you need for sorting and searching is your own _comparator_,
not your own iterator. The idea is that your comparator should only look
at particular members of the object passed to it for inspection.

Read up on algorithms and see the "xxx_if" (like 'find_if') variations.

For maintaining different sorting criteria you might actually consider
keeping a separate vector of indices which when iterated should give you
the right order of [indices to] objects. Another way is to keep sets of
indices with your own sorting functors.
[..]


That's as much as I could come up with in a minute.

V
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Steven Bethard | last post by:
So, as I understand it, in Python 3000, zip will basically be replaced with izip, meaning that instead of returning a list, it will return an iterator. This is great for situations like: zip(*)...
18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
1
by: Marcin Kaliciñski | last post by:
template<class RanAccIt> void some_algorithm(RanAccIt begin, RanAccIt end) { // this algorithm involves calling std::lexicographical_compare // on range [begin, end), and on reverse of this range...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
8
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
14
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.