要附加字符,可以使用运算符+ =,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:不允许追加char数组。
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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。