473,320 Members | 2,071 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,320 software developers and data experts.

STL Iterators

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
8 1595
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
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

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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Steven Bethard | last post by:
So, as I understand it, in Python 3000, zip will basically be replaced with izip, meaning that instead of returning a list, it will return an iterator. This is great for situations like: zip(*)...
18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
1
by: Marcin Kaliciñski | last post by:
template<class RanAccIt> void some_algorithm(RanAccIt begin, RanAccIt end) { // this algorithm involves calling std::lexicographical_compare // on range [begin, end), and on reverse of this range...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
8
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
14
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.