[英]How the ++ operator of the const_iterator works for vector<string> in C++ standard library?
String size is not fixed.字符串大小不固定。 How they made iterator knows where to point to after the increment?
他们如何让迭代器知道在增量后指向哪里?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> myVector{"ABC", "DEF"};
for (auto p = myVector.begin(); p != myVector.end(); ++p)
cout << *p << endl;
}
In fact, sizeof(std::string)
is fixed.实际上,
sizeof(std::string)
是固定的。 The actual string data (which is not of fixed size) is stored elsewhere in memory, and the std::string
object only contains a pointer to it.实际的字符串数据(不是固定大小)存储在内存中的其他地方,而
std::string
对象只包含一个指向它的指针。
So an iterator over std::vector<std::string>
can simply advance by sizeof(std::string)
bytes at a time.因此,
std::vector<std::string>
的迭代器一次可以简单地前进sizeof(std::string)
个字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.