Thread: Difference between a const iterator and const_iterator

  1. #1
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51

    Difference between a const iterator and const_iterator

    Well, I had a question in my book asking this. I was wondering if this is right?:

    "You would use an iterator that is const when you will just be reading/writing from ONE element in a container. You would use const_iterator when you will just be READING from one or more elements in a container. The difference between them is the fact a const iterator's location cannot be changed where as with a const_iterator the value of the location cannot be changed."

    Am I on right track here? (I hope you don't mind me asking all these questions here, but I want to make sure I'm getting all this) Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    A const_iterator is a misnomer for an "iterator to const". What your book means by a "const iterator" is an iterator that is const. Your interpretation sounds correct, though you can modify an object using an iterator that is const, assuming that what it points to is not const.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 01:06 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. vector<>
    By teval in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2003, 03:27 PM
  5. difference between: const char *, char *
    By creeping_death in forum C Programming
    Replies: 3
    Last Post: 08-04-2003, 11:51 PM