我正在审查另一个开发人员的代码,其中包含以下内容:
std::string name;
...
std::string::size_type colon = name.find(":");
我认为使用 size_t
会更容易理解并且同样安全,因为 STL 标准规定 std::string
是std::basic_string<char, std::allocator>
, 和 std::allocator::size_type
是size_t
.
他希望确保 STL 标准永远不会改变,从而使该假设无效;如果标准可以改变,那么 size_type
会比 size_t
更安全.
这会发生吗?还有其他使用原因吗 size_type
而不是 size_t
?
最佳答案
size_type
应该用在基于容器类型的模板化代码中。当你一直在操作std::string
的代码时,你可以直接使用size_t
。
关于c++ - 使用 std::string::size_type 而不是 size_t 有什么好处吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10520058/