473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Comment please: begin() method in container class

Dear all :
I am not good at programming, please give a hand.

My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It means
"next position in the object sequence" in STL but now I redefine it to
mean "next position within a 2-d range in the object sequence" (my data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.

To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important for
me to have a begin() in my container, like this:

using std::vector

class container::private vector<DATATYPE>
{

public:
using vector<DATATYPEl>::begin;
}

however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).

To to that, I make the container::begin() like this :

#inlcude myIterator.h

class container::private vector<DATATYPE>
{

private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}

}

// in myIterator.h , I have:

#include DATATYPE.h

class myIterator {

private:

DATATYPE* current;
.....................

public:

DATATYPE& operator*()
{
return *current;
}
}

I am not sure if it is the correct way to do that. Any one can give me
a hand is highly appreciated.

Thanks in advance.
Sheldon

Dec 8 '06 #1
11 1655
On Dec 8, 2:06 pm, "food4uk" <cai_xiaod...@hotmail.comwrote:
Dear all :
I am not good at programming, please give a hand.

My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It means
"next position in the object sequence" in STL but now I redefine it to
mean "next position within a 2-d range in the object sequence" (my data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.

To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important for
me to have a begin() in my container, like this:

using std::vector

class container::private vector<DATATYPE>
{

public:
using vector<DATATYPEl>::begin;

}however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).

To to that, I make the container::begin() like this :

#inlcude myIterator.h

class container::private vector<DATATYPE>
{

private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}

}// in myIterator.h , I have:

#include DATATYPE.h

class myIterator {

private:

DATATYPE* current;
.....................

public:

DATATYPE& operator*()
{
return *current;
}

}I am not sure if it is the correct way to do that. Any one can give me
a hand is highly appreciated.
Since you are using a vector you can take advantage of the fact that
it's elements are stored sequentially. However it might be
better/easier to simply make your iterator a wrapper around the
vector's iterator, much like your container.

--
Erik Wikström

Dec 8 '06 #2

"er****@student.chalmers.se 写é“:
"
On Dec 8, 2:06 pm, "food4uk" <cai_xiaod...@hotmail.comwrote:
Dear all :
I am not good at programming, please give a hand.

My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It means
"next position in the object sequence" in STL but now I redefine it to
mean "next position within a 2-d range in the object sequence" (my data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.

To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important for
me to have a begin() in my container, like this:

using std::vector

class container::private vector<DATATYPE>
{

public:
using vector<DATATYPEl>::begin;

}however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).

To to that, I make the container::begin() like this :

#inlcude myIterator.h

class container::private vector<DATATYPE>
{

private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}

}// in myIterator.h , I have:

#include DATATYPE.h

class myIterator {

private:

DATATYPE* current;
.....................

public:

DATATYPE& operator*()
{
return *current;
}

}I am not sure if it is the correct way to do that. Any one can give me
a hand is highly appreciated.

Since you are using a vector you can take advantage of the fact that
it's elements are stored sequentially. However it might be
better/easier to simply make your iterator a wrapper around the
vector's iterator, much like your container.

--
Erik Wikström
Thanks Erik !
I want to do that but actually I can not do it in that way.
Because most behivours of my iterator are different from a "raw pointer
to an array" - like the vector one. For example, the "--" , "++" "+="
etc. all are diffferent, mine iterator is working on a 2-d concept(for
example, a 2-d sub-range area of a 2-d image). But I think the
dereferencing * is the same. So It seems that I can not benefit two
much by wrapping the iterator.

Dec 8 '06 #3
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

"er****@student.chalmers.se ??:
"
On Dec 8, 2:06 pm, "food4uk" <cai_xiaod...@hotmail.comwrote:
Dear all :
I am not good at programming, please give a hand.

My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It means
"next position in the object sequence" in STL but now I redefine it to
mean "next position within a 2-d range in the object sequence" (my data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.

To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important for
me to have a begin() in my container, like this:

using std::vector

class container::private vector<DATATYPE>
{

public:
using vector<DATATYPEl>::begin;

}however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).

To to that, I make the container::begin() like this :

