Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 25th, 2006, 06:05 PM
Piotr
Guest
 
Posts: n/a
Default Random Access Iterator


Can I create a Random Access Iterator which start at a certain index
and end at a certain index of a container?

I go thru this page, but I can't find an example.
http://www.sgi.com/tech/stl/random_a...rator_tag.html

Any pointer to an example is appreciated.

  #2  
Old January 25th, 2006, 06:15 PM
TB
Guest
 
Posts: n/a
Default Re: Random Access Iterator

Piotr sade:[color=blue]
> Can I create a Random Access Iterator which start at a certain index
> and end at a certain index of a container?
>
> I go thru this page, but I can't find an example.
> http://www.sgi.com/tech/stl/random_a...rator_tag.html
>
> Any pointer to an example is appreciated.
>[/color]

You mean like:?

std::vector<int> v;
// populate v
std::vector<int>::iterator start = v.begin(), end;
start += 3;
end = start + 10;
for( ; start != end; ++start);

--
TB @ SWEDEN
  #3  
Old January 25th, 2006, 06:35 PM
Ben Pope
Guest
 
Posts: n/a
Default Re: Random Access Iterator

Piotr wrote:[color=blue]
> Can I create a Random Access Iterator which start at a certain index
> and end at a certain index of a container?[/color]

yes.
[color=blue]
> I go thru this page, but I can't find an example.
> http://www.sgi.com/tech/stl/random_a...rator_tag.html[/color]

#include <vector>

int main() {
const size_t size = 128;
std::vector<int> v(size, 3);

// create random access iterator
std::vector<int>::iterator iter = v.begin();

// Access objects with "random" offset (less than size)
iter += 6;
}
[color=blue]
> Any pointer to an example is appreciated.[/color]

Does the above help?

Or do you want to *implement* a random iterator?

Not all containers support random access, in which case you can create a
random access iterator class by overriding +, +=, -, -= etc, and using
the ++ and -- operator of the forward and reverse iterators of the
underlying type, in a for loop.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles