在以下代码中使用size_t(0)
与使用0
相同吗?
const string foo = "testing";
const size_t pos = foo.find("i");
string name = foo.substr(size_t(0), pos);
也就是说,当我只输入0
时,它是否转换为size_t(0)
?如果是这样,一种形式优于另一种形式吗?我的猜测是size_t(0)
是最好的选择,因为它使转换变得明确。另一方面,也许某些程序员认为size_t(0)
很冗长?我猜测实际上没有人会在乎这两种方式。
您将类型0强制转换为相同的参数类型,这是一种很好的做法,但不必要,因为这将被隐式转换。
basic_string substr( size_type pos = 0,
size_type count = npos );
签出string substr ( size_t pos = 0, size_t n = npos ) const;
。