部分内容翻译自 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:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79//-------------------------------+=运算符重载部分--------------------------- //追加 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 函数:不允许追加完整字符串。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36// 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 函数:不支持追加部分字符串。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31// 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。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36// 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 函数:不支持追加字符数组。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36// 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', ''}; 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 函数:支持追加单个字符。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34// 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 函数:不支持迭代器范围。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31// 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 函数:不返回值
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32// 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; }
(全文完)
最后
以上就是傻傻蜗牛最近收集整理的关于C++的string类型中关于append函数、push_back函数和+=运算符的区别的全部内容,更多相关C++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复