部分內容翻譯自 https://www.geeksforgeeks.org/stdstringappend-vs-stdstringpush_back-vs-operator-c/?ref=lbp
引言
C++的string類中,要想在字符串後附加字符,可以使用append函數、push_back函數或者是+=運算符,這些附加字符的方法其實現不盡相同,因此應用場景也不同。
首先我們先一窺源碼(gcc 4.9.2):
basic_string.h:
//-------------------------------+=運算符重載部分---------------------------
//追加 string 類型字符串
basic_string&
operator+=(const basic_string& __str)
{ return this->append(__str); }
//追加 cstring 類型字符串
basic_string&
operator+=(const _CharT* __s)
{ return this->append(__s); }
//追加單個字符
basic_string&
operator+=(_CharT __c)
{
this->push_back(__c);
return *this;
}
#if __cplusplus >= 201103L
//追加字符類型的初始化列表
basic_string&
operator+=(initializer_list<_CharT> __l)
{ return this->append(__l.begin(), __l.size()); }
//-------------------------------append函數實現部分---------------------------
//追加 string 類型字符串
basic_string&
append(const basic_string& __str);
//追加部分 string 類型字符串
basic_string&
append(const basic_string& __str, size_type __pos, size_type __n);
//追加部分 cstring 類型字符串
basic_string&
append(const _CharT* __s, size_type __n);
//追加 cstring 類型字符串
basic_string&
append(const _CharT* __s)
{
__glibcxx_requires_string(__s);
return this->append(__s, traits_type::length(__s));
}
//追加多個字符
basic_string&
append(size_type __n, _CharT __c);
#if __cplusplus >= 201103L
//追加字符類型的初始化列表
basic_string&
append(initializer_list<_CharT> __l)
{ return this->append(__l.begin(), __l.size()); }
#endif // C++11
template<class _InputIterator>
basic_string& //附加給定範圍內的多個字符
append(_InputIterator __first, _InputIterator __last)
{ return this->replace(_M_iend(), _M_iend(), __first, __last); }
//-------------------------------push_back函數實現部分---------------------------
//追加單個字符
void
push_back(_CharT __c)
{
const size_type __len = 1 + this->size();
if (__len > this->capacity() || _M_rep()->_M_is_shared())
this->reserve(__len);
traits_type::assign(_M_data()[this->size()], __c);
_M_rep()->_M_set_length_and_sharable(__len);
}
從以上源碼的角度來分析append函數、push_back函數和+=運算符的不同使用場景,就非常直觀了:
- += 運算符:追加單個參數值。
- append 函數:允許追加多個參數值。
- push_back 函數:只能追加單個字符。
append() | += | push_back | |
---|---|---|---|
全字符串(string) | √ | √ | × |
部分字符串(substring) | √ | × | × |
字符數組(char array) | √ | √ | × |
單個字符(char) | × | √ | √ |
迭代器範圍(iterator range) | √ | × | × |
返回值(return value) | *this | *this | none |
cstring(char*) | √ | √ | × |
示例
1 全字符串(string)
- += 運算符:可以追加完整字符串。
- append 函數:也允許追加完整字符串。
- push_back 函數:不允許追加完整字符串。
// CPP code for comparison on the
// basis of appending Full String
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str1, string str2)
{
string str = str1;
// Appending using +=
str1 += str2;
cout << "Using += : ";
cout << str1 << endl;
// Appending using append()
str.append(str2);
cout << "Using append() : ";
cout << str << endl;
}
// Driver code
int main()
{
string str1("Hello World! ");
string str2("GeeksforGeeks");
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
2 追加部分字符串(substring)
- += 運算符:不允許追加部分字符串。
- append 函數:該函數允許追加部分字符串。
- push_back 函數:不支持追加部分字符串。
// CPP code for comparison on the basis of
// Appending part of string
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str1, string str2)
{
// Appends 5 characters from 0th index of
// str2 to str1
str1.append(str2, 0, 5);
cout << "Using append() : ";
cout << str1;
}
// Driver code
int main()
{
string str1("GeeksforGeeks ");
string str2("Hello World! ");
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
3 追加 C-string(char*)
- += 運算符:允許追加 C-string。
- append 函數:同樣允許追加 C-string。
- push_back 函數:不允許使用 push_back 函數追加 C-string。
// CPP code for comparison on the basis of
// Appending C-string
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str)
{
string str1 = str;
// Appending using +=
str += "GeeksforGeeks";
cout << "Using += : ";
cout << str << endl;
// Appending using append()
str1.append("GeeksforGeeks");
cout << "Using append() : ";
cout << str1 << endl;
}
// Driver code
int main()
{
string str("World of ");
cout << "Original String : " << str << endl;
appendDemo(str);
return 0;
}
4 追加字符數組(char array)
- += 運算符:允許追加字符數組。
- append 函數:同樣允許追加字符數組。
- push_back 函數:不支持追加字符數組。
// CPP code for comparison on the basis of
// Appending character array
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str)
{
char ch[6] = {'G', 'e', 'e', 'k', 's', '\0'};
string str1 = str;
// Appending using +=
str += ch;
cout << "Using += : " << str << endl;
// Appending using append()
str1.append(ch);
cout << "Using append() : ";
cout << str1 << endl;
}
// Driver code
int main()
{
string str("World of ");
cout << "Original String : " << str << endl;
appendDemo(str);
return 0;
}
5 追加單個字符(char)
- += 運算符:允許使用 += 運算符追加單個字符。
- append 函數:不允許追加單個字符。
- push_back 函數:支持追加單個字符。
// CPP code for comparison on the basis of
// Appending single character
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str)
{
string str1 = str;
// Appending using +=
str += 'C'; cout << "Using += : " << str << endl;
// Appending using push_back()
str1.push_back('C');
cout << "Using push_back : ";
cout << str1;
}
// Driver code
int main()
{
string str("AB");
cout << "Original String : " << str << endl;
appendDemo(str);
return 0;
}
6 迭代器範圍(iterator range)
- += 運算符:不支持迭代器範圍。
- append 函數:支持迭代器範圍。
- push_back 函數:不支持迭代器範圍。
// CPP code for comparison on the basis of
// Appending using iterator range
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str1, string str2)
{
// Appends all characters from
// str2.begin()+5, str2.end() to str1
str1.append(str2.begin() + 5, str2.end());
cout << "Using append : ";
cout << str1;
}
// Driver code
int main()
{
string str1("Hello World! ");
string str2("GeeksforGeeks");
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
7 返回值(return value)
- += 運算符:返回 *this
- append 函數:返回 *this
- push_back 函數:不返回值
// CPP code for comparison on the basis of
// Return value
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
string appendDemo(string str1, string str2)
{
// Appends str2 in str1
str1.append(str2); // Similarly with str1 += str2
cout << "Using append : ";
// Returns *this
return str1;
}
// Driver code
int main()
{
string str1("Hello World! ");
string str2("GeeksforGeeks");
string str;
cout << "Original String : " << str1 << endl;
str = appendDemo(str1, str2);
cout << str;
return 0;
}