Thread: Subtracting offsets from iterators

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    11

    Subtracting offsets from iterators

    Hi all,

    Got a quick question regarding iterators and subtracting offsets and I was wondering if you can help me out here. I had some code that was accessing a vector like such:

    Code:
    	for (int i = 0; i < m_linkArray.size() - 1; i++) {
    		m_linkArray[i]->update();
    	}
    .. but wanted to change it so that it used iterators. The following compiles and runs okay - just wondering however if the subtraction of one to offset the iterator is correct:

    Code:
    	std::vector <CLink *>::iterator itPos = m_linkArray.begin();
    	
    	for (; itPos < m_linkArray.end() - 1; itPos++)
    	{
    		(*itPos)->update();
    	}
    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it is incorrect. Since .end() gives you an iterator just beyond the end, iterating to end() is the correct logic, or you will miss the last element.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Additionally, if you are not going to use the iterator outside of the loop, then limit the iterator to the scope of the loop by writing it like this:
    Code:
    for (std::vector<CLink*>::iterator itPos = m_linkArray.begin(); itPos != m_linkArray.end(); ++itPos)
    {
        (*itPos)->update();
    }
    Note that I used operator!= instead of operator< since you do not actually need operator< (and it helps if you need to change the container to one without random access iterators). It is also more idiomatic to use pre-increment rather than post-increment (unless post-increment is actually useful in the context) since it can be more efficient and is almost certainly no less efficient.

    You could also consider using a standard generic algorithm instead of an explicit loop, e.g.,
    Code:
    std::for_each(m_linkArray.begin(), m_linkArray.end(), std::mem_fun(&CLink::update));
    Last edited by laserlight; 12-27-2008 at 02:37 AM. Reason: Fixed typo.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    11
    Ah yeah - thanks - I was totally reading that initial code wrong when I tried to re-implement it with iterators. Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithms over vector: offsets or iterators for speed?
    By pheres in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 02:23 AM
  2. iterators?
    By Warrax in forum C++ Programming
    Replies: 6
    Last Post: 06-11-2007, 03:13 PM
  3. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  4. Writing Iterators...
    By Geolingo in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM
  5. accessing vector elements: iterators are faster?
    By Captain Penguin in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2002, 02:27 PM