#inlcude myIterator.h

class container::private vector<DATATYPE>
{

private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}

}// in myIterator.h , I have:

#include DATATYPE.h

class myIterator {

private:

DATATYPE* current;
.....................

public:

DATATYPE& operator*()
{
return *current;
}

}I am not sure if it is the correct way to do that. Any one can give me
a hand is highly appreciated.

Since you are using a vector you can take advantage of the fact that
it's elements are stored sequentially. However it might be
better/easier to simply make your iterator a wrapper around the
vector's iterator, much like your container.

--
Erik Wikström
Thanks Erik !
I want to do that but actually I can not do it in that way.
Because most behivours of my iterator are different from a "raw pointer
to an array" - like the vector one. For example, the "--" , "++" "+="
etc. all are diffferent, mine iterator is working on a 2-d concept(for
example, a 2-d sub-range area of a 2-d image). But I think the
dereferencing * is the same. So It seems that I can not benefit two
much by wrapping the iterator.

Well, why don't you just use it as a 2D array then using at() the formula
being:
row * maxrow + column

That is, [2][3] for a 5x5 array would be:
..at( 2 * 5 + 3 );

You don't need to create your own container class, because vector supports
both [index] and .at(index). The different being .at does out of bounds
checking, [index] doesn't.
Dec 8 '06 #4
"Jim Langston 写é“:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

"er****@student.chalmers.se ??:
"
On Dec 8, 2:06 pm, "food4uk" <cai_xiaod...@hotmail.comwrote:
Dear all :
I am not good at programming, please give a hand.
>
My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It means
"next position in the object sequence" in STL but now I redefine it to
mean "next position within a 2-d range in the object sequence" (my data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.
>
To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important for
me to have a begin() in my container, like this:
>
using std::vector
>
class container::private vector<DATATYPE>
{
>
public:
using vector<DATATYPEl>::begin;
>
}however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).
>
To to that, I make the container::begin() like this :
>
#inlcude myIterator.h
>
class container::private vector<DATATYPE>
{
>
private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}
>
}// in myIterator.h , I have:
>
#include DATATYPE.h
>
class myIterator {
>
private:
>
DATATYPE* current;
.....................
>
public:
>
DATATYPE& operator*()
{
return *current;
}
>
}I am not sure if it is the correct way to do that. Any one can give me
a hand is highly appreciated.
Since you are using a vector you can take advantage of the fact that
it's elements are stored sequentially. However it might be
better/easier to simply make your iterator a wrapper around the
vector's iterator, much like your container.

--
Erik Wikström

Thanks Erik !
I want to do that but actually I can not do it in that way.
Because most behivours of my iterator are different from a "raw pointer
to an array" - like the vector one. For example, the "--" , "++" "+="
etc. all are diffferent, mine iterator is working on a 2-d concept(for
example, a 2-d sub-range area of a 2-d image). But I think the
dereferencing * is the same. So It seems that I can not benefit two
much by wrapping the iterator.

Well, why don't you just use it as a 2D array then using at() the formula
being:
row * maxrow + column

That is, [2][3] for a 5x5 array would be:
.at( 2 * 5 + 3 );

You don't need to create your own container class, because vector supports
both [index] and .at(index). The different being .at does out of bounds
checking, [index] doesn't.

Thank you Jim!
The reason for making a customer container is for the iterator. I agree
what you said. but there is some special situations:
say, accroding to your method, If I need to return an iterator in the
position of [2,3](say for a 5x5 array), then the vector::at() will
return a reference of the DATA in the location [2,3], but what I need a
iterator in [2,3] whose behaviours is customer defined, NOT a reference
of DATA.

Off course, you can get an iterator in [2,3], but as I said, this
iterator you got is an std::vector::iterator type NOT myIterator type,
which means that I cannot get myIterator type iterator by using at(),
then I will not be able to use STL algorithm by passing iterators of
myIterator type.

What do you think?

Thank you.
Sheldon

Dec 8 '06 #5
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
"Jim Langston ??:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

