Subtracting offsets from iterators

This is a discussion on Subtracting offsets from iterators within the C++ Programming forums, part of the General Programming Boards category; Hi all, Got a quick question regarding iterators and subtracting offsets and I was wondering if you can help me ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    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 01:37 AM. Reason: Fixed typo.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 DeepBlackMagic 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, 01:27 PM

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