By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

STL Iterators

P: n/a
Hi,

my program contains a class with a STL vector std::vector<int> data and an
integer variable "currentbit" which indicates the current position while
traversing the vector.

In my function first() I'm searching for a particular value and save the
position in "currentbit". Within another function next() I'd like to
continue searching beginning in the vector at position "currentbit". My
idea was to create an iterator:

vector<int>::const_iterator iter = *data[currentbit+1]

but the compiler complains:

invalid type argument of `unary *'

What's wrong? How can I assing an iterator to a particular position in the
vector?

Thanks
Chris
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Christian Christmann wrote:
Hi,

my program contains a class with a STL vector std::vector<int> data and an
integer variable "currentbit" which indicates the current position while
traversing the vector.

In my function first() I'm searching for a particular value and save the
position in "currentbit". Within another function next() I'd like to
continue searching beginning in the vector at position "currentbit". My
idea was to create an iterator:

vector<int>::const_iterator iter = *data[currentbit+1]

but the compiler complains:

invalid type argument of `unary *'

What's wrong? How can I assing an iterator to a particular position in the
vector?

Thanks
Chris


its complaining because *data[currentBit+1] is not an iterator.

It would be simpler to declare currentBit to be an Iterator rather than
an (I'm assuming) int.

so your first() method would use iterators to loop through the vector to
find the specific element and when found, instead of assigning an int
value to currentBit, you would: currentBit = iter;

Then when your next() method is called, you can simply do

return currentBit++;

Andrew
Jul 23 '05 #2

P: n/a
In my function first() I'm searching for a particular value and save the
position in "currentbit". Within another function next() I'd like to
continue searching beginning in the vector at position "currentbit". My
idea was to create an iterator:

vector<int>::const_iterator iter = *data[currentbit+1]
its complaining because *data[currentBit+1] is not an iterator.

It would be simpler to declare currentBit to be an Iterator rather than an
(I'm assuming) int.
Thank you for your suggestion. That's a good idea but I need currentBit as
int for some other calculations. So I don't want to change it to an
iterator.

Is there another way to assign an iterator to an particular int array
position?

Andrew


Chris
Jul 23 '05 #3

P: n/a

"Christian Christmann" <pl*****@yahoo.de> wrote in message news:3a*************@individual.net...
In my function first() I'm searching for a particular value and save the
position in "currentbit". Within another function next() I'd like to
continue searching beginning in the vector at position "currentbit". My
idea was to create an iterator:

vector<int>::const_iterator iter = *data[currentbit+1]

its complaining because *data[currentBit+1] is not an iterator.


What's the *data for?
Have you tried

vector<int>::iterator iter = data[currentbit + 1];
or
vector<int>::iterator iter = data.begin() + (currentbit + 1);
Jul 23 '05 #4

P: n/a
On Sat, 26 Mar 2005 14:27:58 -0500, Duane Hebert wrote:

"Christian Christmann" <pl*****@yahoo.de> wrote in message
news:3a*************@individual.net...
>> In my function first() I'm searching for a particular value and save
>> the position in "currentbit". Within another function next() I'd like
>> to continue searching beginning in the vector at position
>> "currentbit". My idea was to create an iterator:
>>
>> vector<int>::const_iterator iter = *data[currentbit+1]
> its complaining because *data[currentBit+1] is not an iterator.


What's the *data for?
Have you tried


std::vector<int> data;
vector<int>::iterator iter = data.begin() + (currentbit + 1);


Thank you very much, this works ;)

Chris

Jul 23 '05 #5

P: n/a
Christian Christmann wrote:
Hi,

my program contains a class with a STL vector std::vector<int> data and an
integer variable "currentbit" which indicates the current position while
traversing the vector.

Which would be better if it was replaced wig vector<int>::size_type.

In my function first() I'm searching for a particular value and save the
position in "currentbit".

Better use std::search family.
Within another function next() I'd like to
continue searching beginning in the vector at position "currentbit". My
idea was to create an iterator:

vector<int>::const_iterator iter = *data[currentbit+1]


If you want to continue from currentbit+1, and data is your vector<int>,
you can do:

vector<int>::const_iterator iter = data.begin()+currentbit+1;

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #6

P: n/a
Ioannis Vranos wrote:
Hi,

my program contains a class with a STL vector std::vector<int> data
and an
integer variable "currentbit" which indicates the current position while
traversing the vector.


Which would be better if it was replaced wig vector<int>::size_type.

In my function first() I'm searching for a particular value and save the
position in "currentbit".
Better use std::find family.
Within another function next() I'd like to
continue searching beginning in the vector at position "currentbit". My
idea was to create an iterator:

vector<int>::const_iterator iter = *data[currentbit+1]



If you want to continue from currentbit+1, and data is your vector<int>,
you can do:

vector<int>::const_iterator iter = data.begin()+currentbit+1;



--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7

P: n/a

"Christian Christmann" <pl*****@yahoo.de> wrote in message news:3a*************@individual.net...
On Sat, 26 Mar 2005 14:27:58 -0500, Duane Hebert wrote:
vector<int>::iterator iter = data.begin() + (currentbit + 1);


Thank you very much, this works ;)


Should work with const_iterator as well. I forgot it in
the example.
Jul 23 '05 #8

P: n/a
"Christian Christmann" <pl*****@yahoo.de> wrote in message
news:3a*************@individual.net...
On Sat, 26 Mar 2005 14:27:58 -0500, Duane Hebert wrote:
> vector<int>::iterator iter = data.begin() + (currentbit + 1);


Thank you very much, this works ;)


Should work with const_iterator as well. I forgot it in the example.


Thanks, I used the const_iterator

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.