#include <iostream>
#include <string>
using namespace std;
int main()
{
string fname;
string lname;
string name;
cout<<"Enter your first name: ";
getline(cin,fname);
cout<<"Enter your last name: ";
getline(cin,lname);
name=lname+", "+fname;
cout<<"Here's the information in a single string: "<<name<<endl;
system("pause");
return 0;
}
与使用char数组和头文件cstring中的函数比较,string对象和string中函数,操作简单,尤其对复杂的问题优势更加明显。