"er****@student.chalmers.se ??:
"
On Dec 8, 2:06 pm, "food4uk" <cai_xiaod...@hotmail.comwrote:
Dear all :
I am not good at programming, please give a hand.
>
My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It means
"next position in the object sequence" in STL but now I redefine it to
mean "next position within a 2-d range in the object sequence" (my
data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.
>
To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important for
me to have a begin() in my container, like this:
>
using std::vector
>
class container::private vector<DATATYPE>
{
>
public:
using vector<DATATYPEl>::begin;
>
}however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).
>
To to that, I make the container::begin() like this :
>
#inlcude myIterator.h
>
class container::private vector<DATATYPE>
{
>
private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}
>
}// in myIterator.h , I have:
>
#include DATATYPE.h
>
class myIterator {
>
private:
>
DATATYPE* current;
.....................
>
public:
>
DATATYPE& operator*()
{
return *current;
}
>
}I am not sure if it is the correct way to do that. Any one can give
me
a hand is highly appreciated.
Since you are using a vector you can take advantage of the fact that
it's elements are stored sequentially. However it might be
better/easier to simply make your iterator a wrapper around the
vector's iterator, much like your container.

--
Erik Wikström

Thanks Erik !
I want to do that but actually I can not do it in that way.
Because most behivours of my iterator are different from a "raw pointer
to an array" - like the vector one. For example, the "--" , "++" "+="
etc. all are diffferent, mine iterator is working on a 2-d concept(for
example, a 2-d sub-range area of a 2-d image). But I think the
dereferencing * is the same. So It seems that I can not benefit two
much by wrapping the iterator.

Well, why don't you just use it as a 2D array then using at() the formula
being:
row * maxrow + column

That is, [2][3] for a 5x5 array would be:
.at( 2 * 5 + 3 );

You don't need to create your own container class, because vector supports
both [index] and .at(index). The different being .at does out of bounds
checking, [index] doesn't.

Thank you Jim!
The reason for making a customer container is for the iterator. I agree
what you said. but there is some special situations:
say, accroding to your method, If I need to return an iterator in the
position of [2,3](say for a 5x5 array), then the vector::at() will
return a reference of the DATA in the location [2,3], but what I need a
iterator in [2,3] whose behaviours is customer defined, NOT a reference
of DATA.

Off course, you can get an iterator in [2,3], but as I said, this
iterator you got is an std::vector::iterator type NOT myIterator type,
which means that I cannot get myIterator type iterator by using at(),
then I will not be able to use STL algorithm by passing iterators of
myIterator type.

What do you think?

Thank you.
Sheldon

Well, exactly how do you expect your iterator to increment?

If you have a 5 x 5 array, and you are at 2,3, when you increment you expect
it to point to 2,4, right? Increment again, and what should it point to?
You are at the end of the row. Do you want it to point to 3,0?

If this is the behavior you want, then incrementing the normal iterator will
do that exact thing. What other type of behavior would you want?
Dec 8 '06 #6

"Jim Langston 写é“:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
"Jim Langston ??:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

"er****@student.chalmers.se ??:
"
On Dec 8, 2:06 pm, "food4uk" <cai_xiaod...@hotmail.comwrote:
Dear all :
I am not good at programming, please give a hand.

My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It means
"next position in the object sequence" in STL but now I redefine itto
mean "next position within a 2-d range in the object sequence" (my
data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.

To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important for
me to have a begin() in my container, like this:

using std::vector

class container::private vector<DATATYPE>
{

public:
using vector<DATATYPEl>::begin;

}however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).

To to that, I make the container::begin() like this :

#inlcude myIterator.h

class container::private vector<DATATYPE>
{

private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}

}// in myIterator.h , I have:

#include DATATYPE.h

class myIterator {

private:

DATATYPE* current;
.....................

public:

DATATYPE& operator*()
{
return *current;
}

}I am not sure if it is the correct way to do that. Any one can give
me
a hand is highly appreciated.
>
Since you are using a vector you can take advantage of the fact that
it's elements are stored sequentially. However it might be
better/easier to simply make your iterator a wrapper around the
vector's iterator, much like your container.
>
--
Erik Wikström
Thanks Erik !
I want to do that but actually I can not do it in that way.
Because most behivours of my iterator are different from a "raw pointer
to an array" - like the vector one. For example, the "--" , "++" "+="
etc. all are diffferent, mine iterator is working on a 2-d concept(for
example, a 2-d sub-range area of a 2-d image). But I think the
dereferencing * is the same. So It seems that I can not benefit two
much by wrapping the iterator.

