C++ C+中的Concat字符串+;(STL)
我有这样的代码C++ C+中的Concat字符串+;(STL),c++,string,stl,C++,String,Stl,我有这样的代码 string xml_path(conf("CONFIG")); xml_path+=FILE_NAME; 在哪里,, conf函数返回char*,文件名为const char* 我想把它合并成一行,就像 xml_path(conf("CONFIG")).append(FILE_NAME) 我该怎么做 有什么建议吗 const char * f = "foo"; char * b = "bar"; string s = string( f ) + b; 请注意,您不能
string xml_path(conf("CONFIG"));
xml_path+=FILE_NAME;
在哪里,,
conf函数返回char*
,文件名为const char*
我想把它合并成一行,就像
xml_path(conf("CONFIG")).append(FILE_NAME)
我该怎么做
有什么建议吗
const char * f = "foo";
char * b = "bar";
string s = string( f ) + b;
请注意,您不能使用append(-0),因为INVOLVED的两个字符串都不是std:;字符串。如果确实要追加,则必须是两个阶段的过程:
string s ( f );
s.append( b );
应该这样做。或者,如果要格式化不同类型的变量,请使用ostringstream 例如
std::ostringstream oss;
INTA=2;
char*s=“sometext”;
oss关于一行的问题:
string xml_path = string(conf("CONFIG")) + string(FILE_NAME);
(我假设xml\u path
是变量的名称,而不是我不知道的库中的某种调用)。每个人都忽略了一个事实,即您要求的是一行解决方案。(但您为什么要这样做呢?)假设我要包含5个字符串,那么我就可以这样做。append().append()我不需要像在java StringBuffer obj=newStringBuffer(“foobar”).append(“foo”).append(“bar”);
std::ostringstream oss;
int a = 2;
char *s = "sometext";
oss<<s<<a<<endl;
cout<<oss.str(); // will output "sometext2"
string xml_path = string(conf("CONFIG")) + string(FILE_NAME);