2011-11-16 47 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);