Well, why don't you just use it as a 2D array then using at() the formula
being:
row * maxrow + column

That is, [2][3] for a 5x5 array would be:
.at( 2 * 5 + 3 );

You don't need to create your own container class, because vector supports
both [index] and .at(index). The different being .at does out of bounds
checking, [index] doesn't.


Thank you Jim!
The reason for making a customer container is for the iterator. I agree
what you said. but there is some special situations:
say, accroding to your method, If I need to return an iterator in the
position of [2,3](say for a 5x5 array), then the vector::at() will
return a reference of the DATA in the location [2,3], but what I need a
iterator in [2,3] whose behaviours is customer defined, NOT a reference
of DATA.

Off course, you can get an iterator in [2,3], but as I said, this
iterator you got is an std::vector::iterator type NOT myIterator type,
which means that I cannot get myIterator type iterator by using at(),
then I will not be able to use STL algorithm by passing iterators of
myIterator type.

What do you think?

Thank you.
Sheldon

Well, exactly how do you expect your iterator to increment?

If you have a 5 x 5 array, and you are at 2,3, when you increment you expect
it to point to 2,4, right? Increment again, and what should it point to?
You are at the end of the row. Do you want it to point to 3,0?

If this is the behavior you want, then incrementing the normal iterator will
do that exact thing. What other type of behavior would you want?

That is the problem: it to point to 2,4, Increment again, I don't
want it to point to 3,0, I want it points the next one which is 3, 3,
not 3, 0! to then end of this row, increase one , it points to 4,3 not
4,0.

That is why I have to define my own iterator.

What do you think?

Dec 8 '06 #7

"Jim Langston 写é“:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
"Jim Langston ??:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

"er****@student.chalmers.se ??:
"
On Dec 8, 2:06 pm, "food4uk" <cai_xiaod...@hotmail.comwrote:
Dear all :
I am not good at programming, please give a hand.

My data structure is very similar as an array. I actually can use the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It means
"next position in the object sequence" in STL but now I redefine itto
mean "next position within a 2-d range in the object sequence" (my
data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.

To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important for
me to have a begin() in my container, like this:

using std::vector

class container::private vector<DATATYPE>
{

public:
using vector<DATATYPEl>::begin;

}however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).

To to that, I make the container::begin() like this :

#inlcude myIterator.h

class container::private vector<DATATYPE>
{

private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}

}// in myIterator.h , I have:

#include DATATYPE.h

class myIterator {

private:

DATATYPE* current;
.....................

public:

DATATYPE& operator*()
{
return *current;
}

}I am not sure if it is the correct way to do that. Any one can give
me
a hand is highly appreciated.
>
Since you are using a vector you can take advantage of the fact that
it's elements are stored sequentially. However it might be
better/easier to simply make your iterator a wrapper around the
vector's iterator, much like your container.
>
--
Erik Wikström
Thanks Erik !
I want to do that but actually I can not do it in that way.
Because most behivours of my iterator are different from a "raw pointer
to an array" - like the vector one. For example, the "--" , "++" "+="
etc. all are diffferent, mine iterator is working on a 2-d concept(for
example, a 2-d sub-range area of a 2-d image). But I think the
dereferencing * is the same. So It seems that I can not benefit two
much by wrapping the iterator.

Well, why don't you just use it as a 2D array then using at() the formula
being:
row * maxrow + column

That is, [2][3] for a 5x5 array would be:
.at( 2 * 5 + 3 );

You don't need to create your own container class, because vector supports
both [index] and .at(index). The different being .at does out of bounds
checking, [index] doesn't.


