# iterators

• 02-09-2005
strickey
iterators
I got index of biggest element in a vector by using:
std::max_element(grey.begin(),grey.end());
now I need that iterator to use for calculations and also as the iterator for some other vector. How can I convert an iterator into int or double type and how can I use it as an iterator for some other vector?
• 02-09-2005
hk_mp5kpdw
Quote:

Originally Posted by strickey
I need that iterator to use for calculations

Use for calculations how?

Quote:

Originally Posted by strickey
and how can I use it as an iterator for some other vector?

Depends on how it is used and what for.

I think we need a better idea of what you are trying to do with the iterator and your two vectors.
• 02-09-2005
strickey
I have two vectors of same size.
I need to find an index of the greatest number in the first vector and find element who has same index in the second vector.
I need also that iterator for calculations of some formulas.
• 02-09-2005
pianorain
Isn't a deferenced iterator just a reference to the value?
Code:

```int main() {     vector<int> a(10);     for(int i = 0; i < 10; i++)         a[i] = i;     vector<int>::iterator b = max_element(a.begin(),a.end());     cout << *a.begin() << " " << *b << endl;  //should output 0 9     return 0; }```
If you need the value that a vector iterator points to, I think you can just dereference the iterator.
• 02-09-2005
strickey
iterator as iterator
Code:

```int main() {     int l;     vector<int> d;     for(int i = 0; i < 15; i++)     {        cin<<p;     d.push_back(p);     }           vector<int> a(10);     for(int i = 0; i < 10; i++)         a[i] = i;     vector<int>::iterator b = max_element(a.begin(),a.end());     l=d[b];//here I get error C2679 // }```
Error says that I cannot put the iterator on the right side of [
• 02-09-2005
pianorain
Heh, you'll have to search vector d for the value in b. I'm sure there's an STL algorithm for it. You might check http://www.sgi.com/tech/stl/table_of_contents.html. Looks like the find algorithm will do it for you.
• 02-09-2005
hk_mp5kpdw
I'm sorry if my previous post mislead you, the max_element function returns an iterator (a pointer) and not the index of the largest value. To get an index you could use the distance function:

Code:

```int main() {     int l;     vector<int> d;     for(int i = 0; i < 15; i++)     {        cin<<p;     d.push_back(p);     }           vector<int> a(10);     for(int i = 0; i < 10; i++)         a[i] = i;     int index = distance(a.begin(),max_element(a.begin(),a.end()));     l=d[index]; }```
• 02-15-2005
Chaplin27
Quote:

function returns an iterator (a pointer)
Not to just hop in here or anything, but what exactly is an iterator? It works like a pointer, so is it a pointer? I wasnt sure if you were saying iterator(a pointer) to be like "it acts exactly like a pointer" or "an iterator is a pointer, period"...just checking...
and if it is a pointer, just out of random curiosity, would it be possible to dynamically allocate memory with an iterator? Obviously if its not a pointer then disregard that last question. Ah just stupid curious questions...- Chap
• 02-15-2005
CornedBee
An iterator is an abstract concept. It's "an object that provides pointer-like semantics to access elements of a real or virtual collection."

The best-known iterators are those that iterate over standard containers. vector::iterator behaves like a real pointer (and often is just a typedef for one). deque::iterator behaves like a pointer, but isn't one. list::iterator doesn't allow full arithmetics. But in all these cases, the collection is a real, tangible set of elements.

But there are weird iterators. istream_iterator "iterates" over the input it reads as it goes. There are iterators that generate random or patterened elements as you move along. There are iterators that concatenate the sequences of other iterators (I've wrote one). There are ...

You imagination is the limit, really. In the end, all that's important is that it has the typical iterator semantics.
• 02-15-2005
Chaplin27
wow thats, so far, the best response I've ever received on these board to any of my questions...You just presented a good example of why I love programming so much...there's this vast space of all these algorithms and idioms and various pieces of knowledge like what container is more appropriate for this or that...awesome, oh...thanks for the reply :p -Chap
• 02-15-2005
7stud
Good question Chap, and a timely one for me, and a great answer CornedBee. To add, my book says:

"(An iterator is) a form of a smart pointer..."

"...smart pointer--something that behaves like a pointer but is really a class object...It can hide the complication in the organization of the objects it points to...In a very complex application...you might have objects that are stored in files or databases across multiple systems on a network, but within your program you want a smart pointer object to take care of sorting out how to retrieve the objects...All of the complexity of accessing the objects pointed to by a smart pointer is buried inside the class that defines it. Externally, you would expect to use a smart pointer pretty much as you would an ordinary pointer. "
(Ivor Horton's Beginning C++)

Then it goes on to show how it's done, which essentially involves defining what you want the *operator and the -> operator to do when used with your 'smart pointer' object. So, in the example mentioned above with the complex application and data spread over multiple systems, you could define the ->operator to open up certain files or search databases across the network for the objects you are interested in--all of which would be set into motion by using the simple pointer notation: ->