在C++中要追加字符,可以使用运算符 +=
、append()
和 push_back()
这几种方法。 所有这些都有助于附加字符,但在实现和应用方面略有不同。
- 运算符
+=
:附加单参数值。 append()
:允许通过使用多个参数来指定附加值。push_back()
:允许一次附加单个字符。
以下是我们可以用来比较这三者的几个标准:
(1)、完整字符串:
+=
:可以使用+=
附加完整的字符串。append()
:也可以使用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("Yiibai"); cout << "Original String : " << str1 << endl; appendDemo(str1, str2); return 0; }
运行结果如下:
Original String : Hello World! Using += : Hello World! Yiibai Using append() : Hello World! Yiibai
(2)、附加部分字符串:
+=
:不允许附加字符串的一部分。append()
:允许附加字符串的一部分。push_back
:不能使用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("Yiibai "); string str2("Hello World! "); cout << "Original String : " << str1 << endl; appendDemo(str1, str2); return 0; }
运行结果如下:
Original String : Yiibai Using append() : Yiibai Hello
(3)、附加 C 字符串 (char*):
+=
:允许附加 C 字符串append()
:它还允许附加 C 字符串push_back
:不能使用push_back()
附加 C 字符串。
示例代码:
// 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 += "Yiibai"; cout << "Using += : "; cout << str << endl; // Appending using append() str1.append("Yiibai"); cout << "Using append() : "; cout << str1 << endl; } // Driver code int main() { string str("World of "); cout << "Original String : " << str << endl; appendDemo(str); return 0; }
运行结果如下:
Original String : World of Using += : World of Yiibai Using append() : World of Yiibai
(4)、附加字符数组:
+=
: 允许追加字符数组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', 'n' }; 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; }
运行结果如下:
Original String : World of Using += : World of Geeks Using append() : World of Geeks
(5)、附加单个字符:
+=
:可以使用+=
运算符附加单个字符。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; string str2 = str; // Appending using += str += 'C'; cout << "Using += : " << str << endl; // Appending using append() str2.append("C"); cout << "Using append() : "; cout << str2 << 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; }
运行结果:
Original String : AB Using += : ABC Using append() : ABC Using push_back : ABC
(6)、迭代器范围:
+=
:不提供迭代器范围。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("Yiibai"); cout << "Original String : " << str1 << endl; appendDemo(str1, str2); return 0; }
运行结果如下:
Original String : Hello World! Using append : Hello World! Yiibai
(7)、返回值:
+=
: 返回*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("Yiibai .com"); string str; cout << "Original String : " << str1 << endl; str = appendDemo(str1, str2); cout << str; return 0; }
运行结果:
Original String : Hello World! Using append : Hello World! Yiibai .com
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/264163.html