Thank you Jim!
The reason for making a customer container is for the iterator. I agree
what you said. but there is some special situations:
say, accroding to your method, If I need to return an iterator in the
position of [2,3](say for a 5x5 array), then the vector::at() will
return a reference of the DATA in the location [2,3], but what I need a
iterator in [2,3] whose behaviours is customer defined, NOT a reference
of DATA.

Off course, you can get an iterator in [2,3], but as I said, this
iterator you got is an std::vector::iterator type NOT myIterator type,
which means that I cannot get myIterator type iterator by using at(),
then I will not be able to use STL algorithm by passing iterators of
myIterator type.

What do you think?

Thank you.
Sheldon

Well, exactly how do you expect your iterator to increment?

If you have a 5 x 5 array, and you are at 2,3, when you increment you expect
it to point to 2,4, right? Increment again, and what should it point to?
You are at the end of the row. Do you want it to point to 3,0?

If this is the behavior you want, then incrementing the normal iterator will
do that exact thing. What other type of behavior would you want?

That is the problem: it to point to 2,4, Increment again, I don't
want it to point to 3,0, I want it points the next one which is 3, 3,
not 3, 0! to then end of this row, increase one , it points to 4,3 not
4,0.

That is why I have to define my own iterator.

What do you think?

Dec 8 '06 #8
On 2006-12-08 17:19, food4uk wrote:
"Jim Langston 写é“:
>Well, exactly how do you expect your iterator to increment?

If you have a 5 x 5 array, and you are at 2,3, when you increment you expect
it to point to 2,4, right? Increment again, and what should it point to?
You are at the end of the row. Do you want it to point to 3,0?

If this is the behavior you want, then incrementing the normal iterator will
do that exact thing. What other type of behavior would you want?


That is the problem: it to point to 2,4, Increment again, I don't
want it to point to 3,0, I want it points the next one which is 3, 3,
not 3, 0! to then end of this row, increase one , it points to 4,3 not
4,0.
Do I understand you correctly that you want it to be a column-iterator
that iterates over a column in your 2d-array? If so then increment is
easy. In fact, as long as you can create some formula for calculating
the next element you can make any kind of iterator you want since the
vector's iterator is random access (a diagonal iterator anyone).

Say you want to iterate over the 3rd column, first create a normal
iterator iter pointing to the beginning of the vector, move the iterator
to the 3rd column (iter += 2) and you are good to go. On increment do
iter += n, where n is the number of columns in the array.

The big question is how you are gonna indicate that you are at the end
of array. One way might be to make your iterator point to vector.end()
after the last element.

--
Erik Wikström
Dec 8 '06 #9

"Erik Wikström 写é“:
"The big question is how you are gonna indicate that you are at the end

"of array. One way might be to make your iterator point to vector.end()

"after the last element.

Hi Erik
I actually use my iterator for a sub-image (arbitrary size rectangle)
with my image.
I use the iterator.operator ! to indicate the end of my sub-image.

thank you.
sheldon

Dec 9 '06 #10
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...

"Jim Langston ??:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
"Jim Langston ??:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

"er****@student.chalmers.se ??:
"
On Dec 8, 2:06 pm, "food4uk" <cai_xiaod...@hotmail.comwrote:
Dear all :
I am not good at programming, please give a hand.

My data structure is very similar as an array. I actually can use
the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements. I
need to redefine the vector::iterator's behavious such as ++. It
means
"next position in the object sequence" in STL but now I redefine it
to
mean "next position within a 2-d range in the object sequence" (my
data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.

To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important
for
me to have a begin() in my container, like this:

using std::vector

class container::private vector<DATATYPE>
{

public:
using vector<DATATYPEl>::begin;

}however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would
like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).

To to that, I make the container::begin() like this :

#inlcude myIterator.h

class container::private vector<DATATYPE>
{

private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}

}// in myIterator.h , I have:

#include DATATYPE.h

