473,480 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

making my own iterator


I have a class of my own which has a 2d matrix of type T. I want to
give the users of my library an iterator to iterator thru all the
elements of this matrix ( they dont need to know anything about the
internal matrix). What is the easiest way to do this?

template< typename T >
class X{

// X::iterator needs to be defined for parsing data.
// const_iterator?

private:
vector< vector< T data;
}

Thanks,
--j

Nov 26 '06 #1
3 2048

John wrote:
I have a class of my own which has a 2d matrix of type T. I want to
give the users of my library an iterator to iterator thru all the
elements of this matrix ( they dont need to know anything about the
internal matrix). What is the easiest way to do this?
use boost's iterator library.

Nov 26 '06 #2
John wrote:
>
I have a class of my own which has a 2d matrix of type T. I want to
give the users of my library an iterator to iterator thru all the
elements of this matrix ( they dont need to know anything about the
internal matrix). What is the easiest way to do this?

template< typename T >
class X{

// X::iterator needs to be defined for parsing data.
// const_iterator?

private:
vector< vector< T data;
}
The most easy to maintain is probably an iterator that does not really care
too much about the internal representation. You could do that as follows
[untested code]:

template < typename T >
class Matrix {

// the usual typedefs
typedef T value_type;

class iterator {

Matrix & the_matrix;
Matrix::size_type the_row;
Matrix::size_type the_col;

public:

Matrix::reference operator* ( void ) {
return ( the_matrix( the_row, the_col ) );
}

iterator operator++ ( void ) {
++ the_col;
if ( the_col == the_matrix.col_size() ) {
++ the_row;
the_col = 0;
}
return ( *this );
}

...

};

};

Note that in operator++ and its relatives, you have a choice for how to
traverse the matrix.

Also note that this iterator only relies on the interface of the Matrix
class. If you want to optimize for performance, you would need to tailor
the iterator towards the internal representation.
Best

Kai-Uwe Bux
Nov 26 '06 #3
JCR
Hellom
There is this paper by Ullrich Kothe that describes, among other
things, iterators for images, which are basically 2d matrices. You may
find it useful.

http://kogs-www.informatik.uni-hambu...C++Report.html

JCR
On Nov 26, 12:12 am, "John" <weekender...@yahoo.comwrote:
I have a class of my own which has a 2d matrix of type T. I want to
give the users of my library an iterator to iterator thru all the
elements of this matrix ( they dont need to know anything about the
internal matrix). What is the easiest way to do this?

template< typename T >
class X{

// X::iterator needs to be defined for parsing data.
// const_iterator?

private:
vector< vector< T data;

}Thanks,
--j
Nov 26 '06 #4

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

Similar topics

38
3636
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
26
1514
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
0
1925
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
14
4849
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
0
2660
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
21
5675
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
16
2551
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
16
5316
by: arnaudk | last post by:
I'm trying to design a simple container class for some data of different types based on a vector of structs, but the vector and struct are protected so that the implemenation of my container class...
4
1983
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.