iterators

This is a discussion on iterators within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    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?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    50
    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.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    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 [

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    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];
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    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

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    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 -Chap

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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: ->
    Last edited by 7stud; 02-15-2005 at 11:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  2. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 02:19 AM
  3. Writing Iterators...
    By DeepBlackMagic in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM
  4. accessing vector elements: iterators are faster?
    By Captain Penguin in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2002, 01:27 PM
  5. Generic Progpammimg Iterators
    By rip1968 in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2002, 10:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21