关于string类的用法
#include "stdafx.h"using namespace std ;
//# include<stdio.h>
int main()
{
string name;
string dessert;
getline(cin,name);
strcat(name," ,");
getline(cin,dessert);
cout<<name+dessert;
}
在strcat出错,大概是不能直接接字符串
然
int main()
{
string name;
string dessert;
getline(cin,name);
//strcat(name," ,");
getline(cin,dessert);
cout<<name+","+dessert;
}
const int SIZE = 20;
int main()
{
using namespace std;
char firstName[SIZE];
char lastName[SIZE];
char fullName[2*SIZE + 1];
cout << "Enter your first name: ";
cin >> firstName;
getchar();
cout << "Enter your last name: ";
cin >> lastName;
strncpy(fullName,lastName,SIZE);
strcat(fullName, ", ");
strncat(fullName, firstName, SIZE);
fullName[SIZE - 1] = '\0';
cout << "Here's the information in a single string: "
<< fullName << endl;
return 0;
}
这两种做法都对
可否说明下为什么string定义的字符串数组 不能用strcat 进行连接字符串呢?又或是用getline()的原因?