VC中使用+=或者+

来源:百度知道 编辑:UC知道 时间:2024/06/04 07:21:52
VC++中怎么使用连接字符串
例如:
在C#可以这么写:string s="h"+"h";或者string s=f+"_end";
VC++中怎么写
如果f是char* 类型的怎么写

string s3="Test_";
strcat(s3,signcode);
提示:error C2664: “strcat”: 不能将参数 1 从“std::string”转换为“char *”
signcode是char*类型的

VC 中不能这样写:string s="h"+"h";
两个常量指针不能相加赋值给s。
需要用
string f="kkk",s;
s=f+"_end";

如果是char*类型,可以使用strcpy()和strcat()函数:
char *s = new char[30];
//实现string s="h"+"h";
strcpy(s,"h");//copy"h"到s变量,s值为"h"
strcat(s,"h");//追加"h"到s变量,此时s值为"hh"

//实现string s=f+"_end";
char *f = "eyi";
strcpy(s,f);
strcat(s,"_end");

//delete[] s;

补充:
strcat是给char *用的。
string类型直接相加就可以了:
char *signcode = "abc";
string s3="Test_";
s3 += signcode;

#include <string>
using namespace std;
void main()
{string s="h"+"h";
string f="kkk",s;
s=f+"_end";
}