2011-11-16 50 views
2

两个重载函数中的std :: string了我的注意:串与char *字符串的版本::追加(),字符串替换::()和字符串::比较()

string& append(const string& str, size_t pos, size_t n); 
string& append(const char* s, size_t n); 

我很好奇的是为什么串::附加的字符*版本()不提供额外的参数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,这不像它们的字符串&副本那样灵活。我的问题如下:

  1. 为什么std :: string设计它的接口是这样的?
  2. 为什么char *版本函数还有size_t pos呢?
  3. 背后有什么考虑?

谢谢您的阅读!

回答

4

因为你可以再补充poss

str.append(ptr + pos, len); 

这并不是说,它不会是一个很好的速记有,但他们(一般)只需要最低限度地增加必要的功能,而不是简单的包装类。

+0

+1;) – chill

+0

此外,'str.append(PTR,POS,LEN);'只有一个字符不是'str.append短(PTR + POS ,len);'(这取决于你的空白习惯)。 – zneak

2

也许你能快速反应做简单str.append(s+pos, n);