STL Iterators

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

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
 
A

Andrew McDonagh

Christian said:
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
 
C

Christian Christmann

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?

Chris
 
D

Duane Hebert

Christian Christmann said:
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);
 
C

Christian Christmann

Christian Christmann said:
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 said:
vector<int>::iterator iter = data.begin() + (currentbit + 1);

Thank you very much, this works ;)

Chris
 
I

Ioannis Vranos

Christian said:
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".


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;
 
I

Ioannis Vranos

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Subtyping iterators 5
Strange bug with iterators 3
Understanding how to design STL-compatible iterators 4
STL Vector Access 1
2 Iterators/Iterator Math 1
Declaring iterators 7
Iterators 8
Sorting an STL map 1

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top