471,071 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 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 1504
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 Wikstrm

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 Wikstrm
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 Wikstrm

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 Wikstrm
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

59 posts views Thread by Raymond Hettinger | last post: by
5 posts views Thread by Shane | last post: by
5 posts views Thread by Rich S. | last post: by
5 posts views Thread by Franco, Gustavo | last post: by
reply views Thread by faktujaa | last post: by
reply views Thread by leo001 | last post: by

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.