【发布时间】:2020-01-16 17:04:31
【问题描述】:
使用 const 迭代器遍历字符串时,它永远不会结束。在遍历期间从不修改字符串。但是itr 永远不会到达src.cend()。最后程序崩溃了。
此功能是将域名转换为标准域名以进行DNS查询。例如,www.abc.com 转换为 3www3abc3com。
string name_hton(const string &src) {
string dst;
auto itr = src.cbegin(), last = itr;
do {
while (itr != src.cend() && *itr != '.') {
++itr;
}
auto diff = itr - last;
// Push the label length
dst.push_back(diff);
// Push the label
dst.append(last, itr);
++itr;
last = itr;
} while (itr != src.cend());
return dst;
}
【问题讨论】:
-
是时候用你的调试器逐步完成它了。
-
@Nico238 这不是格式错误