开发者

How Iterator pointers in the STL work

Hey i'm a little confused on how iterators work. Specifically a const_iterator for a class in this case. I understand that they are really just pointers to specific elements of a string or whatever yo开发者_开发知识库u're working with, but in that case: I would think of them as being a memory address, making it so you could not just add integers to get to the next item in the string.

Here's the code I'm confused about:

string::const_iterator iCharacterLocater;

for ( iCharacterLocater = strSTLString.Begin();
      iCharacterLocater != strSTLString.end();
      ++ iCharacterLocater )
{
    cout << "Character [ " << nCharOffset ++ <<"] is: ";
    cout << *iCharacterLocater << endl;
}

Thanks!! =)


It's a bit more complicated than that. Iterators refer to the GOF design pattern of the same name, and, in C++, are applied in such a way that they look just like pointers. C++ lets you overload operators, which means that custom types can behave in specific ways when certain operators are used on them.

There are several types of iterators in C++, with varying feature levels. The underlying representation of a vector and of a string are both simple enough that iterators behave pretty much exactly like pointers: you can add numbers to them to seek a specific item. For instance, myVector.begin() + 5 returns an iterator to the 6th element of the vector (6th because indices are zero-based, and 0 would be the first). String iterators also let you do this.

String constant iterators behave much like constant char pointers. When you see const char* foo, it doesn't mean you can't change foo–it just means you can't change what it points to. So when you see std::string::const_iterator foo, it doesn't mean you can't change foo. It just means you can't change what it references.

const char* foo = "abcd";
foo = "zyxwvu"; // valid: the pointer itself can be changed
*foo = 't'; // invalid: the pointed data can't be changed
*(foo + 2) == 'x'; // true

std::string myString = "zyxwvu";
std::string::const_iterator foo = myString.begin();
foo = myString.begin() + 2; // valid: the iterator itself can be changed
*foo = 't'; // invalid: the pointed data can't be changed
*(foo + 2) == 'x'; // true

If you don't attempt to modify data, iterators and const iterators behave just the same. Also, except that there is no direct conversion between the two, a pointer to the contents of a string and an iterator to the contents of a string behave exactly the same.


They are a memory address. The thing about these STL containers is that when you store 10 elements in one, those elements are all adjacent just for this reason. So that when you itr++, it points to the next memory address, which is guaranteed to be the next element.

So an std::string just sits on top of a character array. Those characters are all side by side in memory. If you're pointing to one character, and you increment the pointer, you are now pointing to the next pointer. std::string::begin() returns a pointer to the first character, and std::string::end() returns a pointer to the position after the last character.

const std::string::iterator means that you can't modify the iterator. So you can't increment it, or make it point to something else.

std::string::const_iterator means you can't modify the value that the iterator points to. So you can change which value the iterator is pointing to (itr++), but you can't change the actual value at that memory address.


Iterator is basically a pointer, correct ! But what really important is to what it pointing.

What I think is, iterator is the pointer to the structure defined as type. For example,

  • for vector of type string, iterator will be a pointer to the string structure (String is the system defined structure)
  • For vector of type classA, iterator will be a pointer to the classA structure (basically class is a user defined structure)

The beauty of defining iterator in this way is, we can defined everything in structure form, so we can retrieve everything by defining iterator to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