C Style Strings [英] C Style Strings
问题描述
我一直使用std :: string但我不得不使用第三方库
返回const字符*秒。鉴于:
char * pString1 =" Blah" ;;
const char * pString2 =" Blah Blah";
>
如何将pString2的内容追加到pString? (给出Blah
Blah Blah)
我看了strcat,但是因未处理的异常而崩溃。
谢谢
Hi,
I''ve always used std::string but I''m having to use a 3rd party library
that returns const char*s. Given:
char* pString1 = "Blah ";
const char* pString2 = "Blah Blah";
How do I append the contents of pString2 to pString? (giving "Blah
Blah Blah")
I looked at strcat but that crashes with an unhandled exception.
Thanks
推荐答案
scroopy写道:
scroopy wrote:
我一直使用std :: string但是我不得不使用第三方库
返回const char * s。鉴于:
char * pString1 =" Blah";
const char * pString2 =" Blah Blah" ;;
如何追加内容pString2到pString? (给出Blah
Blah Blah)
我看着strcat,但是因未处理的异常而崩溃。
Hi,
I''ve always used std::string but I''m having to use a 3rd party library
that returns const char*s. Given:
char* pString1 = "Blah ";
const char* pString2 = "Blah Blah";
How do I append the contents of pString2 to pString? (giving "Blah
Blah Blah")
I looked at strcat but that crashes with an unhandled exception.
那个很简单,按C方式做:
char * result = new char [strlen(s1)+ strlen(s2)+1];
strcpy(结果,s1);
strcat(结果,s2);
问候
Jiri Palecek
That''s easy, do it the C way:
char* result=new char[strlen(s1)+strlen(s2)+1];
strcpy(result,s1);
strcat(result,s2);
Regards
Jiri Palecek
scroopy写道:
scroopy wrote:
我一直用std :: string但我不得不使用第三方库
返回const char * s。鉴于:
char * pString1 =" Blah";
const char * pString2 =" Blah Blah" ;;
如何追加内容pString2到pString? (给出Blah
Blah Blah)
Hi,
I''ve always used std::string but I''m having to use a 3rd party library
that returns const char*s. Given:
char* pString1 = "Blah ";
const char* pString2 = "Blah Blah";
How do I append the contents of pString2 to pString? (giving "Blah
Blah Blah")
a)继续使用std :: string作为你自己的东西。 std :: string类有
方法,允许你与
库的C风格字符串接口进行交互。
b )请注意,在上面的示例中,pString1初始化为const
字符串。任何修改该字符串的尝试都将是未定义的行为。
的危险在于没有将pString1声明为char const *。
c)你可以这样做:
#include< string>
#include< algorithm>
char * strdup(std :: string str){
char * result = new char [str.length()+1];
std :: copy(str.begin(),str.end(),result);
结果[str.length()] = 0;
返回(结果);
}
int main (void){
char * pString1 =" Blah" ;;
char const * pString2 =" Blah Blah";
pString1 = strdup(std :: string(pString1).append(pString2));
}
但是,我更喜欢使用std :: string作为我自己的东西:
#include< string>
int main(void){
std :: string pString1 = Blah; //由某些库返回的rhs
std :: string pString2 =" Blah Blah" ;; //由某些库返回的rhs
pString1.append(pString2);
}
最佳
Kai-Uwe Bux
a) Keep using std::string for your own stuff. The std::string class has
methods that allow you to interact with C-style string interfaces of
libraries.
b) Note that in your example above, pString1 is initialized to a const
string. Any attempt to modify that string would be undefined behavior. The
danger lies in not declaring pString1 as a char const *.
c) You could do:
#include <string>
#include <algorithm>
char * strdup ( std::string str ) {
char * result = new char [ str.length() +1 ];
std::copy( str.begin(), str.end(), result );
result[ str.length() ] = 0;
return ( result );
}
int main ( void ) {
char * pString1 = "Blah ";
char const * pString2 = "Blah Blah";
pString1 = strdup( std::string( pString1 ).append( pString2 ) );
}
However, I would prefer to use std::string for my own stuff:
#include <string>
int main ( void ) {
std::string pString1 = "Blah "; // rhs returned by some library
std::string pString2 = "Blah Blah"; // rhs returned by some library
pString1.append( pString2 );
}
Best
Kai-Uwe Bux
On Thu,01 Jun 2006 08:35:36 +0100,scroopy< sc ***** @ nospam.com>
写道:
On Thu, 01 Jun 2006 08:35:36 +0100, scroopy <sc*****@nospam.com>
wrote:
我总是使用std :: string但我必须使用第三方库
返回const char * s。
首先你需要知道你是否拥有''返回的字符串,即如果
你必须释放(可能删除)返回的字符* 。好的C库
通常不要求用户免费拨打电话。
鉴于:
char * pString1 =" Blah" ;
const char * pString2 =" Blah Blah" ;;
如何将pString2的内容追加到pString? (给出Blah
Blah Blah)
I''ve always used std::string but I''m having to use a 3rd party library
that returns const char*s.
First you need to find out if you ''own'' the returned string, i.e. if
you must free (maybe delete) the returned char*. Good C libraries
usually don''t require the user to call free.
Given:
char* pString1 = "Blah ";
const char* pString2 = "Blah Blah";
How do I append the contents of pString2 to pString? (giving "Blah
Blah Blah")
您可以将const char *附加到std :: string:
string myString =" Blah" ;;
const char * s = myLibFunc(...);
if(s){
myString + = s; //或myString.append(s);
}
// free(s); //如果这是一个糟糕的博物馆
祝福,
Roland Pibinger
You can append a const char* to a std::string:
string myString = "Blah ";
const char* s = myLibFunc (...);
if (s) {
myString += s; // or myString.append(s);
}
// free (s); // if it''s a bad lib
Best wishes,
Roland Pibinger
这篇关于C Style Strings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!