要追加字符,您可以使用运算符+=、append() 和 push_back()。所有这些都有助于附加字符,但在实现和应用方面略有不同。
- 运算符+= :附加单参数值。
- append() :允许您使用多个参数指定附加值。
- push_back() :让您一次附加一个字符。
以下是我们可以在这三者之间进行比较的一些标准:
1)完整字符串:
- += :我们可以使用 += 附加完整的字符串。
- append() :我们也可以使用 append() 附加完整的字符串。
- push_back :不允许追加完整的字符串。
CPP
// CPP code for comparison on the
// basis of appending Full String
#include
#include
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;
}
CPP
// CPP code for comparison on the basis of
// Appending part of string
#include
#include
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;
}
CPP
// CPP code for comparison on the basis of
// Appending C-string
#include
#include
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;
}
CPP
// CPP code for comparison on the basis of
// Appending character array
#include
#include
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;
}
CPP
// CPP code for comparison on the basis of
// Appending single character
#include
#include
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;
}
CPP
// CPP code for comparison on the basis of
// Appending using iterator range
#include
#include
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;
}
CPP
// CPP code for comparison on the basis of
// Return value
#include
#include
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;
}
输出:
Original String : Hello World!
Using += : Hello World! GeeksforGeeks
Using append() : Hello World! GeeksforGeeks
2)附加部分字符串:
- += :不允许附加字符串 的一部分。
- append() :允许追加字符串的一部分。
- push_back :我们不能使用 push_back 附加字符串的一部分。
CPP
// CPP code for comparison on the basis of
// Appending part of string
#include
#include
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;
}
输出:
Original String : GeeksforGeeks
Using append() : GeeksforGeeks Hello
3)附加 C 字符串 (char*):
- += :允许附加 C 字符串
- append() :它还允许附加 C 字符串
- push_back :我们不能使用 push_back() 附加 C 字符串。
CPP
// CPP code for comparison on the basis of
// Appending C-string
#include
#include
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;
}
输出:
Original String : World of
Using += : World of GeeksforGeeks
Using append() : World of GeeksforGeeks
4)追加字符数组:
- += :允许追加字符数组
- append() :允许追加字符数组。
- push_back :不允许附加字符数组。
CPP
// CPP code for comparison on the basis of
// Appending character array
#include
#include
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;
}
输出:
Original String : World of
Using += : World of Geeks
Using append() : World of Geeks
5)附加单个字符:
- += :我们可以使用 +=运算符附加单个字符。
- append() :允许附加单个字符。
- push_back :允许附加单个字符。
CPP
// CPP code for comparison on the basis of
// Appending single character
#include
#include
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
// CPP code for comparison on the basis of
// Appending using iterator range
#include
#include
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;
}
输出:
Original String : Hello World!
Using append : Hello World! forGeeks
7)返回值:
- += :返回 *this。
- append() :返回 *this
- push_back :不返回任何内容。
CPP
// CPP code for comparison on the basis of
// Return value
#include
#include
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;
}
输出:
Original String : Hello World!
Using append : Hello World! GeeksforGeeks
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。