【发布时间】:2011-12-30 04:55:42
【问题描述】:
std::string 中的两个重载函数引起了我的注意:
string& append(const string& str, size_t pos, size_t n);
string& append(const char* s, size_t n);
我很好奇为什么char*版本的string::append()没有提供额外的参数size_t pos,如下:
string& append(const char* s, size_t pos, size_t n);
对于其他两个函数,情况也一样:
int compare(size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2) const;
int compare(size_t pos1, size_t n1, const char* s, size_t n2) const;
string& replace(size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2);
string& replace(size_t pos1, size_t n1, const char* s, size_t n2);
这些函数的 char* 版本缺少参数size_t pos2,它不如它们的 string& 对应物灵活。我的问题如下:
- std::string为什么要这样设计它的接口?
- 为什么char*版本函数没有
size_t pos? - 这背后的考虑是什么?
感谢您的阅读!
【问题讨论】: