string::substr(pos, n)表示截取字符串string,从pos下标开始,长度为n的子串。
比如:str = “helloworld”,那么str.substr(2, 3)等于“llo”。
1分析substr()源码
==substr()==源码如下:
basic_string substr(size_type __pos = 0, size_type __n = npos) const {
if (__pos > size())
_M_throw_out_of_range();
return basic_string(_M_start + __pos,
_M_start + __pos + min(__n, size() - __pos));
}
大意为,如果==__n的默认缺省等于npos的话,就截取从__pos==到末尾的全部字符串。
链接: 左旋转字符串.