Connecting Tech Pros Worldwide Help | Site Map

Random Access Iterator

  #1  
Old January 25th, 2006, 06:05 PM
Piotr
Guest
 
Posts: n/a

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

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

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...
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method Underscores? Chris S. answers 31 July 18th, 2005 06:16 PM