class myIterator {

private:

DATATYPE* current;
.....................

public:

DATATYPE& operator*()
{
return *current;
}

}I am not sure if it is the correct way to do that. Any one can give
me
a hand is highly appreciated.
>
Since you are using a vector you can take advantage of the fact that
it's elements are stored sequentially. However it might be
better/easier to simply make your iterator a wrapper around the
vector's iterator, much like your container.
>
--
Erik Wikström
Thanks Erik !
I want to do that but actually I can not do it in that way.
Because most behivours of my iterator are different from a "raw pointer
to an array" - like the vector one. For example, the "--" , "++" "+="
etc. all are diffferent, mine iterator is working on a 2-d concept(for
example, a 2-d sub-range area of a 2-d image). But I think the
dereferencing * is the same. So It seems that I can not benefit two
much by wrapping the iterator.

Well, why don't you just use it as a 2D array then using at() the
formula
being:
row * maxrow + column

That is, [2][3] for a 5x5 array would be:
.at( 2 * 5 + 3 );

You don't need to create your own container class, because vector
supports
both [index] and .at(index). The different being .at does out of bounds
checking, [index] doesn't.


Thank you Jim!
The reason for making a customer container is for the iterator. I agree
what you said. but there is some special situations:
say, accroding to your method, If I need to return an iterator in the
position of [2,3](say for a 5x5 array), then the vector::at() will
return a reference of the DATA in the location [2,3], but what I need a
iterator in [2,3] whose behaviours is customer defined, NOT a reference
of DATA.

Off course, you can get an iterator in [2,3], but as I said, this
iterator you got is an std::vector::iterator type NOT myIterator type,
which means that I cannot get myIterator type iterator by using at(),
then I will not be able to use STL algorithm by passing iterators of
myIterator type.

What do you think?

Thank you.
Sheldon

Well, exactly how do you expect your iterator to increment?

If you have a 5 x 5 array, and you are at 2,3, when you increment you
expect
it to point to 2,4, right? Increment again, and what should it point to?
You are at the end of the row. Do you want it to point to 3,0?

If this is the behavior you want, then incrementing the normal iterator
will
do that exact thing. What other type of behavior would you want?

That is the problem: it to point to 2,4, Increment again, I don't
want it to point to 3,0, I want it points the next one which is 3, 3,
not 3, 0! to then end of this row, increase one , it points to 4,3 not
4,0.

That is why I have to define my own iterator.

What do you think?

Why are you iterating over the columns instead of the rows? The normal way
to iteratore throught a 2d array is by row first, then colums. Thats why
arrays are stored the way they are, row by row, not column by column.

Okay, but now your problem. You want to iterator through a container in an
arbitrary maner. For that I think you would need to at least wrap the
iterator.
Dec 11 '06 #11

"Jim Langston 写é“:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...

"Jim Langston ??:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
"Jim Langston ??:
"
"food4uk" <ca**********@hotmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
>
"er****@student.chalmers.se ??:
"
On Dec 8, 2:06 pm, "food4uk" <cai_xiaod...@hotmail.comwrote:
Dear all :
I am not good at programming, please give a hand.
>
My data structure is very similar as an array. I actually can use
the
std::vector as container to organize my data objects. However, the
behaviours of std::vector::iterator can not meet my requirements.I
need to redefine the vector::iterator's behavious such as ++. It
means
"next position in the object sequence" in STL but now I redefine it
to
mean "next position within a 2-d range in the object sequence" (my
data
is 2d data). The 2-d range get defined when an iterator get
initialized. Therefore, I think I have to create my own container
class and iterator class.
>
To reduce my work, I inherit my container class from the std::vector
privately, and try to use the vector::begin(), as it is important
for
me to have a begin() in my container, like this:
>
using std::vector
>
class container::private vector<DATATYPE>
{
>
public:
using vector<DATATYPEl>::begin;
>
}however, I found that the begin() I got from here returns a
vector:iterator type iterator, which is not what I want. I would
like
to see that the container::begin() can return a myIterator type
iterator which enables me to use the customerized definition of its
behivours (such as the definition of "++ "above).
>
To to that, I make the container::begin() like this :
>
#inlcude myIterator.h
>
class container::private vector<DATATYPE>
{
>
private:
myIterator pIter;
public:
.....
myIterator begin()
{
*pIter=vector<DATATYPE>::front();
return pIter;
}
>
}// in myIterator.h , I have:
>
#include DATATYPE.h
>
class myIterator {
>
private:
>
DATATYPE* current;
.....................
>
public:
>
DATATYPE& operator*()
{
return *current;
}
>
}I am not sure if it is the correct way to do that. Any one can give
me
a hand is highly appreciated.

