📅  最后修改于: 2020-09-25 06:36:29             🧑  作者: Mango
您可以使用=赋值运算符简单地在C++中复制字符串对象。
#include
using namespace std;
int main()
{
string s1, s2;
cout << "Enter string s1: ";
getline (cin, s1);
s2 = s1;
cout << "s1 = "<< s1 << endl;
cout << "s2 = "<< s2;
return 0;
}
输出
Enter string s1: C++ Strings
s1 = C++ Strings
s2 = C++ Strings
要在C++中复制c字符串,请使用strcpy()
函数 。
#include
#include
using namespace std;
int main()
{
char s1[100], s2[100];
cout << "Enter string s1: ";
cin.getline(s1, 100);
strcpy(s2, s1);
cout << "s1 = "<< s1 << endl;
cout << "s2 = "<< s2;
return 0;
}
输出
Enter string s1: C-Strings
s1 = C-Strings
s2 = C-Strings