473,320 Members | 2,024 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.

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.

Jan 25 '06 #1
2 6393
TB
Piotr sade:
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.


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
Jan 25 '06 #2
Piotr wrote:
Can I create a Random Access Iterator which start at a certain index
and end at a certain index of a container?
yes.
I go thru this page, but I can't find an example.
http://www.sgi.com/tech/stl/random_a...rator_tag.html
#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;
}
Any pointer to an example is appreciated.


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...
Jan 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
5
by: Ross MacGregor | last post by:
I have a very simple yet complicated problem. I want to generate a random list of indices (int's) for a container. Let's say I have a container with 10 items and I want a list of 3 random...
23
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you...
12
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of...
1
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length...
1
by: Kristoffer Vinther | last post by:
Hi, I need to store of parts of containers. The interface to the store could look something like this (the code in this post serves as illustrations, it is not meant to be 100% correct): ...
11
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
6
by: lukasso | last post by:
Hi, this is my code that should produce something like a timetable for a few days with each day divided into 30 minute pieces. It makes query from MySQL and then creates a 2d $array which then is to...
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...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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.