Since you are using a vector you can take advantage of the fact that
it's elements are stored sequentially. However it might be
better/easier to simply make your iterator a wrapper around the
vector's iterator, much like your container.

--
Erik Wikström
>
Thanks Erik !
I want to do that but actually I can not do it in that way.
Because most behivours of my iterator are different from a "raw pointer
to an array" - like the vector one. For example, the "--" , "++" "+="
etc. all are diffferent, mine iterator is working on a 2-d concept(for
example, a 2-d sub-range area of a 2-d image). But I think the
dereferencing * is the same. So It seems that I can not benefit two
much by wrapping the iterator.
>
Well, why don't you just use it as a 2D array then using at() the
formula
being:
row * maxrow + column
>
That is, [2][3] for a 5x5 array would be:
.at( 2 * 5 + 3 );
>
You don't need to create your own container class, because vector
supports
both [index] and .at(index). The different being .at does out of bounds
checking, [index] doesn't.

Thank you Jim!
The reason for making a customer container is for the iterator. I agree
what you said. but there is some special situations:
say, accroding to your method, If I need to return an iterator in the
position of [2,3](say for a 5x5 array), then the vector::at() will
return a reference of the DATA in the location [2,3], but what I need a
iterator in [2,3] whose behaviours is customer defined, NOT a reference
of DATA.

Off course, you can get an iterator in [2,3], but as I said, this
iterator you got is an std::vector::iterator type NOT myIterator type,
which means that I cannot get myIterator type iterator by using at(),
then I will not be able to use STL algorithm by passing iterators of
myIterator type.

What do you think?

Thank you.
Sheldon

Well, exactly how do you expect your iterator to increment?

If you have a 5 x 5 array, and you are at 2,3, when you increment you
expect
it to point to 2,4, right? Increment again, and what should it point to?
You are at the end of the row. Do you want it to point to 3,0?

If this is the behavior you want, then incrementing the normal iterator
will
do that exact thing. What other type of behavior would you want?


That is the problem: it to point to 2,4, Increment again, I don't
want it to point to 3,0, I want it points the next one which is 3, 3,
not 3, 0! to then end of this row, increase one , it points to 4,3 not
4,0.

That is why I have to define my own iterator.

What do you think?

Why are you iterating over the columns instead of the rows? The normal way
to iteratore throught a 2d array is by row first, then colums. Thats why
arrays are stored the way they are, row by row, not column by column.

Okay, but now your problem. You want to iterator through a container in an
arbitrary maner. For that I think you would need to at least wrap the
iterator.
Hi, Jim
Is it possible to use a customer iterator with the std::vector? if so ,
how ?
Thanks.

Dec 11 '06 #12

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

Similar topics

59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
5
by: Shane | last post by:
Thanks in advance for the help. I'm new to the STL and havig a bit of trouble figuring out the whole iterator thing. I am using the <list> template and I can insert elements into the list just...
5
by: Rich S. | last post by:
I'm reading through records in a file creating bitset objects from them. Each bitset object has a score and I'm trying to collect just the top 2000 scores from over a million records. I tihnk...
5
by: Franco, Gustavo | last post by:
Hi, I have a question, and please I need a answer. How can I finalize a thread running with Application.Run (I need the message loop!!!) without call Thread.Abort?. I want to call...
0
by: faktujaa | last post by:
XML Comment tags (except summary, remarks, param and returns) don't work in my small trial program. The comment tags i want to work are see, seealso, exception, etc. But none of them work. I tried...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
0
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be...
0
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be...
2
budigila
by: budigila | last post by:
Hiya peeps, Okies, I have been trying to work this out for a while now to no avail... I am a beginner to this whole coding thing but have made great strides in my project. Basically what I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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
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,...

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.